0

我正在开发我的第一个 django 频道应用程序,我想在 Google Cloud Platform 中部署我的 django 频道应用程序,它在本地服务器上工作正常,但是当我在 Google Cloud Platform 上部署它时,它给了我错误:

WebSocket 连接到“wss://sockets-263709.appspot.com/ws/chat/user2/”失败:WebSocket 握手期间出错:意外响应代码:400

我已经研究过但无法弄清楚如何设置它,这是我的以下代码:

入口点

from Frames.wsgi import application
app = application

wsgi.py

import os

from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Frames.settings')
application = get_wsgi_application()

Asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Frames.settings')
django.setup()
application = get_default_application()

设置.py

WSGI_APPLICATION = 'Frames.wsgi.application'
ASGI_APPLICATION = "Frames.routing.application"

CHANNEL_LAYERS={
    "default":{
      "BACKEND": "channels_redis.core.RedisChannelLayer",
      "CONFIG": {
                "hosts": [("sockets-263709.appspot.com", 6379)],
            },
    },
}

客户端

var wsStart='ws://';
    // check if it is secured then assign wss://
    if (loc.protocol==='https:'){
        wsStart="wss://"
    }

    var chatSocket = new WebSocket(wsStart +window.location.host +
        '/ws/chat/' + roomName + '/');

要求.txt

absl-py==0.8.1
aioredis==1.3.1
asgiref==3.2.3
astor==0.8.1
async-timeout==3.0.1
attrs==19.3.0
autobahn==19.11.1
Automat==0.8.0
certifi==2019.11.28
cffi==1.13.2
channels==2.3.1
channels-redis==2.4.1
chardet==3.0.4
constantly==15.1.0
daphne==2.4.0
Django==3.0.1
djangochannelsrestframework==0.0.3
djangorestframework==3.11.0
gast==0.3.2
google-pasta==0.1.8
grpcio==1.25.0
h5py==2.10.0
hiredis==1.0.1
hyperlink==19.0.0
idna==2.8
incremental==17.5.0
Markdown==3.1.1
msgpack==0.6.2
mysqlclient==1.4.6
protobuf==3.11.1
pyasn1==0.4.8
pyasn1-modules==0.2.7
pycparser==2.19
PyHamcrest==1.9.0
PyMySQL==0.9.3
pyOpenSSL==19.1.0
pytz==2019.3
PyYAML==5.2
requests==2.22.0
scipy==1.3.3
service-identity==18.1.0
six==1.13.0
sqlparse==0.3.0
termcolor==1.1.0
tqdm==4.40.2
Twisted==19.10.0
txaio==18.8.1
urllib3==1.25.7

我有一个从 WSGI.py 开始的起点,我研究并了解了 daphne 但不知道,谁能指出我如何正确部署它。连接在本地成功打开。主机是否在 settings.py 的 config(CHANNEL_LAYERS) 中正确提及

4

0 回答 0