socketio,如果本地托管,客户端可以连接到服务器,但在尝试连接到单独虚拟服务器上托管的服务器时返回错误
我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档中,根据 客户端初始化部分,在 Node.js 中,您应该在连接到服务器时提供协议。
前两个示例显示安全 https/wss 作为协议,因为您需要从服务器提供所需的文件,例如 文档。
使用 http/ws 作为协议,它应该可以工作,但通信不安全。
服务器初始化/使用 Express 显示了调用
.listen
来自http
模块的createServer
返回值,并以app
作为参数给出。警告说:
In the documentation, according to the Client Initialization part, in node.js you should provide the protocol when connecting to the server.
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 ofcreateServer
from thehttp
module, with theapp
given as a parameter.With a caution that says: