Socket.IO client library gives “welcome to socket.

2020-03-04 03:24发布

问题:

After some time I tried working with node.js and socket.IO again, but it didn't work as expected:

My setup

  1. Downloaded node.js from github and compiled it on my external webserver running on debian squeeze
  2. Created a directory for the node.js project
  3. Added socket.io locally with npm
  4. Created socketIO_server.js and just added this single line of code:

    var socketIO = require('socket.io').listen(8000);
    
  5. Started the socketIO_server.js and console log says "info - socket.io started"

  6. Opening http://domain.tld:8000 gives the message "welcome to socket.io"

The Problem

When I try to access the client library by http://domain.tld:8000/socket.io/socket.io.js it gives also the message "welcome to socket.io", but the console log shows "served static content /socket.io.js". I have no idea why this happens! I though the nginx server running parallel causes this problem but stopping the server didn't change anything.

Thanks for reading and help!

回答1:

This is caused by a commit made to the EventEmitter lib of nodejs in a recent change. I've opened an issue on socket.io.

https://github.com/LearnBoost/socket.io/issues/987

UPDATE

This issue has been fixed as of socket.io 0.9.12

Fix: https://github.com/LearnBoost/socket.io/blob/0.9.12/lib/manager.js#L116

Commit: https://github.com/LearnBoost/socket.io/commit/0d3313f536d0231932dd6617db449a071f5bc03a


Can not serve socket.io.js when listening on port. (node 0.9.1-pre, socket.io 0.9.9)

Due to a recent commit to node, you can no longer splice out event listeners. This causes socket.io to display the welcome message when trying to access the socket.io.js client file as the original event listener does not get removed.

Example breakage:

var socketIO = require('socket.io').listen(8000);

This breaks due to the way node 0.9.1-pre changed the way you can access listeners for the EventEmitter lib.

nodejs commit that breaks socket.io

Make EventEmitter.listeners(event) return a copy of the listeners array instead of the array itself.

EventEmitter.prototype.listeners = function(type) {
   if (!isArray(this._events[type])) {
     this._events[type] = [this._events[type]];
   }        
-  return this._events[type];   
+  return this._events[type].slice(0);
};

https://github.com/joyent/node/commit/20e12e4be37f394672c001fdb9b05c0275731901#L1R245

Relative socket.io code:

// reset listeners
this.oldListeners = server.listeners('request').splice(0);

https://github.com/LearnBoost/socket.io/blob/master/lib/manager.js#L115



回答2:

I ran into this problem a few days ago. Had to downgrade socket.io to v0.8.7 and it worked fine.