部署Django Channels应用程序后,在与用户端点进行WebSocket握手期间出错

时间:2017-04-16 16:49:46

标签: django heroku websocket reverse-proxy django-channels

我正在尝试在我的django应用程序中在用户级别设置websocket连接以接收通知。 prod是https所以需要使用wss。

这是js:

$( document ).ready(function() {

socket = new WebSocket("wss://" + window.location.host + "/user_id/");
var $notifications = $('.notifications');
var $notificationList = $('.dropdown-menu.notification-list');

$notifications.click(function(){
    $(this).removeClass('newNotification')
});

socket.onmessage = function(e) {

// notification stuff

// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();

}

我在settings.py中实现了django-channels,如下所示:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts":[ENV_STR("REDIS_URL")]
        },
        "ROUTING": "project.routing.channel_routing",
     },
}

routing.py

from channels.routing import route
from community.consumers import ws_add, ws_disconnect

channel_routing = [
    route("websocket.connect", ws_add),
    route("websocket.disconnect", ws_disconnect),
]
在当地,这握手就好了。

2017-04-16 16:37:04,108 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-04-16 16:37:04,109 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-04-16 16:37:04,109 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-04-16 16:37:04,110 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-04-16 16:37:04,111 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2017-04-16 16:37:04,112 - INFO - server - Using busy-loop synchronous mode on channel layer
2017-04-16 16:37:04,112 - INFO - server - Listening on endpoint tcp:port=8000:interface=127.0.0.1
[2017/04/16 16:37:22] HTTP GET / 200 [0.55, 127.0.0.1:60129]
[2017/04/16 16:37:23] WebSocket HANDSHAKING /user_id/ [127.0.0.1:60136]
[2017/04/16 16:37:23] WebSocket CONNECT /user_id/ [127.0.0.1:60136]
[2017/04/16 16:37:25] HTTP GET /user/test10 200 [0.47, 127.0.0.1:60129]
[2017/04/16 16:37:25] WebSocket DISCONNECT /user_id/ [127.0.0.1:60136]
[2017/04/16 16:37:26] WebSocket HANDSHAKING /user_id/ [127.0.0.1:60153]
[2017/04/16 16:37:26] WebSocket CONNECT /user_id/ [127.0.0.1:60153]

但是,现在应用程序已部署到Heroku,/ _user_id / endpoint返回404,当它应该是有效端点时,我得到ERR_DISALLOWED_URL_SCHEME:

WebSocket connection to 'wss://mydomain.com/user_id/' failed: Error during WebSocket handshake: Unexpected response code: 404.

由于我正在继续研究,似乎这个服务器配置实际上不能支持prod中的websockets。当前Procfile:

web: gunicorn project.wsgi:application --log-file -
worker: python manage.py runworker -v2

我花了几个小时看着将应用程序转换为asgi并使用Daphne,虽然因为项目是python 2.7,这是一个很难转换(更不用说似乎需要不同的静态文件实现)

0 个答案:

没有答案