2017-07-06 53 views
0

我跟着這個教程:https://github.com/jacobian/channels-example的WebSocket連接(反覆)後立即斷開

我的稱號得到了錯誤,那麼我上面克隆庫,並得到了同樣的錯誤。

(如果你想克隆庫進行測試,記得要更改數據庫設置settings.py中,遷移數據庫,並進入意見和修改import haikunatorfrom 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

任何幫助表示讚賞,謝謝提前。

回答

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}) 
+0

這裏還指出:https://stackoverflow.com/questions/42615905/django-channels-websockets-connecting - 斷開 - 立即脫離 – luskbo

+0

這個問題在Chrome中仍然存在(即使接受:真正的json被返回)... Safari和Firefox的工作。 –