Hen*_*ash 4 javascript node.js socket.io
一段时间后,我再次尝试使用node.js和socket.IO,但它没有按预期工作:
创建了socketIO_server.js并添加了这一行代码:
var socketIO = require('socket.io').listen(8000);
Run Code Online (Sandbox Code Playgroud)启动socketIO_server.js和控制台日志说"info - socket.io started"
当我尝试通过http://domain.tld:8000/socket.io/socket.io.js访问客户端库时,它还会给出消息"welcome to socket.io",但控制台日志显示"提供的静态内容" /socket.io.js".我不知道为什么会这样!我虽然运行并行的nginx服务器导致此问题,但停止服务器并没有改变任何东西.
感谢阅读和帮助!
这是由最近更改中对nodejs的EventEmitter lib的提交引起的.我在socket.io上打开了一个问题.
https://github.com/LearnBoost/socket.io/issues/987
UPDATE
从socket.io 0.9.12开始修复此问题
修复:https: //github.com/LearnBoost/socket.io/blob/0.9.12/lib/manager.js#L116
提交:https: //github.com/LearnBoost/socket.io/commit/0d3313f536d0231932dd6617db449a071f5bc03a
在端口上侦听时无法提供socket.io.js.(node 0.9.1-pre,socket.io 0.9.9)
由于最近提交节点,您不能再拼接出事件侦听器.这会导致socket.io在尝试访问socket.io.js客户端文件时显示欢迎消息,因为原始事件侦听器未被删除.
示例破损:
var socketIO = require('socket.io').listen(8000);
Run Code Online (Sandbox Code Playgroud)
由于节点0.9.1-pre改变了您可以访问EventEmitter lib的侦听器的方式,这会中断.
nodejs提交破坏了socket.io
使EventEmitter.listeners(event)返回侦听器数组的副本而不是数组本身.
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);
};
Run Code Online (Sandbox Code Playgroud)
https://github.com/joyent/node/commit/20e12e4be37f394672c001fdb9b05c0275731901#L1R245
相对socket.io代码:
// reset listeners
this.oldListeners = server.listeners('request').splice(0);
Run Code Online (Sandbox Code Playgroud)
https://github.com/LearnBoost/socket.io/blob/master/lib/manager.js#L115