views:

256

answers:

2

Hi,

I'm trying to push data from my browser (Chrome) to a nodejs server and I'm having terrible trouble.

Basically, I've got this code that appears in the browser:

<script src="./Socket.IO/socket.io.js"></script>
<script>
        io.setPath('./Socket.IO/');

        var socket=new io.Socket('xxx.xxx.xxx.xxx');

        socket.on('connect',function(){
                alert('connect');       
        });
        socket.on('message',function(msg){
                alert('message'+msg);
        });
        socket.on('close',function(){
                alert('close');
        });
        socket.on('disconnect',function(){
                alert('disconnect');
        });
        socket.connect();

</script>

The only alert that's appearing is the 'close' alert.

Here is my server code:

var http=require('http');
var io=require('./Socket.IO-node');

var server=http.createServer(function(req, res){
    // your normal server code
    res.writeHeader(200, {'Content-Type': 'text/html'});
    res.writeBody('<h1>Hello world</h1>');
    res.finish();
});

var socket=io.listen(server,{transports:websocket,port:8080});

socket.on('connection',function(client){
        console.log('connection');
});

You can see I'm trying to log the connections to the console, but nothing ever shows up. I've been googling and trying to work through the Socket.IO examples at http://github.com/LearnBoost/Socket.IO-node and nothing seems to be working for me...

Any pointers is much appreciated.

Edit: Hi,

I've got the following server code now:

var http=require('http');
var io=require('./Socket.IO-node');

var server=http.createServer(function(req, res){
        //your normal server code
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('Hello world');
        res.end();
});

server.listen(8124);
server=io.listen(server);
server.on('connection', function(client){
        console.log('EOH connected');
        sys.log('EOH connected');
});
server.on('clientConnect',function(client){
        console.log('EOH connected');
        sys.log('EOH connected');
});
server.on('clientDisconnect',function(client){
        console.log('EOH disconnected');
        sys.log('EOH disconnected');
});

And the following client code:

<script>
window.onload=function(){
io.setPath('./Socket.IO/');
socket = new io.Socket('localhost', {'port': 8124});
socket.connect();
socket.send('xxx');
}
</script>

When I load the client code at localhost:8124, I'd expect some sort of 'clientConnect' events to be fired. I'm also sending data on the socket object and there's nothing appearing on the server... Totally stumped. Looking at using node-websocket-server now (http://github.com/miksago/node-websocket-server).

A: 

this code works for me:

var server = http.createServer(...);    

server.listen(8124);
server = io.listen(server);
server.on('connection', function(client){
    sys.log('client connected');
});

and then on client:

io.setPath('/media/js/Socket.IO/');
socket = new io.Socket('localhost', {'port': 8124});
socket.connect();

Maybe the explicit port parameter will help in your case?

Mike Korobov
Hey, thanks for that. I tried the code you gave and I'm not getting any messages on the server-side console ;(
Eamorr
Do your node instance have enough rights to open the port? Maybe this is a reason the examples don't work. You may try sudo node server.jsAlso, does you hello-world work? Visit http://localhost:8124/ - if there is no 'hello world' then the problem is with server part of your app - with basic node.js deployment. If so try first get it running without Socket.IO. Also check the error console in browser: if some other js fails then the connect method may not be executed.
Mike Korobov
It is also possible that some files are missing from socket.io. They ARE missing in github downloads now. Try to do the git checkout: git clone git://github.com/LearnBoost/Socket.IO.git --recursiveinstead of downloading zip archive.
Mike Korobov