简体   繁体   English

Socket.io-客户端连接错误:websocket错误

[英]Socket.io-client connection error: websocket error

Previously, I was getting the output "xhr poll error" with the code:以前,我收到 output“xhr 轮询错误”代码:

import { io } from "socket.io-client";

const socket = io("https://socket.io/docs/v4", {
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionDelayMax: 5000,
    reconnectionAttempts: 3
});

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

With a bit of research, this was "resolved" by adding "transports: ['websocket']".通过一些研究,通过添加“传输:['websocket']”“解决”了这个问题。 Here's my current code :这是我当前的代码

import { io } from "socket.io-client";

const socket = io("https://socket.io/docs/v4", {
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionDelayMax: 5000,
    reconnectionAttempts: 3,
    transports: ['websocket']
});

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

Unfortunately, this doesn't really resolve my problem because now I get the following output: websocket error.不幸的是,这并没有真正解决我的问题,因为现在我收到以下 output: websocket 错误。

Other things I've tried: specifying the port after the URL and setting rejectUnauthorized to false.我尝试过的其他事情:在 URL 之后指定端口并将 rejectUnauthorized 设置为 false。 (both giving me the same websocket error. (都给我同样的 websocket 错误。

I'm a bit stumped on what to do next, and it'd be nice to hear some of you guys' feedback!我有点不知道下一步该怎么做,很高兴听到你们中的一些人的反馈!

Socket.io-client version: 4.5.1 Socket.io-客户端版本:4.5.1

By default, the client will try to establish a WebSocket connection if possible and will fall back on HTTP long polling if not, which explains why the first change you mentioned resolved the polling error, but now you have a websocket error.默认情况下,如果可能,客户端将尝试建立 WebSocket 连接,否则将回退到 HTTP 长轮询,这解释了为什么您提到的第一个更改解决了轮询错误,但现在您遇到了 websocket 错误。

If it's attempting to reconnect automatically, you might need to enable CORS or set credentials.如果它尝试自动重新连接,您可能需要启用 CORS 或设置凭据。 If it's not, you should try manually reconnecting after a timeout with socket.connect()如果不是,您应该尝试在超时后使用socket.connect()手动重新连接

It might be a version compatibility issue with Client and Server libraries.这可能是客户端和服务器库的版本兼容性问题。 I was having similar issues and was able to resolve them by adding https://socket.io/docs/v4/server-options/#alloweio3 after looking at https://socket.io/docs/v4/client-installation/#version-compatibility我遇到了类似的问题,并且能够通过在查看https ://socket.io/docs/v4/client-installation/ 之后添加 https://socket.io/docs/v4/server-options/#alloweio3来解决它们#版本兼容性

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

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