要使用AWS.ApiGatewayManagementApi()的postToConnection方法,您需要满足以下条件:
console.log(require('aws-sdk').VERSION);
一旦满足了上述要求,您可以使用以下代码示例来调用postToConnection方法:
const AWS = require('aws-sdk');
const apiGateway = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: 'YOUR_API_GATEWAY_ENDPOINT' // 替换为您的API Gateway管理API端点
});
const connectionId = 'YOUR_CONNECTION_ID'; // 替换为您要发送消息的连接ID
const postData = JSON.stringify({
message: 'Hello from AWS Lambda!'
});
const params = {
ConnectionId: connectionId,
Data: postData
};
apiGateway.postToConnection(params, (err, data) => {
if (err) {
console.log('Error:', err);
} else {
console.log('Success:', data);
}
});
请确保将YOUR_API_GATEWAY_ENDPOINT
替换为您的API Gateway管理API端点,并将YOUR_CONNECTION_ID
替换为您要发送消息的连接ID。
这是一个基本的示例,您可以根据您的需求进行修改和扩展。请注意,postToConnection方法是一个异步操作,您可以使用回调函数处理成功或错误的响应。
上一篇:AWS.ApiGatewayManagementApi BadRequestException: 连接标识无效。
下一篇:AWS.ApiGatewayManagementApi.postToConnection()被调用一次却执行两次