socketio,如果本地托管,客户端可以连接到服务器,但在尝试连接到单独虚拟服务器上托管的服务器时返回错误

发布于 2025-01-20 01:35:33 字数 1297 浏览 0 评论 0原文

我正在尝试使用socket.io 和express 制作一个简单的服务器,并通过网站连接到它。 当我使用 localhost 遵循有关 socketio 的教程时,一切正常,但是当我将服务器放在虚拟服务器上并尝试连接到它时,我收到此错误:

无法加载资源:net::ERR_SSL_PROTOCOL_ERROR

以及:

GET <一href="https://54.53.0.254:47185/socket.io/?EIO=4&transport=polling&t=O09jjrs" rel="nofollow noreferrer">https://54.53.0.254:47185/socket.io /?EIO=4&transport=polling&t=O09jjrs net::ERR_SSL_PROTOCOL_ERROR

这是我的服务器代码:

const express = require('express');
const app = express();
const server = app.listen(47185);
const socket = require('socket.io');
const io = socket(server)
console.log('server running on port 47185');

io.sockets.on('connection', newConnection);

function newConnection(socket) {
    console.log('new connection: ' + socket.id);
    socket.on('input', inputLog)

    function inputLog(data) {
        socket.broadcast.emit('input', data);
        console.log(data);
    }
}

这是我的客户端代码(这是与socket.io相关的所有内容,其余的仅用于网站)

var options = {
    rejectUnauthorized:false
}
var socket;
socket = io.connect('89.58.0.199:47185', options);

socket.on('input', foreignInput)

function foreignInput(data) {
    terminal_animate('\n' + data)
}

我尝试了许多不同的修复程序并用谷歌搜索了我能想到的所有内容,并且我只是不确定问题是什么。

谁能帮我解决这个问题吗?提前致谢。

I am trying to make a simple server with socket.io and express and connect to it through a website.
when i followed a tutorial on socketio with localhost, everything worked fine, but when i put the server on a vserver, and tried to connect to it, i got this error:

Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR

as well as:

GET https://54.53.0.254:47185/socket.io/?EIO=4&transport=polling&t=O09jjrs net::ERR_SSL_PROTOCOL_ERROR

here is my server code:

const express = require('express');
const app = express();
const server = app.listen(47185);
const socket = require('socket.io');
const io = socket(server)
console.log('server running on port 47185');

io.sockets.on('connection', newConnection);

function newConnection(socket) {
    console.log('new connection: ' + socket.id);
    socket.on('input', inputLog)

    function inputLog(data) {
        socket.broadcast.emit('input', data);
        console.log(data);
    }
}

and here is my client code (this is all that relates to socket.io, the rest is just for the website)

var options = {
    rejectUnauthorized:false
}
var socket;
socket = io.connect('89.58.0.199:47185', options);

socket.on('input', foreignInput)

function foreignInput(data) {
    terminal_animate('\n' + data)
}

i have tried many different fixes and googled everything i can think of, and i'm just not sure what the problem is.

can anyone help me out with this issue? thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2025-01-27 01:35:34

文档中,根据 客户端初始化部分,在 Node.js 中,您应该在连接到服务器时提供协议。

// the following forms are similar
const socket = io("https://server-domain.com");
const socket = io("wss://server-domain.com");
const socket = io("server-domain.com"); // only in the browser when the page is served over https (will not work in Node.js)

前两个示例显示安全 https/wss 作为协议,因为您需要从服务器提供所需的文件,例如 文档

使用 http/ws 作为协议,它应该可以工作,但通信不安全

服务器初始化/使用 Express 显示了调用 .listen 来自 http 模块的 createServer 返回值,并以 app 作为参数给出。

const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");

const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { /* options */ });

io.on("connection", (socket) => {
  // ...
});

httpServer.listen(3000);

警告说:

使用 app.listen(3000) 在这里不起作用,因为它会创建一个新的 HTTP 服务器。

In the documentation, according to the Client Initialization part, in node.js you should provide the protocol when connecting to the server.

// the following forms are similar
const socket = io("https://server-domain.com");
const socket = io("wss://server-domain.com");
const socket = io("server-domain.com"); // only in the browser when the page is served over https (will not work in Node.js)

The first two example shows the secure https/wss as protocol, for that you need to serve the required files from the server, example in the documentation.

With http/ws as protocol it should work, but the communication will not be secure.

The Server Initialization / With Express shows an example to call .listen on the return value of createServer from the http module, with the app given as a parameter.

const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");

const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { /* options */ });

io.on("connection", (socket) => {
  // ...
});

httpServer.listen(3000);

With a caution that says:

Using app.listen(3000) will not work here, as it creates a new HTTP server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文