websocket - 返回 django graphene 订阅中订阅事件的初始数据

标签 websocket graphql subscription django-channels graphene-django

我正在尝试回复用户的订阅。例如,在聊天室中,当用户连接到订阅时,订阅会用数据响应他(例如欢迎消息),但仅限于刚刚连接的同一用户(无广播)。

我怎样才能做到这一点? :(

更新:我们决定使用 channel 。 DjangoChannelsGraphqlWs 不允许直接返回消息。

最佳答案

看看this DjangoChannelsGraphQL example 。链接指向避免“用户 self 通知”(避免用户被通知其自己的操作)的部分。您可以使用相同的技巧仅向执行操作的用户发送通知,例如谁刚刚订阅了。

修改后的 publish 处理程序可能如下所示:

def publish(self, info, chatroom=None):
    new_msg_chatroom = self["chatroom"]
    new_msg_text = self["text"]
    new_msg_sender = self["sender"]
    new_msg_is_greetings = self["is_greetings"]

    # Send greetings message only to the user who caused it.
    if new_msg_is_greetings:
        if (
            not info.context.user.is_authenticated
            or new_msg_sender != info.context.user.username
        ):
            return OnNewChatMessage.SKIP

    return OnNewChatMessage(
        chatroom=chatroom, text=new_msg_text, sender=new_msg_sender
    )

我没有测试上面的代码,因此可能存在问题,但我认为它很好地说明了这个想法。

关于websocket - 返回 django graphene 订阅中订阅事件的初始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58460027/

相关文章:

python - Tornadio2 + Socket.io 负载测试

GraphQL:突变运行时订阅不会触发

iOS应用内购买: Managing auto-renewing subscriptions

c# - 使用 Stripe 每月付款

javascript - 如何关闭 SignalR 中的客户端 JavaScript 'Hub' 连接?

c# - 从 WebSocketCollection 获取特定客户端

javascript - RTCPeerConnection 在 Firefox 和 Safari 中不是构造函数

reactjs - 无法查询类型 "allMdx"上的字段 "Query"

javascript - Gatsby : Accessing different sections of a blog post saved in markdown file (GraphQL)

Angular : View not updating when variable changed asynchronously