Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django通道-内部自定义路由不工作_Python_Django_Websocket_Channels_Django Channels - Fatal编程技术网

Python Django通道-内部自定义路由不工作

Python Django通道-内部自定义路由不工作,python,django,websocket,channels,django-channels,Python,Django,Websocket,Channels,Django Channels,我正在基于这个示例()构建一个聊天应用程序。 当javascript运行时(套接字已经打开,ws\u connect已经执行),它通过websocket向发送JSON。此'message'路由到ws\u receive,然后将JSON加载到一个“payload”变量中。“消息”回复信道被添加到有效负载变量(dict)中。然后使用Channels命令,有效负载被发送并路由到chat\u join,在那里它只需执行硬编码的message.reply\u channel.send 所有步骤直到 pay

我正在基于这个示例()构建一个聊天应用程序。 当javascript运行时(套接字已经打开,ws\u connect已经执行),它通过websocket向发送JSON。此'message'路由到ws\u receive,然后将JSON加载到一个“payload”变量中。“消息”回复信道被添加到有效负载变量(dict)中。然后使用Channels命令,有效负载被发送并路由到chat\u join,在那里它只需执行硬编码的message.reply\u channel.send

所有步骤直到

payload['reply_channel'] = message.content['reply_channel']
干得好。但是负载没有被路由到聊天室加入消费者。如果它被正确路由,则应答信道值没有被正确读取,因此消息没有被发送回客户端

似乎找不到这里的断点。需要帮助修复此代码。

.js

//Join Room
socket.send(JSON.stringify({
    "command": "join",
    "room": "102"
}));
routing.py

from channels.routing import route
from MyProject.consumers import ws_connect, ws_receive, chat_join

websocket_routing = [
    route("websocket.connect", ws_connect),
    route("websocket.receive", ws_receive),    
]

custom_routing = [
    route("chat.receive", chat_join, command=r'^join$'),
]
from channels import Channel
def ws_receive(message):
    payload = json.loads(message['text'])
    payload['reply_channel'] = message.content['reply_channel']
    Channel("chat.receive").send(payload)

def chat_join(message):
    message.reply_channel.send({
        "text": json.dumps({
            "alpha": "1",
            "beta": "2",
        }),
    })
consumers.py

from channels.routing import route
from MyProject.consumers import ws_connect, ws_receive, chat_join

websocket_routing = [
    route("websocket.connect", ws_connect),
    route("websocket.receive", ws_receive),    
]

custom_routing = [
    route("chat.receive", chat_join, command=r'^join$'),
]
from channels import Channel
def ws_receive(message):
    payload = json.loads(message['text'])
    payload['reply_channel'] = message.content['reply_channel']
    Channel("chat.receive").send(payload)

def chat_join(message):
    message.reply_channel.send({
        "text": json.dumps({
            "alpha": "1",
            "beta": "2",
        }),
    })

Javascript文件应使用
“text”
键发送数据


来源:

我试图将routing.py更改为如下所示。我将自定义路由作为普通路由包含在websocket_路由中,它开始为我工作。希望这有帮助

websocket_routing = [
    # Called when WebSockets connect
    route("websocket.connect", ws_connect),

    # Called when WebSockets get sent a data frame
    route("websocket.receive", ws_receive), 

    # Called when WebSockets disconnect
    route("websocket.disconnect", ws_disconnect),    


    #Custom Routing
    #Join Chat
    route("chat.receive", chat_join, command=r'^join$'),
    route("chat.receive", chat_send, command=r'^send$'),
]

我也有同样的问题。单击“房间名称”按钮时不会发生任何情况。