我正在尝试响应订阅上的用户。例如,在聊天室中,当用户连接到subscription时,subscription会用数据(如欢迎消息)响应他,但只对刚连接的同一用户进行响应(无广播)。
我该怎么做呢?
更新:我们决定使用通道。DjangoChannelsGraphqlWs不允许直接回传消息。
发布于 2020-04-05 20:13:41
看看this DjangoChannelsGraphQL example吧。链接指向要避免“用户自我通知”的部分(避免通知用户自己的操作)。您可以使用相同的技巧仅向执行操作的用户发送通知,例如,刚刚订阅的用户。
修改后的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
)我没有测试上面的代码,所以可能会有问题,但我认为它很好地说明了这个想法。
https://stackoverflow.com/questions/58460027
复制相似问题