django-channels / websockets:WebSocketBadStatusException:握手状态200

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django-channels / websockets:WebSocketBadStatusException:握手状态200相关的知识,希望对你有一定的参考价值。

我正在学习本教程:https://gearheart.io/blog/creating-a-chat-with-django-channels/

我在运行manage.py runserver时遇到以下代码时出现此错误

#settings.py

redis_host = os.environ.get('REDIS_HOST', 'localhost')

# Channel layer definitions
# http://channels.readthedocs.org/en/latest/deploying.html#setting-up-a-channel-backend
CHANNEL_LAYERS = {
    "default": {
        # This example app uses the Redis channel layer implementation asgi_redis
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [(redis_host, 6379)],
        },
       "ROUTING": "gameapollius.routing.channel_routing", # We will create it in a moment
    },
}

#routing.py

from channels import route,include

def message_handler(message):
    print(message['text'])

channel_routing = [
    route('websocket.receive', message_handler)
]

#traceback

In [1]: import websocket

In [2]: ws = websocket.WebSocket()

In [3]: ws.connect("ws://localhost:8000")
---------------------------------------------------------------------------
WebSocketBadStatusException               Traceback (most recent call last)
<ipython-input-3-43b98f503495> in <module>()
----> 1 ws.connect("ws://localhost:8000")

c:Python27libsite-packageswebsocket\_core.pyc in connect(self, url, **options)
212
213         try:
--> 214             self.handshake_response = handshake(self.sock, *addrs, **options)
215             self.connected = True
216         except:

c:Python27libsite-packageswebsocket\_handshake.pyc in handshake(sock, hostname, port, resource, **options)
 67     dump("request header", header_str)
 68
---> 69     status, resp = _get_resp_headers(sock)
 70     success, subproto = _validate(resp, key, options.get("subprotocols"))
 71     if not success:

c:Python27libsite-packageswebsocket\_handshake.pyc in _get_resp_headers(sock, success_status)
127     status, resp_headers = read_headers(sock)
128     if status != success_status:
--> 129         raise WebSocketBadStatusException("Handshake status %d", status)
130     return status, resp_headers
131

WebSocketBadStatusException: Handshake status 200

我究竟做错了什么?我已经按照教程到T直到“ws.connect(”ws:// localhost:8000“)”行。这是我被困的地方。任何帮助将不胜感激,提前谢谢。

答案

我的错误是我忘了在我安装的应用中添加“频道”

以上是关于django-channels / websockets:WebSocketBadStatusException:握手状态200的主要内容,如果未能解决你的问题,请参考以下文章

在真实服务器中部署 django-channels 的最佳方式

在负载均衡器后面运行多个 daphne 实例:django-channels

使用 Apache2 和 Daphne 部署 django-channels

Django-Channels 中的类型错误

通过 celery 向 django-channels 发送消息

Django-Channels:在课堂上锁定关键部分