将 PostToConnection 与 AWS SDK 或 CLI 配合使用 - AWS SDK 代码示例

AWS 文档 SDK 示例 GitHub 存储库中还有更多 AWS SDK 示例。

PostToConnection 与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 PostToConnection

CLI
AWS CLI

向 WebSocket 连接发送数据

以下 post-to-connection 示例向连接到指定 WebSocket API 的客户端发送一条消息。

aws apigatewaymanagementapi post-to-connection \ --connection-id L0SM9cOFvHcCIhw= \ --data "Hello from API Gateway!" \ --endpoint-url https://aabbccddee.execute-api.us-west-2.amazonaws.com/prod

此命令不生成任何输出。

有关更多信息,请参阅《Amazon API Gateway 开发人员指南》中的在后端服务中使用 @connections 命令

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 PostToConnection

Rust
适用于 Rust 的 SDK
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

async fn send_data( client: &aws_sdk_apigatewaymanagement::Client, con_id: &str, data: &str, ) -> Result<(), aws_sdk_apigatewaymanagement::Error> { client .post_to_connection() .connection_id(con_id) .data(Blob::new(data)) .send() .await?; Ok(()) } let endpoint_url = format!( "https://{api_id}.execute-api.{region}.amazonaws.com/{stage}", api_id = api_id, region = region, stage = stage ); let shared_config = aws_config::from_env().region(region_provider).load().await; let api_management_config = config::Builder::from(&shared_config) .endpoint_url(endpoint_url) .build(); let client = Client::from_conf(api_management_config);
  • 有关 API 详细信息,请参阅《AWS SDK for Rust API Reference》中的 PostToConnection