首页 > 解决方案 > 当部署在 cpanel(vps) 上时,客户端不会与 socket.io (nodejs) 服务器建立连接。错误:意外的服务器响应:101

问题描述

套接字连接在本地主机上工作,但在 cpanel 上上传时不起作用。

服务器端代码

const express = require('express');
const app = express()
const http = require('http').createServer(app);
const io = require('socket.io')(http, {'pingInterval': 10000, 'pingTimeout': 10000});
const bodyParser = require('body-parser');
const port = 3000 // i tried a rage of ports but non of them works

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json({extended: true,limit: '50mb'}));

io.on('connection', (socket) => {
    console.log(socket.id)
    io.emit('message', "hi");
});

app.get("/", (req, res) => {
    res.send("Get method called")
})

app.post("/", (req, res) => {
    res.send("Post method called")
})

http.listen(port, () => console.log("Listening on port: ",port))

(服务器)邮递员的结果:

在此处输入图像描述

(本地主机)邮递员的结果:

在此处输入图像描述

我不知道该怎么办,我确实尝试了很多端口,但仍然没有成功。

标签: javascriptnode.jsreactjsexpresssocket.io

解决方案


推荐阅读