0

我一直在研究 websocket 客户端应用程序。

我目前正在使用 ws 客户端库,因为添加一些标头很容易(我需要这个来进行身份验证)。我已经成功连接到服务器,但现在我需要连接到特定频道。

当前代码:

const WebSocket = require('ws');

var option = {
  headers: {
    api_key: 'xxxxx'
  }
};

// I know this is not a correct url, but I removed it for security reasons
const url = '../websocket/'; 

const wss = new WebSocket(url, option);


wss.on('open', () => {

  console.log("Connection is succesfull");

  wss.on('message', message => {

    console.log(message);
  });

});

当我运行代码时,它会打印“连接成功”,但现在我想连接到一个名为 /people 的频道。我怎样才能做到这一点。

我尝试了几件事,例如:

更改了网址 websocket/people。

这不起作用,因为它首先需要对用户进行身份验证,然后才能连接到通道

将 url 更改为 websocket/?people。

我没有收到错误消息,但是当有内容发送到此频道时,我也没有收到回复。

在 open 函数中添加:

 wss.on('/people', message => {

        console.log(message);
 });

我没有收到错误消息,但是当有内容发送到此频道时,我也没有收到回复。

作为记录。我只能访问文档,而不能访问服务器。

4

0 回答 0