我遵循了本教程: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
提前感谢任何帮助。
答案 0 :(得分:1)
我用同样的回购遇到了这个问题。对我来说问题是频道的版本。我相信在版本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})