【问题标题】:AWS Websocket doesnt receive previous message until new message is sentAWS Websocket 在发送新消息之前不会收到先前的消息
【发布时间】:2022-03-29 21:37:34
【问题描述】:

在大多数情况下,消息会正常传递,但特别是在接收客户端发送消息之前,不会收到一些消息。对于特定方法/消息,每次都会发生这种情况,但对于其他方法/消息,根本不会

示例:user1 发送消息,user2 然后发送消息以接收来自user1 的消息。


相关资料

  1. 已删除问题:websocket receives previous message only when new message is sent
  2. Github 问题:webSocket client does not receive messages before sending...

【问题讨论】:

  • 你在这方面有什么收获吗?我们看到了类似的行为
  • @BamdadDashtban 不,我没有,有点放弃了。你们呢,你们都能够在这个问题上取得进展吗?
  • 我们不得不重写 lambda。我认为这是承诺的问题

标签: websocket aws-lambda aws-api-gateway


【解决方案1】:

我们遇到了这个问题,解决方案与我们如何编写承诺有关。我们最初使用亚马逊提供的示例代码

https://github.com/aws-samples/simple-websockets-chat-app/blob/master/sendmessage/app.js#L26

const postCalls = connectionData.Items.map(async ({ connectionId }) => {
    try {
      await apigwManagementApi.postToConnection({ ConnectionId: connectionId, Data: postData }).promise();
    } catch (e) {
      if (e.statusCode === 410) {
        console.log(`Found stale connection, deleting ${connectionId}`);
        await ddb.delete({ TableName: TABLE_NAME, Key: { connectionId } }).promise();
      } else {
        throw e;
      }
    }
  });

而且我很确定将 async 函数作为 map 函数不能正常或可靠地工作(无论出于何种原因。也许这在某处有记录),所以我们将其更改为一个简单的 for 循环并解决了问题.

for(const connection of connectionData.Items) {
    const connectionId = connection.connectionId;
    ...same logic goes here
}

【讨论】:

  • 尚未测试,但我发誓这是否可行......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 2013-08-03
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 2020-02-18
相关资源
最近更新 更多