通道未达到Websocket AsyncJsonWebsocketConsumer断开连接的通道

时间:2018-09-19 07:24:31

标签: django python-3.x django-channels daphne

我有以下消费者:

class ChatConsumer(AsyncJsonWebsocketConsumer):
    pusher = None

    async def connect(self):
        print(self.scope)
        ip = self.scope['client'][0]
        print(ip)
        self.pusher = await self.get_pusher(ip)
        print(self.pusher)
        await self.accept()

    async def disconnect(self, event):
        print("closed connection")
        print("Close code = ", event)
        await self.close()
        raise StopConsumer

    async def receive_json(self, content):
        #print(content)
        if 'categoryfunctionname' in content:
            await cellvoltage(self.pusher, content)
        else:
            print("ERROR: Wrong data packets send")
            print(content)


    @database_sync_to_async
    def get_pusher(self, ip):
        p = Pusher.objects.get(auto_id=1)
        try:
            p = Pusher.objects.get(ip=ip)
        except ObjectDoesNotExist:
            print("no pusher found")
        finally:
            return p

连接,接收甚至从数据库异步获取内容都可以正常工作。仅断开连接无法正常工作。以下终端日志说明了发生的情况:

2018-09-19 07:09:56,653 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-09-19 07:09:56,653 - INFO - server - Configuring endpoint tcp:port=1111:interface=192.168.1.111
2018-09-19 07:09:56,653 - INFO - server - Listening on TCP address 192.168.1.111:1111
[2018/09/19 07:11:25] HTTP GET / 302 [0.02, 10.171.253.112:35236]
[2018/09/19 07:11:25] HTTP GET /login/?next=/ 200 [0.05, 10.111.253.112:35236]
{'type': 'websocket', 'path': '/ws/chat/RP1/', 'headers': [(b'upgrade', b'websocket'), (b'connection', b'Upgrade'), (b'host', b'10.111.111.112:1111'), (b'origin', b'http://10.111.253.112:1111'), (b'sec-websocket-key', b'vKFAnqaRMm84AGUCxbAm3g=='), (b'sec-websocket-version', b'13')], 'query_string': b'', 'client': ['10.111.253.112', 35238], 'server': ['192.168.1.111', 1111], 'subprotocols': [], 'cookies': {}, 'session': <django.utils.functional.LazyObject object at 0x7fe4a8d1ba20>, 'user': <django.utils.functional.LazyObject object at 0x7fe4a8d1b9e8>, 'path_remaining': '', 'url_route': {'args': (), 'kwargs': {'room_name': 'RP1'}}}
10.111.253.112
[2018/09/19 07:11:25] WebSocket HANDSHAKING /ws/chat/RP1/ [10.111.253.112:35238]
[2018/09/19 07:11:25] WebSocket CONNECT /ws/chat/RP1/ [10.111.111.112:35238]
no pusher found
1 - DEFAULT - 0.0.0.0
ERROR: Wrong data packets send
{'hello': 'Did I achieve my objective?'}
[2018/09/19 07:11:46] WebSocket DISCONNECT /ws/chat/RP1/ [10.111.253.112:35238]
2018-09-19 07:11:56,792 - WARNING - server - Application instance <Task pending coro=<SessionMiddlewareInstance.__call__() running at /home/pi/PycharmProjects/LOGGER/venv/lib/python3.6/site-packages/channels/sessions.py:175> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/lib/python3.6/asyncio/futures.py:403, <TaskWakeupMethWrapper object at 0x7fe4a82e6fd8>()]>> for connection <WebSocketProtocol client=['10.171.253.112', 35238] path=b'/ws/chat/RP1/'> took too long to shut down and was killed.

10秒超时后,它会警告连接已终止:

  

警告-服务器-在位置运行的应用程序实例任务名称   在连接cxn-name的行号上花费了太长时间才能关闭,并且   被杀了。

因此也没有达到断开连接的方法。

  • 这可能是什么?
  • 我使用的方法正确吗?
  • 我可以延长超时时间吗?

1 个答案:

答案 0 :(得分:1)

如果您打算在连接关闭期间运行一些自定义逻辑,则应覆盖websocket_disconnect,然后调用super(而不是自己引发异常)

https://github.com/django/channels/blob/507cb54fcb36df63282dd19653ea743036e7d63c/channels/generic/websocket.py#L228-L241