开发者

What is wrong with my nodejs simple code

开发者 https://www.devze.com 2023-03-16 01:24 出处:网络
This is my nodejs code : var io = require(\'socket.io\').listen(8011); io.sockets.on(\'connection\', function (socket) {

This is my nodejs 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);
  });
});

and the client html is :

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

when I run it using node a.js, I get the following error:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Object 8011 has no method 'listeners'
    at new <anonymous> (/home/zjm1126/node_modules/socket.io/lib/socket.io/listener.js:29:31)
    at Object.listen (/home/zjm1126/node_mod开发者_开发技巧ules/socket.io/lib/socket.io/index.js:11:10)
    at Object.<anonymous> (/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.<anonymous> (module.js:426:10)
    at EventEmitter._tickCallback (node.js:126:26)

but this is the demo code in http://socket.io/, why do I get this error?

updated:

When I run npm update socket.io, the server side is running

but the client side gives this error :

io is not defined
 var socket = io.connect('http://localhost:8011');

what is wrong with this?


The code you specified below is for socket.io 0.7.x(API) and not for 0.6.x so that is where the error could be. You should try to npm update socket.io . My dependencies are the following according to npm ls:

alfred@alfred-laptop:~/node/socketio-demo$ npm ls
/home/alfred/node/socketio-demo
└─┬ socket.io@0.7.2 
  ├── policyfile@0.0.3 
  └── socket.io-client@0.7.2 

Also are you using socket.io client html from same node.js server or from some other server(PHP or something for example).


I also did at test via PHP:

mkdir socketio-demo`
cd socketio-demo`
gedit server.js # my quick/dirty editor

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);
  });
});

alfred@alfred-laptop:~/node/socketio-demo$ node server.js 
   info  - socket.io started

I also have a PHP server running on port 80:

gedit socket.php

<script src="http://localhost:8011/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8011/');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

When on another host you need to update <script src="http://localhost:8011/socket.io/socket.io.js"></script> to point to your socket.io server

and var socket = io.connect('http://localhost:8011/'); to point to your socket.io server. Using this setup I got it working without any problems at all.


ZJM - I had an identical build to yours (trying to get it working out of the box). I was getting a different error (the socket.js file was being served and logged as such but no connection took place). I think (90% certain - seemed to have some caching issues as well) that on the client side, supplying socket.io with no parameters solved the problem.

No idea if this will help you but hope it

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号