为api网关websocket实现心跳

问题描述 投票:0回答:1

我正在使用 Nodejs + API 网关 Websockets 构建一个聊天室应用程序。我知道每个 websocket 连接在断开连接之前最多只能空闲 29 秒,所以我编写了一个“hearbeat”函数,其中 websocket 连接每 20 秒 ping 一次客户端,以保持连接处于活动状态而不是空闲,并且我有服务器发送回复。尽管我有心跳功能,但我的 websocket 连接仍然每 29 秒自动关闭一次。有人可以让我知道我的实施有什么不正确吗?

Client:

   const ws = new WebSocket(myWebsocketURL);

    ws.onopen = () => {
      setTimeout(() => {
        ws.send("ping");
      }, 20000);
    };

    ws.onclose = () => {
      // handle close
    };

Server:

   const api = new ApiGatewayManagementApi({
      endpoint: 'my-apigateway-websocket-url',
    });

    const params = {
      ConnectionId: connectionId,
      Data: Buffer.from('pong'),
    };

    await api.postToConnection(params).promise()
node.js websocket aws-api-gateway
1个回答
0
投票

ws客户端连接超时的超时限制是多少?

© www.soinside.com 2019 - 2024. All rights reserved.