为什么在 nodejs 中采样 websocket 项目卡在“连接”中?提供的示例

Why sample websocket project in nodejs stuck in "Connecting"? example provided

本文关键字:采样 nodejs websocket 项目 为什么 连接      更新时间:2023-09-26

我在测试websocket-server nodejs有这个样本websocket服务器

当我运行它时,它"似乎"已经启动。

但是当我尝试使用tyrus(或任何其他方式)websocket 客户端连接到它时,我陷入了连接阶段:

$ java -jar tyrus-client-cli-1.1.jar ws://localhost:8080/mypath 正在连接到 ws://localhost:8080/mypath...

只是像这样保持在"连接"阶段,据我所知它应该"连接"。

app.js的来源(也出现在GitHub的链接中)

var wsServer = require('ws').Server;
var ws = new wsServer({
  port: '8080',
  path: 'mypath'
});
ws.on('open', function open() {
  ws.send('something');
});
ws.on('message', function(data, flags) {
  console.log("got message: " + data)
  // flags.binary will be set if a binary data is received.
  // flags.masked will be set if the data was masked.
});

根据 RFC 6455,您需要在路径前面加上/

var wss = new wsServer({
  port: 8080,
  path: '/mypath'
});