您可以从非websocket lambda向websocket客户端发送消息吗?

编程入门 行业动态 更新时间:2024-10-11 23:27:26
本文介绍了您可以从非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:

开机自检 {api-id} .execute-api.us-east-1.amazonaws/{stage}/@ connections/{connection_id}

POST {api-id}.execute-api.us-east-1.amazonaws/{stage}/@connections/{connection_id}

https ://docs.aws.amazon/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html

更多推荐

您可以从非websocket lambda向websocket客户端发送消息吗?

本文发布于:2023-11-25 23:20:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631750.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:您可以   客户端   发送消息   lambda   websocket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!