django channels websocket connect not working

半城伤御伤魂 提交于 2019-12-23 17:03:19

问题


I am trying to run the multichat project from django channels-examples on a server. It works locally on a windows machine but when I put this on the linux server and start it with runserver it does not:

./manage.py runserver

Then when I call up the website, it is shown correctly but as soon as the JS sends it websocket request I always get this reponse:

[2016/08/02 14:35:48] HTTP GET /chat/stream/ 404 [0.04, 127.0.0.1:40186]
....(many lines of this)

So the websocket request is handled as an http request. The response should be this:

[2016/08/02 16:34:45] WebSocket CONNECT /chat/stream/ [127.0.0.1:60250]

I have no clue where this is going wrong. The routing of http versus websocket seems to be done somewhere deep inside daphne/twisted/...

My channel settings are (if that is of any help at all):

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [(redis_host, 62031)],
        },
        "ROUTING": "multichat.routing.channel_routing",
    },
}

Twisted version is 16.2.0.

Any help or hint in what direction to look is very appreciated.


回答1:


Some questions/suggestions that come to mind:

Is the linux server for development or production? I.e. Is there a HTTP server or just plain DJANGO running? (In the former case you don't start with "./manage runserver".)

"Then when I call up the website, it is shown correctly ..."

Is this because the port numbers different for the the requests? 40186 vs 60250.

You're getting a 404, e.g. url Not Found. This can be either the response of the HTTP server of from DJANGO.




回答2:


Can you post the complete log ?

Try running the server with daphne and run worker

daphne multichat.asgi:channel_layer --port 80 --bind 0.0.0.0 -v2 
python manage.py runworker -v2 

Use something like supervisor to do this in a better way to restart these on failure.



来源:https://stackoverflow.com/questions/38724126/django-channels-websocket-connect-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!