javascript node.js 和 socket.io。websocket的传输类型配置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14333942/
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-10-26 21:20:06  来源:igfitidea点击:

node.js and socket.io. transport type configuration for websocket?

javascriptnode.jswebsocketsocket.io

提问by Rai Blaze

This question concerns socket.io versions < 0.9.x. Newer versions have different transports and methods of setting transports.

这个问题涉及 socket.io 版本 < 0.9.x。较新的版本具有不同的传输和设置传输的方法。

I test node js and socket.io in two week. when I began I get the problem from socket.send(message)function in client. I can't send any message to the server. But I still can receive messages from the server. I solved this problem when I found the configure transport of server side:

我在两周内测试了 node js 和 socket.io。当我开始时,我从socket.send(message)客户端的功能中得到了问题。我无法向服务器发送任何消息。但是我仍然可以从服务器接收消息。当我找到服务器端的配置传输时,我解决了这个问题:

socket.set('transports',[
   'xhr-polling'
  , 'jsonp-polling'
]);

Everything good. Now I can send messages to the server as well. But I still have a question why I have to configure transport. Default socket.io use websocket transport setting like this:

一切安好。现在我也可以向服务器发送消息了。但是我仍然有一个问题,为什么我必须配置传输。默认 socket.io 使用 websocket 传输设置,如下所示:

socket.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
]);

so it uses websocket at first, not xhr-polling. But the server cannot receive any messages sent from the client when using socket.send(msg)even socket.emit(...).

所以它首先使用 websocket,而不是 xhr-polling。但是使用socket.send(msg)even时,服务器无法接收客户端发送的任何消息socket.emit(...)

So the problem is: what is not supporting websocket here? browser or node.js ... I'm sorry but I searched so many pages from google and I haven't found an answer for this.

所以问题是:这里不支持 websocket 是什么?浏览器或 node.js ......对不起,我从谷歌搜索了很多页面,但我没有找到答案。

I use node.js version 0.8.16, socket.io version 0.9.13 and newest browsers: chrome, firefox, opera

我使用 node.js 版本 0.8.16、socket.io 版本 0.9.13 和最新的浏览器:chrome、firefox、opera

I want to use websocket not xhr-polling.

我想使用 websocket 而不是 xhr-polling。

回答by Gianluca Pinoci

That's odd because even if websockets are not supported by your server configuration, socket.io will select the next best available method (in your case xhr-polling). Actually, you shouldn't even need to set those transports as socket.io will try to use 'websocket' as a primary method by default. This may indicate some other problem, possibly with your code?

这很奇怪,因为即使您的服务器配置不支持 websockets,socket.io 也会选择下一个最佳可用方法(在您的情况下为 xhr-polling)。实际上,您甚至不需要设置这些传输,因为 socket.io 默认会尝试使用“websocket”作为主要方法。这可能表明其他问题,可能与您的代码有关?

What is not supporting websockets is definitely not the browsers you're using nor node.js of course. This will depend on your server setup.

不支持 websockets 的绝对不是你使用的浏览器,当然也不是 node.js。这将取决于您的服务器设置。

First check:

首先检查:

  1. The port you're listening to is open in your firewall
  2. Your webserver supports websockets. If you're using Apache and proxing your request to an internal IP:PORT, websocket will not work unless you install something like apache-websocketor pywebsocket
  1. 您正在侦听的端口已在防火墙中打开
  2. 您的网络服务器支持 websockets。如果您使用 Apache 并将您的请求代理到内部 IP:PORT,除非您安装apache-websocketpywebsocket 之类的东西,否则 websocket 将无法工作

What finally solved my issue was to disable Apache listening on port 80 and having node.js listening on that port. Here's the answer on SO that helped me: https://stackoverflow.com/a/7640966/2347777

最终解决我的问题的是禁用 Apache 侦听端口 80 并让 node.js 侦听该端口。这是对我有帮助的 SO 的答案:https: //stackoverflow.com/a/7640966/2347777