一段时间后,我尝试再次使用 node.js 和 socket.IO,但它没有按预期工作:
我的设置
- 从 github 下载 node.js 并在运行 debian squeeze 的外部网络服务器上编译
- 为 node.js 项目创建了一个目录
- 使用 npm 在本地添加了 socket.io
创建了 socketIO_server.js 并添加了这行代码:
var socketIO = require('socket.io').listen(8000);
启动了 socketIO_server.js 并且控制台日志显示“信息 - socket.io 已启动”
- 开业 http://domain.tld:8000给出消息“欢迎使用 socket.io”
问题
当我尝试通过 http://domain.tld:8000/socket.io/socket.io.js 访问客户端库时它还显示消息“欢迎使用 socket.io”,但控制台日志显示“提供的静态内容/socket.io.js”。我不知道为什么会这样! 我虽然并行运行的 nginx 服务器导致了这个问题,但停止服务器并没有改变任何东西。
感谢阅读和帮助!
最佳答案
这是由于在最近的更改中对 nodejs 的 EventEmitter 库进行的提交引起的。我已经在 socket.io 上打开了一个问题。
https://github.com/LearnBoost/socket.io/issues/987
更新
此问题已从 socket.io 0.9.12 开始修复
修复: https://github.com/LearnBoost/socket.io/blob/0.9.12/lib/manager.js#L116
promise : https://github.com/LearnBoost/socket.io/commit/0d3313f536d0231932dd6617db449a071f5bc03a
监听端口时无法提供 socket.io.js。 ( Node 0.9.1-pre,socket.io 0.9.9)
由于最近对 Node 的提交,您不能再拼接事件监听器。这会导致 socket.io 在尝试访问 socket.io.js 客户端文件时显示欢迎消息,因为原始事件监听器不会被删除。
破损示例:
var socketIO = require('socket.io').listen(8000);
由于 Node 0.9.1-pre 更改了您访问 EventEmitter 库的监听器的方式,因此中断。
nodejs 提交破坏了 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
相对socket.io代码:
// reset listeners
this.oldListeners = server.listeners('request').splice(0);
https://github.com/LearnBoost/socket.io/blob/master/lib/manager.js#L115
关于javascript - Socket.IO 客户端库给出 "welcome to socket.io"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11889132/