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).
Solution: git clone git://github.com/LearnBoost/Socket.IO.git --recursive
Use --recursive flag. Doh!