WebSocket可在开发服务器中使用,但不能在Google云中使用。 官方文档在Flask中提供了一个WebSocket示例。但是我没有在Django得到一个。
我认为错误的原因是app.yaml中的 entrypoint:变量。
app.yaml
# [START runtime]
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT myproject.wsgi:application
beta_settings:
cloud_sql_instances: project-id:region:instance-name
runtime_config:
python_version: 3
# [END runtime]
settings.py
ASGI_APPLICATION = "myproject.routing.application"
# channels
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
# "hosts": [("localhost", 6379)],
"hosts": [("redis-ip", port)]
},
"ROUTING": "myproject.routing.application",
},
}
routing.py
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from app.consumers import NoseyConsumer
application = ProtocolTypeRouter({
"websocket": URLRouter([
path("path/", NoseyConsumer),
])
})
在Google Cloud中会引发错误:
“失败:WebSocket握手时出错:意外的响应代码:404”。