Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with my nodejs simple code
    text
    copied!<p>This is my nodejs code :</p> <pre><code>var io = require('socket.io').listen(8011); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); </code></pre> <p>and the client html is :</p> <pre><code>&lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt; &lt;script&gt; var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); &lt;/script&gt; </code></pre> <p>when I run it using node a.js, I get the following error:</p> <pre><code>node.js:134 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: Object 8011 has no method 'listeners' at new &lt;anonymous&gt; (/home/zjm1126/node_modules/socket.io/lib/socket.io/listener.js:29:31) at Object.listen (/home/zjm1126/node_modules/socket.io/lib/socket.io/index.js:11:10) at Object.&lt;anonymous&gt; (/home/zjm1126/桌面/app/a.js:1:93) at Module._compile (module.js:407:26) at Object..js (module.js:413:10) at Module.load (module.js:339:31) at Function._load (module.js:298:12) at Array.&lt;anonymous&gt; (module.js:426:10) at EventEmitter._tickCallback (node.js:126:26) </code></pre> <p>but this is the demo code in <a href="http://socket.io/" rel="nofollow">http://socket.io/</a>, why do I get this error?</p> <p><strong>updated:</strong></p> <p>When I run <code>npm update socket.io</code>, the server side is running </p> <p>but the client side gives this error :</p> <pre><code>io is not defined var socket = io.connect('http://localhost:8011'); </code></pre> <p>what is wrong with this?</p>
 

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