简体   繁体   English

Socket.IO 不会升级到 websockets 的连接

[英]Socket.IO doesn't upgrade connection to websockets

I'm working on a project with Angular and Node.js where I'm using socket.io (v3.1.0).我正在使用 Angular 和 Node.js 开展一个项目,我正在使用 socket.io (v3.1.0)。 The problem is that the connection between server and client is never upgraded from polling to websockets , even though I can see on the web console that the upgrade connection request is sent and the 101 code response with "swithcing protocols" is received.问题是服务器和客户端之间的连接永远不会从轮询升级到websockets ,即使我可以在 web 控制台上看到发送了升级连接请求并且收到了带有“交换协议”的 101 代码响应。

Here it is the socket.io config code on the server:这是服务器上的 socket.io 配置代码:


const app = express();
const server = await app.listen(port);

const test = io.of('/api/test');
test.on('connect', socket => {
  console.log("Connected");
});


io.listen(server, 
      {
      cors: { 
         origin: "*", 
         methods: ["GET", "POST"],
         allowedHeaders: ["content-type"],
         credentials: true
      }
   });

Client code:客户端代码:

this.socket = io("/api/test")
this.socket.on('connect', () => {
      console.log("Connected");
});

Proxy config file:代理配置文件:

"/sock/*": {
        "target": "http://localhost:3000/socket.io/",
        "logLevel": "debug"
    }

Another thing I'd like to add is that if I change the proxy file to this:我想补充的另一件事是,如果我将代理文件更改为:

"/sock/*": {
        "target": "http://localhost:3000/socket.io/",
        "logLevel": "debug",
        "ws": true
    }

Now in chrome it works only with ws (as expected) but it doesn't work on mozilla (it keeps refreshing the page and throwing an error).现在在 chrome 中它只适用于 ws(如预期的那样),但它不适用于 mozilla(它不断刷新页面并引发错误)。

So the question is: Does anyone know why it won't automatically upgrade to websockets?所以问题是:有谁知道为什么它不会自动升级到 websockets? From What I understand It should send an http request first for handshake but after that it should upgrade to websockets by default, am I wrong?据我了解,它应该首先发送一个 http 请求进行握手,但之后它应该默认升级到 websockets,我错了吗?

I have the same issues with Firefox.我对 Firefox 也有同样的问题。 If I set the script to get the bundle from the server, socket.io sends the script as text/html which forces Firefox to not load it because of X-Content-Type-Options: nosniff header.如果我将脚本设置为从服务器获取包,socket.io 将脚本作为 text/html 发送,这会强制 Firefox 由于 X-Content-Type-Options 不加载它:nosniff Z099FB995346F31C7549Z.E40EDB And if I load the script directly, it doe pooling continuously without any chance of upgrading.而且,如果我直接加载脚本,它会不断地进行池化,而没有任何升级的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM