Nodejs ws 模块:TypeScript 中的心跳

D. *_*ser 6 websocket node.js typescript ws

我目前正在努力使用 TypeScript 在 NodeJs 上运行 WebSocket 服务器。作为 WebSocket 服务器实现,我使用ws。除此之外,我还使用@types/ws包进行打字。我希望服务器定期向每个连接的客户端发送 ping 包。ws 模块的自述文件已经提供了一个 JavaScript 实现示例,我也想使用它。您可以在这里找到示例实现:

我面临的问题是,示例实现将“isAlive”属性添加到套接字对象,这显然无法在 TypeScript 中执行,因为套接字类不存在“isAlive”属性。

我的问题:将此属性添加到套接字类的最佳方法是什么?由于我将这些套接字对象作为参数传递,因此我希望避免向以某种方式与套接字一起使用的所有文件添加某种额外的导入。

我想避免将套接字对象类型转换为任何类型。

我尝试围绕 ws 实现一个包装器类,以便我可以实例化我的 WebSocket 类并在整个应用程序中使用它。

我的班级看起来像这样

import * as ws from "ws";


export default class WebSocketServer {
  private wss: ws.Server;
  private onSubscription: (socket: ws, deviceId: string, topicFilter: string) => void;

  constructor(port: number, onSubscription: (socket: ws, deviceId: string, topicFilter: string) => void) {
    this.wss = new ws.Server({ port });
    this.onSubscription = onSubscription;
    const conhandler = this.handleConnection.bind(this);
    this.wss.on("connection", conhandler);
  }

  public static init(onSubscription: (socket: ws, deviceId: string, topicFilter: string) => void): WebSocketServer {
    const port: number = Number(process.env.WSS_PORT);
    if (!port) {
      throw new Error("Unable to create the WebSocket server: Environment variable \"WSS_PORT\" is missing");
    }

    const server: WebSocketServer = new WebSocketServer(port, onSubscription);

    console.info(`Websocket Server listening on port ${port}`);
    return server;
  }


  private handleConnection(socket: ws): void {
    const messageHandler: (socket: ws, data: ws.Data) => void = this.handleMessage.bind(this);
    socket.on("message", data => messageHandler(socket, data));

    /* This is where I try to add the isAlive attribute to a socket object */
    /* but since ws does not have an attribute isAlive, TypeScript complains */
    socket.on("pong", () => socket.isAlive = true);
  }


  private handleMessage(socket: ws, data: ws.Data): void {
    /* Do stuff with the message */
  }


  public quit() {
    this.wss.close();
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑

正如评论者提到的,我尝试了声明合并。我创建了一个名为“Augment.ts”的新文件并添加了以下代码:

import * as ws from "ws";
declare module "ws" {
  interface WebSocket {
    isAlive: boolean;
  }
}
Run Code Online (Sandbox Code Playgroud)

在包含 WebSocketServer 类的文件中,我添加了 Augment.ts 文件的导入,如下所示import "./Augment":结果是另一个错误消息(但不知何故说明相同?): Property 'isAlive' does not exist on type 'import("c:/path/to/project/node_modules/@types/ws/index.d.ts")'.ts(2339)

小智 2

我通过创建一个扩展 Web Socket 的自定义 Socket 接口来让它工作。

interface CustomSocket extends WebSocket {
    isAlive: boolean
}
export default class WebSocket {
    private heartbeat: ReturnType<typeof setInterval>
    private wss: WebSocket.Server


    constructor(port: number) {
        this.wss = new WebSocket.Server({port});

        this.wss.on('connection', this.handleConnection)
    }

    private handleConnection(socket: CustomSocket) {
        socket.on('pong', () => socket.isAlive = true)
    }
}
Run Code Online (Sandbox Code Playgroud)