Note that there are some explanatory texts on larger screens.

plurals
  1. PONodejs & websockets
    primarykey
    data
    text
    <p>I'm trying to push data from my browser (Chrome) to a nodejs server and I'm having terrible trouble.</p> <p>Basically, I've got this code that appears in the browser:</p> <pre><code>&lt;script src="./Socket.IO/socket.io.js"&gt;&lt;/script&gt; &lt;script&gt; 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(); &lt;/script&gt; </code></pre> <p>The only alert that's appearing is the 'close' alert.</p> <p>Here is my server code:</p> <pre><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('&lt;h1&gt;Hello world&lt;/h1&gt;'); res.finish(); }); var socket=io.listen(server,{transports:websocket,port:8080}); socket.on('connection',function(client){ console.log('connection'); }); </code></pre> <p>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 <a href="http://github.com/LearnBoost/Socket.IO-node" rel="noreferrer">http://github.com/LearnBoost/Socket.IO-node</a> and nothing seems to be working for me...</p> <p>Any pointers is much appreciated.</p> <p>Edit: Hi,</p> <p>I've got the following server code now:</p> <pre><code>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'); }); </code></pre> <p>And the following client code:</p> <pre><code>&lt;script&gt; window.onload=function(){ io.setPath('./Socket.IO/'); socket = new io.Socket('localhost', {'port': 8124}); socket.connect(); socket.send('xxx'); } &lt;/script&gt; </code></pre> <p>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 (<a href="http://github.com/miksago/node-websocket-server" rel="noreferrer">http://github.com/miksago/node-websocket-server</a>).</p> <p>Solution: git clone git://github.com/LearnBoost/Socket.IO.git --recursive</p> <p>Use --recursive flag. Doh!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload