Arm*_*nMz 1 python django disconnect websocket django-channels
自从我开始使用 django-channels 已经 1 个月了,现在我觉得我没有正确断开 websockets。当我断开连接时,如果没有人在那里,我想完全摧毁该组,并且它应该没有存在的迹象。当我拒绝连接时,我提出 channels.exceptions.DenyConnection 或发送 {'accepted': 'False'} 我只是想知道这是否是我提到的事情的正确方式。
试试打电话 self.close()
class MyConsumer(WebsocketConsumer):
def connect(self):
# Called on connection.
# To accept the connection call:
self.accept()
# Or accept the connection and specify a chosen subprotocol.
# A list of subprotocols specified by the connecting client
# will be available in self.scope['subprotocols']
self.accept("subprotocol")
# To reject the connection, call:
self.close()
Run Code Online (Sandbox Code Playgroud)
据我了解,关闭组的方法是使用 group_discard。
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)("yourgroupname", self.channel_name)
Run Code Online (Sandbox Code Playgroud)
如果没有对此进行测试,我会假设引发异常会导致客户端出现错误 500。收到错误的客户端可能会将其解释为“正常关闭”。
请参阅此处的频道文档:https ://channels.readthedocs.io/en/latest/topics/channel_layers.html#groups
| 归档时间: |
|
| 查看次数: |
2672 次 |
| 最近记录: |