django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.11k stars 800 forks source link

#WebSocket DISCONNECT /ws/server/ [127.0.0.1:56029 #1109

Closed EightyEighth closed 6 years ago

EightyEighth commented 6 years ago

I have a problem with HANDSHAKING consumer.

class WSServerConsumer(JsonWebsocketConsumer):
    START_CMD = 'start'
    STOP_CMD = 'stop'

    def __init__(self, *args, **kwargs):
        super(WSServerConsumer, self).__init__(*args, **kwargs)
        self.active_channels = {}

    def get_channel(self, name):
        channel = self.active_channels.get(name)
        if not channel:
            self.register_channel(name)
        return self.active_channels.get(name)

    def register_channel(self, name):
        self.active_channels[name] = FactoryChannel.get_channel_instance(name)

    def handler_message(self, event):
        channel = self.get_channel(event['channel'])
        if channel.is_open:
            self.send_json(
                {
                    'channel': channel.name,
                    'response': event['result']
                })

    def connect(self):
        print('START')
        self.accept()
        # await self.send('Hello client!')

    def disconnect(self, code):
        print('CODE: %s' % code)
        self.close()

    def receive_json(self, content, *args):
        action = content.get('action')
        channel_name = content.get('channel')
        init = content.get('init')

        channel = self.get_channel(channel_name)

        channel.switch(action)

[2018/07/23 22:41:13] WebSocket CONNECT /ws/server/ [127.0.0.1:56029] [2018/07/23 22:41:14] WebSocket DISCONNECT /ws/server/ [127.0.0.1:56029] [2018/07/23 22:41:16] WebSocket HANDSHAKING /ws/server/ [127.0.0.1:56040] START [2018/07/23 22:41:16] WebSocket CONNECT /ws/server/ [127.0.0.1:56040] [2018/07/23 22:41:16] WebSocket DISCONNECT /ws/server/ [127.0.0.1:56040] [2018/07/23 22:41:18] WebSocket HANDSHAKING /ws/server/ [127.0.0.1:56055] START [2018/07/23 22:41:18] WebSocket CONNECT /ws/server/ [127.0.0.1:56055] [2018/07/23 22:41:19] WebSocket DISCONNECT /ws/server/ [127.0.0.1:56055]

I tried create a websocket server with Twisted and it's works. channels==2.1.2 channels-redis==2.2.1 chardet==3.0.4 constantly==15.1.0 cryptography==2.3 daphne==2.2.1 dj-database-url==0.3.0 dj-email-url==0.1.0 Django==2.0.4 django-configurations==2.0 django-cors-headers==2.1.0 django-debug-toolbar==1.9.1 django-filter==1.1.0 django-filters==0.2.1 django-rest-framework==0.1.0 django-rest-multiple-models==2.0.1 djangorestframework==3.7.7 enum-compat==0.0.2 eventlet==0.21.0 gevent==1.3.5 greenlet==0.4.14 hiredis==0.2.0 hyperlink==18.0.0 idna==2.7 incremental==17.5.0 Jinja2==2.10 kombu==4.1.0 MarkupSafe==1.0 meld3==1.0.2 mock==2.0.0 msgpack==0.5.6 paramiko==2.4.1 path.py==11.0.1 pbr==4.1.0 psycopg2-binary==2.7.4 pyasn1==0.4.3 pycparser==2.18 PyHamcrest==1.9.0 PyNaCl==1.2.1 python-bitcoinrpc==1.0 pytz==2018.5 PyYAML==3.13 redis==2.10.6 requests==2.19.1 six==1.11.0 sqlparse==0.2.4 supervisor==4.0.0.dev0 Twisted==18.7.0 txaio==2.10.0 urllib3==1.23 uWSGI==2.0.17 vine==1.1.4 websocket==0.2.1 websocket-client==0.48.0 websockets==6.0 Werkzeug==0.14.1 wrapt==1.10.11 zope.interface==4.5.0

YangYi215613 commented 2 years ago

hava you solve this problem?