您可以从非websocket lambda向websocket客户端发送消息吗?
我已经设置了一个nodejs websocket AWS lambda端点(Api网关),它可以连接并可以回显消息.在初始连接期间,我将端点和connection_id保存到数据库中.可以节省下来.如果我打开浏览器客户端并连接到websocket端点,则可以成功连接,并可以从浏览器成功发送消息-我有代码可以将消息回显,并且可以正常工作.
I have a nodejs websocket AWS lambda endpoint (Api Gateway) set up and it connects and can echo messages back. During initial connection, I save the endpoint and connection_id to a database. That gets saved just fine. If I open a browser client, and connect to the websocket endpoint, I can connect successfully, and send a message from the browser successfully - I have code to echo the message back, and it works.
现在,在另一个提供REST端点的nodejs lambda中,我有代码从数据库中加载connection_id并执行以下操作:
Now, in another nodejs lambda, one that provides a REST endpoint, I have code that loads the connection_id from the database and does this:
// 'connection' is loaded successfully from DB (I log it and see the right values)
let api = new AWSSDK.ApiGatewayManagementApi({apiVersion: '2018-11-29', endpoint: connection.endpoint});
await api.postToConnection({
ConnectionId: connection.connection_id,
Data: JSON.stringify({ message: 'Hello World' })
}).promise();
但是,REST端点中的代码(上面的代码)在postToConnection中始终出现410错误.我知道该连接仍处于活动状态,因为可以在测试上面的REST API之前连接到该连接并在浏览器客户端中对其进行ping操作.
However, the code in the REST endpoint (code above) always gets a 410 error in the postToConnection. I know the connection is still active, since I can connect to it and ping it in a browser client just prior to testing the REST API above.
是否可以从非websocket lambda发布到websocket连接?
Is it not possible to post to a websocket connection from a non-websocket lambda?
我认为您应该使用它来将消息从后端发送到您的客户端.
I think you should use this to send messages from the backend to your clients.
要向客户端发送回叫消息,请使用:
To send a callback message to the client, use:
开机自检 https://{api-id} .execute-api.us-east-1.amazonaws.com/{stage}/@ connections/{connection_id}
POST https://{api-id}.execute-api.us-east-1.amazonaws.com/{stage}/@connections/{connection_id}