WebSocket connection to 'wss://******/socket.io/?EIO=4&transport=websocket&sid=T2Sf_4oNIisxKLwsAAAK' failed

huangapple go评论119阅读模式
英文:

WebSocket connection to 'wss://******/socket.io/?EIO=4&transport=websocket&sid=T2Sf_4oNIisxKLwsAAAK' failed

问题

I'm making a WebSocket connection with socket.io and when I log in, I get this error:

WebSocket connection to 'wss://******/socket.io/?EIO=4&transport=websocket&sid=T2Sf_4oNIisxKLwsAAAK' failed: websocket.js:54

But then the connection is established, although I'm not sure if it's done correctly. It hasn't caused me any issues, but that error always appears first when starting, and then everything works fine.

But the thing is, once I'm logged in, everything seems to work just fine after the error. It's just that this error pops up at the beginning, and I want to make sure it won't cause any issues once it's in production. I'd really appreciate any help you can provide. Thanks a bunch in advance!

This is my server code with express:

const http = require('http');
const server = http.createServer(app);

const { Server } = require('socket.io')
const io = new Server(server)

This is my client code:

var socket = io.(Config.getHost());
socket.emit('connection', {
    console.log('*******');
});

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

Image description here

英文:

I'm making a WebSocket connection with socket.io and when I log in, I get this error:

WebSocket connection to 'wss://******/socket.io/?EIO=4&transport=websocket&sid=T2Sf_4oNIisxKLwsAAAK' failed:websocket.js:54

WebSocket connection to 'wss://******/socket.io/?EIO=4&transport=websocket&sid=T2Sf_4oNIisxKLwsAAAK' failed

But then the connection is established, although I'm not sure if it's done correctly. It hasn't caused me any issues, but that error always appears first when starting, and then everything works fine.
But the thing is, once I'm logged in, everything seems to work just fine after the error. It's just that this error pops up at the beginning, and I want to make sure it won't cause any issues once it's in production. I'd really appreciate any help you can provide. Thanks a bunch in advance!

This is my server code with express:

const http = require('http');
const server = http.createServer(app);

const { Server } = require('socket.io')
const io = new Server(server)

This is my client code:

       var socket = io.(Config.getHost());
       socket.emit('connection', {
           console.log('*******');
       });
   
       socket.on('disconnect', function() {
           console.log('*******');
       });

答案1

得分: 0

我不知道这是否有帮助,但我的控制台中没有错误。在我的情况下,我的客户端是一个Angular项目。因此,我在服务器上建立了以下连接。

const app = require('express')();
const httpServer = require('http').createServer(app);
const cors = require('cors');
const io = require('socket.io')(httpServer, {
  cors: {
    origin: 'http://localhost:4200',
    methods: ["GET", "POST"],  
  },
});

const { instrument } = require("@socket.io/admin-ui");
const passport = require('passport');
const bcrypt = require('bcrypt');
const bodyParser = require("body-parser");
const LocalStrategy = require('passport-local').Strategy;
const port = 3000;
const shapes = {}; // 在服务器上存储形状
const { Pool } = require('pg');
const dotenv = require('dotenv');
dotenv.config();
const session = require('express-session');
const pgSession = require('connect-pg-simple')(session);
app.use(require('express').json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors({
  origin: 'http://localhost:4200',
  methods: ["GET", "POST"],
}));

io.on('connection', (socket) => {
    const shapeTypes = ['Circle', 'Rect', 'Line', 'Arrow', 'Text'];

    shapeTypes.forEach((shapeType) => {
      socket.on(`create${shapeType}`, (shapeData) => {
        shapes[shapeData.id] = shapeData;
        io.emit(`create${shapeType}`, shapeData);
      });

      socket.on(`update${shapeType}`, (shapeData) => {
        shapes[shapeData.id] = shapeData;
        io.emit(`update${shapeType}`, shapeData);
      });
    });
      
    // 处理断开连接
    socket.on('disconnect', () => {
      console.log('客户端断开连接');
    });

});

instrument(io, {
  auth: false,
  mode: "development",
});
httpServer.listen(port, () => console.log(`监听端口 ${port}`));

而在我的Angular项目中,我只进行了简单的连接。

import { io } from 'socket.io-client';
public io = io('http://localhost:3000'); // 我在构造函数中初始化了这个

我可以使用 io.emitio.on

英文:

I don't know if this would help, but I have no error in my console. In my own case, my client is an angular project. So I established my connection on server as follow.

const app = require('express')();
const httpServer = require('http').createServer(app);
const cors = require('cors');
const io = require('socket.io')(httpServer, {
cors: {
origin : 'http://localhost:4200',
methods: ["GET", "POST"],  
},
});
const { instrument } = require("@socket.io/admin-ui");
const passport = require('passport');
const bcrypt = require('bcrypt');
const bodyParser = require("body-parser");
const LocalStrategy = require('passport-local').Strategy;
const port = 3000;
const shapes = {}; // Store shapes on the server
const { Pool } = require('pg')
const dotenv = require('dotenv');
dotenv.config();
const session = require('express-session');
const pgSession = require('connect-pg-simple')(session);
app.use(require('express').json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors({
origin : 'http://localhost:4200',
methods: ["GET", "POST"],
}));
io.on('connection', (socket) => {
const shapeTypes = ['Circle', 'Rect', 'Line', 'Arrow', 'Text'];
shapeTypes.forEach((shapeType) => {
socket.on(`create${shapeType}`, (shapeData) => {
shapes[shapeData.id] = shapeData;
io.emit(`create${shapeType}`, shapeData);
});
socket.on(`update${shapeType}`, (shapeData) => {
shapes[shapeData.id] = shapeData;
io.emit(`update${shapeType}`, shapeData);
});
});
// Handle disconnection
socket.on('disconnect', () => {
console.log('A client disconnected');
});
});
instrument(io, {
auth: false,
mode: "development",
});
httpServer.listen(port, () => console.log(`listening on port ${port}`));

and in my angular, I did just simple connection.

import { io } from 'socket.io-client';
public io = io('http://localhost:3000'); // I initialized this in the constructor

I can do io.emit and io.on

答案2

得分: 0

问题已经澄清,问题通过在httpd.conf文件中使用以下设置配置代理以使用WebSocket协议而得以解决:

ProxyPass /socket.io ws://127.0.0.1:5001/socket.io connectiontimeout=3000 timeout=3000
ProxyPassReverse /socket.io ws://127.0.0.1:5001/socket.io

这允许服务器和应用程序之间成功通信。希望这个解决方案能帮助未来遇到类似问题的其他人!

英文:

Just to clarify the issue, the problem was resolved by configuring the proxy in the httpd.conf file with the WebSocket protocol using the following settings:

ProxyPass /socket.io ws://127.0.0.1:5001/socket.io connectiontimeout=3000 timeout=3000
ProxyPassReverse /socket.io ws://127.0.0.1:5001/socket.io

This allowed for successful communication between the server and the application.I hope this solution helps somebody else facing a similar issue in the future!

huangapple
  • 本文由 发表于 2023年6月5日 15:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定