5

I'm trying to use this simple tutorial:

http://socket.io/socket-io-with-apache-cordova/

My node.js is working fine and i'm emulating to iOS without problem, but the socket.io isn't working, here is my javascript(the same way as the tutorial above):

app.initialize();

document.addEventListener('deviceready', function() {
    console.log(socket);
    socket.on('connect', function() {
        socket.on('text', function(text) {
            alert(text);
        });
    });
});

and one more thing how can i get this console.log to debug?

here is how i'm getting socket.io(the same way as the tutorial above):

<script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script>

here is my server.js(the same way as the tutorial above):

var server  = require('http').createServer();
var io      = require('socket.io')(server);

io.sockets.on('connection', function (socket) {
    console.log('socket connected');

    socket.on('disconnect', function () {
        console.log('socket disconnected');
    });

    socket.emit('text', 'wow. such event. very real time.');
});

server.listen(3000);

I think, that the problem and the tutorial didn't told is how i would connect my cordova app with port 3000

Rodrigo Fonseca
  • 944
  • 7
  • 21

2 Answers2

12

I made it, this tutorial is very good but it's not totally right.

You have to connect the socket to your server first (i'm using localhost and port 3000, but if you're using some server outside, i think you've to just put the ip and port):

var socket = io.connect('http://localhost:3000');

and after that, you call "socket.io", here is my complete code:

document.addEventListener('deviceready', function() {
        var socket = io.connect('http://localhost:3000');
        socket.on('connect', function() {
            socket.on('text', function(text) {
                alert(text);
            });
        });
    });
Rodrigo Fonseca
  • 944
  • 7
  • 21
-1
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
var socketHost = "http://localhost:3000";
var socket = io.connect(socketHost);
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114