簡體   English   中英

在python中向特定客戶端connectionId Websocket API AWS發送消息?

[英]Send message to specific client connectionId Websocket API AWS in python?

我正在嘗試使用 API Gateway 中的 AWS Lambda + Websocket API 構建一個實時聊天應用程序。

我想要做的是將每個用戶的connectionId保存在我的數據庫中,以便能夠在 2 個用戶處於同一對話中時向特定客戶端發送消息。

如何從 python 中的lambda函數指定connectionId的目標?

這實際上應該是一個connectionId列表,要發送給我在應用程序當前對話中與之交談的每個人。

偽代碼看起來像(在lambda內部):


def create_message(message, conversation):
    save_message_in_db(message)
    ids_that_should_receive_message = conversation["participantsID"]
    return {"body": json.dumps({"message": message}), "connectionId": ids_that_should_receive_message}

提前致謝

解決方案是使用APIGatewayManagementAPI並使用采用特定ConnectionId參數的post_to_connection方法將消息發送到特定客戶端:

post_to_connection

是我找到的 boto3 文檔的鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM