开发者社区> 问答> 正文

使用django频道向其他用户/组发送消息

我试图发送通知给所有其他连接的用户除了我自己(或连接的用户)。我有三个独立的用户帐户,我希望发送不同的消息,每个用户取决于帐户类型。“帐户类型1”、“类型2”和“无授权用户”。 所有用户都位于不同的域中,并通过不同的路径连接到web套接字。我希望能够让每个用户都知道对方何时加入了连接/页面。但是,它目前正在为已连接的用户显示消息。如。 当我以“帐户1”的身份访问该页面时,我得到“帐户1已加入该组”。但是,我不想看到其他连接的用户何时加入。 我可能忽略了一些简单的东西,所以想要一些建议。如果我尝试创建一个基于用户ID的组,那么我只能看到我正在访问页面的帐户类型发送的消息。 我有一个'booking'对象,我使用ID来创建房间。然后将用户添加到连接后的组中,并向组发送消息。传入消息后用jquery显示消息。

 async def connect (self):

        Join room group and
        announce connection.


    account = self.scope["user"]

    self.booking_id = self.scope['url_route']['kwargs']['booking_id']
    #self.user_id = self.scope['url_route']['kwargs']['user_id']

    self.room_group_name = 'booking_%(booking_id)s' % { 
        'booking_id': self.booking_id,
    }


    await self.channel_layer.group_add (
        self.room_group_name,
        self.channel_name,
    )

    account_type = 'Account 1' ## Get account type from scope?

    await self.channel_layer.group_send (
        self.room_group_name,
        {
            'type': 'booking_message',
            'message': {
                # 'display_to': 'all',
                'level': 'success',
                'account': str(account),
                'account_type': str(account_type),
                'message': _ ('Welcome to the page!'),
                'join_message': _('{account_type} has joined the booking.'.format (account_type = account_type)),
            },
        },
    )
    await self.accept ()

问题来源StackOverflow 地址:/questions/59378329/send-message-to-other-users-groups-with-django-channels

展开
收起
kun坤 2019-12-30 10:41:58 1248 0
0 条回答
写回答
取消 提交回答
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载