Heroku托管上的Django频道

时间:2018-11-13 07:35:26

标签: python django django-channels

由于我不能使用redis,所以我使用了另一个通道层 InMemoryChannelLayer

WSGI_APPLICATION = 'django_analytics.wsgi.application'
ASGI_APPLICATION = "django_analytics.routings.application"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer",
        "CONFIG": {
        },
    },
}

我在heroku上部署了它,但出现错误

  

(索引):9 WebSocket连接到   'wss://mkmnim.herokuapp.com/ws/chat/page/'失败:发生错误   WebSocket握手:意外的响应代码:404

我可以在本地服务器上运行它,但是不能在线托管它,我还必须做一些与达芙妮有关的事情吗? 最初,我使用redis并在本地运行redis服务器,但是在托管redis安装程序时已付款,因此我选择InMemoryChannelLayer (channels without channel layer or any other free hosting

那我该怎么办?

django_analytics.routings.py

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from view_analytics import routing

application = ProtocolTypeRouter({
    # Empty for now (http->django views is added by default)
    'websocket': AuthMiddlewareStack(
        URLRouter(
            routing.websocket_urlpatterns
        )
    ),
})

view_analytics.routings.py

from django.conf.urls import url

from . import consumers

websocket_urlpatterns = [
    url(r'^ws/chat/room/$', consumers.ChatConsumer),
    url(r'^ws/chat/page/$', consumers.PageConsumer),

]

page.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    let chatSocket = new WebSocket(
        'wss://' + window.location.host +
        '/ws/chat/' + 'page' + '/');

    chatSocket.onmessage = function(e) {
        const data = JSON.parse(e.data);
        const message = data['message'];
        console.log("message is"+ message)
    };

    chatSocket.onclose = function(e) {
        console.error('Chat socket closed unexpectedly');
    };
</script>

</body>
</html>

编辑1: 还添加了heroku日志

  

2018-11-13T08:12:14.240908 + 00:00 heroku [router]:at = info method = GET   path =“ / view_analytics / page_check /” host = mkmnim.herokuapp.com   request_id = 93802d36-fece-47b0-9e91-7f453d891f00 fwd =“ 47.31.181.82”   dyno = web.1 connect = 1ms服务= 60ms status = 200字节= 726   protocol = https 2018-11-13T08:12:14.240275 + 00:00 app [web.1]:   10.41.198.116--[13 / Nov / 2018:08:12:14 +0000]“ GET / view_analytics / page_check / HTTP / 1.1” 200536“-”“ Mozilla / 5.0(X11;   Linux x86_64)AppleWebKit / 537.36(KHTML,例如Gecko)   Chrome / 69.0.3497.81 Safari / 537.36“ 2018-11-13T08:12:15.342043 + 00:00   heroku [路由器]:at =信息方法=获取路径=“ / ws / chat / page /”   host = mkmnim.herokuapp.com   request_id = c2f225eb-1f88-4417-87fe-281b6dd6fd51 fwd =“ 47.31.181.82”   dyno = web.1 connect = 1ms服务= 7ms status = 404字节= 2534   protocol = https 2018-11-13T08:12:15.340615 + 00:00 app [web.1]:找不到:   / ws / chat / page / 2018-11-13T08:12:15.341106 + 00:00 app [web.1]:   10.168.81.123--[13 / Nov / 2018:08:12:15 +0000]“ GET / ws / chat / page / HTTP / 1.1” 404 2351“-”“ Mozilla / 5.0(X11; Linux x86_64)   AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 69.0.3497.81   Safari / 537.36“

0 个答案:

没有答案