调用 Lambda的Websocket会忽略消息并发送最后接收到的消息。

0

【以下的问题经过翻译处理】 当我调用一个发送数据到客户端的websocket API的lambda时,它发送的不是实际的消息/负载,而是上次收到的消息/负载。

示例:

  • Lambda 1使用载荷“test1”调用Lambda 2> Lambda 2不向客户端发送任何内容
  • Lambda 1使用第二个载荷“test2”调用Lambda 2> Lambda 2向客户端发送“test1”
  • Lambda 1使用第三个载荷“test3”调用Lambda 2> Lambda 2向客户端发送“test2”

我的环境: • Nodejs 16 • WebSocket库:AWS.ApiGatewayManagementApi.postToConnection

WebSocket API上没有缓存。 没有错误,只是奇怪的行为。

1 Risposta
0

【以下的回答经过翻译处理】 我找到了答案,将 forEach 循环改为 for 循环。

原代码如下:

items?.forEach(async (item) => {
  await apigwManagementApi.postToConnection(params).promise()
});

改为:

for (const item of items) {
  await apigwManagementApi.postToConnection(params).promise()
}

我无法解释原理,但它确实有效,消息即时发送到 WebSocket。

profile picture
ESPERTO
con risposta 5 mesi fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande