【问题标题】:WebSocket connection timing outWebSocket 连接超时
【发布时间】:2020-05-04 00:46:34
【问题描述】:

我正在编写一个 JavaScript 客户端和 node.JS 服务器之间的 WebSocket 连接。

我的客户代码:

<!DOCTYPE html>
<html>

<head>
    <title>Socket Test</title>
</head>

<body>
</body>
<script>
    const ws = new WebSocket('wss://mydomain.in:26031/');
    ws.onopen = function () {
        console.log('WebSocket Client Connected');
        ws.send('Hi this is web client.');
    };
    ws.onmessage = function (e) {
        console.log("Received: '" + e.data + "'");
    };
</script>

服务器代码:

const net = require('net');

const server = net.createServer((socket) => {
    socket.on('data', (data) => {
        console.log(data.toString());
    });
    socket.write('SERVER: Hello! This is server speaking.\n');
    socket.end('SERVER: Closing connection now.\n');
}).on('error', (err) => {
    console.error(err);
});
server.listen(26031, () => {
    console.log('opened server on', server.address().port);
});

我得到的错误: WebSocket connection to 'wss://mydomain.in:26031/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

我在堆栈溢出中尝试了其他答案,但从我的角度来看,一切似乎都很完美。服务器端口已打开以接受请求。还有什么问题?

【问题讨论】:

标签: javascript html node.js websocket


【解决方案1】:

仅当您安装了 SSL 时才使用“wss://” 或者尝试使用 ws://

您也可以尝试使用 Socket.IO 进行套接字连接。

【讨论】:

猜你喜欢
  • 2014-05-15
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多