2017-04-06 96 views
4

我在Django上使用Redis上的網絡套接字。 Django的運行MacOS的服務器上很好,但我一開始紅帽Linux服務器上運行它,現在的服務器給了我這個錯誤,每當我過的WebSockets發送一個包:django websockets無法在頻道上發送消息

ERROR - server - HTTP/WS send decode error: 
    Cannot dispatch message on channel 
    u'daphne.response.fzdRCEVZkh!nqhIpaLfWb' (unknown) 

注意:當出現錯誤時,包會被正確接收。

我找不到此錯誤的任何資源。

我跟着official instructions尋找頻道。

回答

3

根據安德魯·戈德溫(頻道包的開發者),當你有一個被斷開的通道此消息被記錄,但不能從信道組已移除:

是啊,這是達芙妮比以前更加冗長,我需要刪除它。不要擔心 - 斷開仍然在一個組中的頻道後,這很正常。不過,您可能希望在斷開連接處理程序中添加Group.discard調用來停止它。

Source.

我有同樣的錯誤,使用channels.generic.websockets.WebsocketConsumer定製IMPL。清除disconnect回叫中組的頻道後,該消息消失。

基於類的使用者的一個簡短示例:假設您將客戶端添加到名爲foo的廣播組,並建立連接。然後,在客戶端斷開連接時,從組中刪除其通道:

from channels import Group 
from channels.generic.websockets import JsonWebsocketConsumer 

class MyConsumer(JsonWebsocketConsumer): 

    groupname = 'foo' 

    def connect(self, message, **kwargs): 
     # send an accept or the connection will be dropped automatically 
     self.message.reply_channel.send({"accept": True}) 
     # add the channel to the broadcast group 
     Group(self.groupname).add(message.reply_channel) 
     # do the rest of logic that should happen on connection established 
     ... 

    def disconnect(self, message, **kwargs): 
     Group(self.groupname).discard(message.reply_channel) 
     # do the rest of logic that should happen on disconnect 
     ...