此代码应通过向发送请求 (CLI) 的用户发送 connectionId 和消息来响应 allowAccess 和拒绝访问事件。
const AWS = require('aws-sdk');
const ENDPOINT = '0ai42l08l3.execute-api.us-east-1.amazonaws.com/production/';
const client = new AWS.ApiGatewayManagementApi({
endpoint: ENDPOINT
});
async function sendToRequestor(id, message) {
try {
await client.postToConnection({
'ConnectionId': id,
'Data': Buffer.from(JSON.stringify(message))
});
} catch(err) {
console.log(err);
}
}
exports.handler = async (event) => {
const connectionId = event.requestContext.connectionId;
const route = event.requestContext.routeKey;
switch(route) {
case '$default':
break;
case '$connect':
break;
case '$disconnect':
break;
case 'createRoom':
break;
case 'allowAccess':
await sendToRequestor(connectionId, 'accessAllowed');
break;
case 'declineAccess':
await sendToRequestor(connectionId, 'accessDeclined');
break;
default:
console.log('Unknown route');
break;
}
const response = {
statusCode: 200,
body: JSON.stringify(event),
};
return response;
};
但是当我使用 wscat 发送 { "action": "allowAccess" } 或 { "action": "declineAccess" } 时,我没有得到服务器的任何响应