意外的响应代码:尝试连接Django频道时,javascript控制台出现404错误

时间:2017-03-20 13:21:57

标签: javascript python django

我正在尝试在以下路径中路由websocket。

matrix.routing -> ws_consumer.routing -> chat.routing.

申请结构

/matrix ->
    /matrix
    /wsconsumer
    /chat

matrix是我的应用名称,其他两个(ws_consumerchat)是应用。 这些文件中的配置如下

matrix.routing.py

ws_consumer_regex = r'^/ws_consumer'

channel_routing = [
    include('ws_consumer.routing', path=ws_consumer_regex),
]

ws_consumer.routing.py

chat_regex = r'^/chat'
notification_regex = r'^/notification'

channel_routing = [
    include('chat.routing', path=chat_regex),
    include('notification.routing', path=notification_regex),
]

chat.routing.py

group_chat_name_regex = r'^/(?P<group_name>[a-zA-Z0-9_]+)/$'

group_chat_routing = [
    route('websocket.connect', consumers.group_chat_connect, path=group_chat_name_regex),
    route('websocket.receive', consumers.group_chat_receive, path=group_chat_name_regex),
    route('websocket.disconnect', consumers.group_chat_disconnect, path=group_chat_name_regex),
]


group_chat_global_regex = r'^/group'

channel_routing = [
    include(group_chat_routing, path=group_chat_global_regex),
]

client.html

var socket = new WebSocket('ws://' + window.location.host + '/ws_consumer/chat/group/test/');
socket.onopen = function open() {...};

socket.onmessage = function (event) {...};

if (socket.readyState == WebSocket.OPEN) {
socket.onopen();
}

在这些配置下,服务器正在启动时没有任何问题。但是在控制台中我遇到了错误。

javascript控制台

VM2306:35 WebSocket connection to 'ws://localhost:8000/ws_consumer/chat/group/test/' failed: Error during WebSocket handshake: Unexpected response code: 404

python console

Not Found: /ws_consumer/chat/group/test/
[19/Mar/2017 23:42:39] "GET /ws_consumer/chat/group/test/ HTTP/1.1" 404 2202

我还尝试直接路由到matrix.routing.py中的消费者,这会让我遇到同样的错误。

matrix.routing.py

from chat.consumers import group_chat_connect #consumer method
channel_routing = [
    route('websocket.connect', group_chat_connect)
]

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我发现该问题不包括对设置的INSTALLED_APPS的“频道”。