向AWS API Gateway中的新WebSocket连接发送消息

huangapple go评论74阅读模式
英文:

Send a message to new Websocket connection in AWS API Gateway

问题

以下是已翻译的代码部分:

import {
  ApiGatewayManagementApiClient,
  PostToConnectionCommand,
} from "@aws-sdk/client-apigatewaymanagementapi";

export const handler = async (event) => {
  const domain = event.requestContext.domainName;
  const stage = event.requestContext.stage;
  const connectionId = event.requestContext.connectionId;
  const callbackUrl = `https://${domain}/${stage}`;
  const client = new ApiGatewayManagementApiClient({ endpoint: callbackUrl });

  const requestParams = {
    ConnectionId: connectionId,
    Data: JSON.stringify({ data: "Hello World" }),
  };

  const command = new PostToConnectionCommand(requestParams);

  try {
    await client.send(command);
  } catch (error) {
    console.log(error);
  }

  const response = {
    statusCode: 200
  };
  return response;
};

希望这对你有所帮助。如果你有任何其他问题,请随时提出。

英文:

I have an AWS API Gateway websocket endpoint for $connect which calls a lambda function. I want that lambda function to send back a message to the user after they connect. Is there any way to do this?

This is what I have tried in the lambda. The message I send is never received, and I assume that is because the connection is not completed yet. However I don't see any way I can send a message after it has been connected.

import {
  ApiGatewayManagementApiClient,
  PostToConnectionCommand,
} from "@aws-sdk/client-apigatewaymanagementapi";

export const handler = async (event) => {
  const domain = event.requestContext.domainName;
  const stage = event.requestContext.stage;
  const connectionId = event.requestContext.connectionId;
  const callbackUrl = `https://${domain}/${stage}`;
  const client = new ApiGatewayManagementApiClient({ endpoint: callbackUrl });

  const requestParams = {
    ConnectionId: connectionId,
    Data: JSON.stringify({ data: "Hello World" }),
  };

  const command = new PostToConnectionCommand(requestParams);

  try {
    await client.send(command);
  } catch (error) {
    console.log(error);
  }

  const response = {
    statusCode: 200
  };
  return response;
};

答案1

得分: 2

代码部分不需要翻译。以下是翻译好的内容:

在处理程序内部无法执行此操作,因为只有在处理程序无错误返回时才会建立连接。

直到与$connect路由相关联的集成执行完成之前,升级请求处于挂起状态,实际连接不会建立。如果$connect请求失败(例如由于AuthN/AuthZ失败或集成失败),则不会建立连接。

来源

您可以使用其他AWS服务,例如SQS,将消息添加到队列,并触发一个Lambda函数来发送该消息。

英文:

You cannot do this inside of the handler because the connection is only established once the handler returns without an error.

> Until execution of the integration associated with the $connect route is completed, the upgrade request is pending and the actual connection will not be established. If the $connect request fails (e.g., due to AuthN/AuthZ failure or an integration failure), the connection will not be made.

source

You could use another AWS service such as SQS in order to add a message to the queue and have that trigger a lambda which would send the message

huangapple
  • 本文由 发表于 2023年3月4日 01:04:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定