socket.on('connection' ... 事件从未触发 nodejs + express + socket.io

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8424886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:52:22  来源:igfitidea点击:

socket.on('connection' ... event never fired nodejs + express + socket.io

node.jswebsocketexpresssocket.io

提问by Gaston

Problemsocket.io NOT working

问题socket.io 不工作

Details

细节

  • Generated a project with express [folder]; cd [folder]; npm install;
  • Setup socket.io npm install socket.io
  • Run node app with below code
  • Client connectevent fires but server connectionNEVER fired.
  • 生成了一个项目 express [folder]; cd [folder]; npm install;
  • 设置 socket.io npm install socket.io
  • 使用以下代码运行节点应用程序
  • 客户端连接事件触发但服务器连接从未触发。

Setup

设置

  • ServerAWS Free Tier, Ubuntu 11.10, ami-a7f539ce
  • nodejs v0.6.5
  • express v2.5.1
  • socket.io v0.8.7
  • 服务器AWS 免费套餐,Ubuntu 11.10,ami-a7f539ce
  • nodejs v0.6.5
  • 快递 v2.5.1
  • socket.io v0.8.7

Client

客户

 var socket = io.connect('http://example.com:3000');

 socket.on('connect', function() { 
    console.log('connected');
 });

 socket.on('message', function(msg){
    console.log(msg);
 });

 socket.on('disconnect', function() {
    console.log('disconnected');
 });

 socket.on('error', function (e) {
    console.log('System', e ? e : 'A unknown error occurred');
 });

Server

服务器

 [...]

 app.listen(3000);

 // socket.io setup
 var socket = require('socket.io').listen(app);

 // socket.io connection establishment
 socket.on('connection', function (client) {
    client.send("hello");
    console.log("hello", client);           
 });

Why is connectionevent never fired?

为什么连接事件永远不会被触发?

采纳答案by Ricardo Tomasi

Took a while to notice... the connectionevent is emmited on io.sockets. In your code this would be

花了一段时间才注意到……该connection事件发生在io.sockets。在您的代码中,这将是

socket.sockets.on('connection', function (client) {
  client.send("hello")
  console.log("hello", client)
})

You should use ioinstead of socketas the var name to avoid this confusion.

您应该使用io而不是socket作为 var 名称来避免这种混淆。

回答by Valerio

Ricardo Tomasiis correct, saved my life, i was going crazy.

Ricardo Tomasi是对的,救了我的命,我快疯了。

Altough things changed, we are in 2013, there's still an issue opened (since 2 years) on this

尽管事情发生了变化,我们是在 2013 年,但仍然存在一个问题(自 2 年以来)

probably something changed, anyway to register the 'connect' event i had to do this:

可能发生了一些变化,无论如何要注册“连接”事件,我必须这样做:

var openSocket = function (uniqueID) {
  var appSocket = io.connect('/'+uniqueID, { transports: ['websocket', 'xhr-polling']});
  appSocket.socket.on('connect', function () {
    console.log('i did connect.');
  });
  return appSocket;
};

回答by gogaman

The following did a trick for me with: socket.io-client: "^0.9.16"

以下为我做了一个技巧:socket.io-client:“^0.9.16”

io.connect("http://localhost:4000", {'force new connection': true});

Now 'connect' event fires consistently and there is no re-use.

现在 'connect' 事件持续触发并且没有重用。

I figured out this option by examining socket.io-client/lib/io.js line: 192 Since I can't even find this io.js file in github, I think there is refactoring in future releases, and this option might not work.

我通过检查 socket.io-client/lib/io.js line: 192 找到了这个选项,因为我什至在 github 中找不到这个 io.js 文件,我认为在未来的版本中会有重构,这个选项可能不会工作。

At least this might be helpful to somebody who will take this temporary work around.

至少这可能对将要解决此临时工作的人有所帮助。