Websocket 连接后立即断开连接(重复)

Websocket disconnects immediately after connecting (repeatedly)

我遵循了这个教程:https://github.com/jacobian/channels-example

然后我得到了标题中的错误,然后我克隆了上面的存储库并得到了同样的错误。

(如果您尝试克隆存储库进行测试,请记住更改 settings.py 中的数据库设置,迁移数据库,然后进入视图并将 import haikunator 更改为 from haikunator import Haikunator 并在 new_room 函数中将行 haikunator = Haikunator() 添加到函数的开头,以便它可以工作)

我正在使用 Python 2.7 和 Django 1.10。 '

回溯:

System check identified no issues (0 silenced).
July 06, 2017 - 13:22:20
Django version 1.10, using settings 'chat.settings'
Starting Channels development server at http://127.0.0.1:8000/
Channel layer default (asgi_redis.core.RedisChannelLayer)
Quit the server with CTRL-BREAK.
2017-07-06 13:22:20,969 - INFO - worker - Listening on channels http.request, websocket
.connect, websocket.disconnect, websocket.receive
2017-07-06 13:22:20,971 - INFO - worker - Listening on channels http.request, websocket
.connect, websocket.disconnect, websocket.receive
2017-07-06 13:22:20,973 - INFO - worker - Listening on channels http.request, websocket
.connect, websocket.disconnect, websocket.receive
2017-07-06 13:22:20,977 - INFO - worker - Listening on channels http.request, websocket
.connect, websocket.disconnect, websocket.receive
2017-07-06 13:22:20,986 - INFO - server - HTTP/2 support not enabled (install the http2
 and tls Twisted extras)
2017-07-06 13:22:20,992 - INFO - server - Using busy-loop synchronous mode on channel l
ayer
2017-07-06 13:22:20,993 - INFO - server - Listening on endpoint tcp:port=8000:interface
=127.0.0.1
[2017/07/06 13:22:36] HTTP GET /floral-water-4056/ 200 [0.21, 127.0.0.1:23450]
[2017/07/06 13:22:37] HTTP GET /static/normalize.css 304 [0.12, 127.0.0.1:23450]
[2017/07/06 13:22:37] HTTP GET /static/chat.css 304 [0.13, 127.0.0.1:23452]
[2017/07/06 13:22:37] HTTP GET /static/skeleton.css 304 [0.13, 127.0.0.1:23451]
[2017/07/06 13:22:37] HTTP GET /static/jquery-1.12.1.min.js 304 [0.11, 127.0.0.1:23453]

[2017/07/06 13:22:37] HTTP GET /static/reconnecting-websocket.min.js 304 [0.10, 127.0.0
.1:23456]
[2017/07/06 13:22:37] HTTP GET /static/chat.js 304 [0.11, 127.0.0.1:23457]
[2017/07/06 13:22:37] chat connect room=floral-water-4056 client=127.0.0.1:23462
[2017/07/06 13:22:39] WebSocket DISCONNECT /chat/floral-water-4056/ [127.0.0.1:23462]
[2017/07/06 13:22:40] chat connect room=floral-water-4056 client=127.0.0.1:23465
[2017/07/06 13:22:42] WebSocket DISCONNECT /chat/floral-water-4056/ [127.0.0.1:23465]
[2017/07/06 13:22:44] chat connect room=floral-water-4056 client=127.0.0.1:23475
[2017/07/06 13:22:47] WebSocket DISCONNECT /chat/floral-water-4056/ [127.0.0.1:23475]
[2017/07/06 13:22:50] chat connect room=floral-water-4056 client=127.0.0.1:23479
[2017/07/06 13:22:53] WebSocket DISCONNECT /chat/floral-water-4056/ [127.0.0.1:23479]
[2017/07/06 13:22:57] chat connect room=floral-water-4056 client=127.0.0.1:23482
[2017/07/06 13:23:00] WebSocket DISCONNECT /chat/floral-water-4056/ [127.0.0.1:23482]
[2017/07/06 13:23:06] chat connect room=floral-water-4056 client=127.0.0.1:23489
[2017/07/06 13:23:08] WebSocket DISCONNECT /chat/floral-water-4056/ [127.0.0.1:23489]

https://github.com/jacobian/channels-example

感谢任何帮助,提前致谢。

我 运行 使用相同的 repo 解决了这个问题。对我来说问题是频道的版本。我相信在 1.10 版本之后(版本可能有误),您现在必须将消息发回 reply_channel。

message.reply_channel.send({'accept':True})

这不在他的代码中。可能是在更改频道之前写的。添加后将停止断开连接。 请参阅此处的文档: Channels Documentation

看看下面最后一行函数:

def ws_connect(message):
# Extract the room from the message. This expects message.path to be of the
# form /chat/{label}/, and finds a Room if the message path is applicable,
# and if the Room exists. Otherwise, bails (meaning this is a some othersort
# of websocket). So, this is effectively a version of _get_object_or_404.
    try:
        prefix, label = message['path'].strip('/').split('/')
        #log.debug('prefix=%s label=%s', prefix, label)
        if prefix != 'chat':
            log.debug('invalid ws path=%s', message['path'])
            return
        room = Room.objects.get(label=label)
    except ValueError:
        log.debug('invalid ws path=%s', message['path'])
        return
    except Room.DoesNotExist:
        log.debug('ws room does not exist label=%s', label)
        return

    log.debug('chat connect room=%s client=%s:%s', 
        room.label, message['client'][0], message['client'][1])


    Group('chat-'+label, channel_layer=message.channel_layer).add(message.reply_channel)

    message.channel_session['room'] = room.label

    message.reply_channel.send({'accept':True})