【发布时间】:2023-03-09 11:29:02
【问题描述】:
我正在尝试在 ubuntu aws ec2 实例中部署我的 django chammels asgi 应用程序,我的 wsgi 应用程序运行良好,ubuntu 上的 asgi 运行良好(使用 python websocket 库测试)代码中没有错误并且 daphne 正在运行完美
代码库没有错误,daphne 也完美运行在 EC2 ubuntu 实例的 localhost 上
server {
listen 80;
server_name MY_SERVER_IP;
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /ws/{
proxy_pass http://0.0.0.0:4001;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
[Unit]
Description=project Daphne Service
After=network.target
[Service]
User=root
Type=simple
WorkingDirectory=/home/ubuntu/myproject
ExecStart=/home/ubuntu/myproject/venv/bin/python /home/ubuntu/myproject/venv/bin/d>
[Install]
WantedBy=multi-user.target
我创建了这个 python 脚本来确定我的 daphne 是否工作,它为我提供了完美的结果,没有错误
import asyncio
import websockets
import json
async def hello():
uri = "ws://127.0.0.1:4001/ws/chat/person/?token=<CHAT_TOKEN>
async with websockets.connect(uri) as websocket:
await websocket.send(json.dumps({"message":"Hey there"}))
g = await websocket.recv()
print(g)
asyncio.get_event_loop().run_until_complete(hello())
以下脚本返回
{
"id": 1,
"message": "Hey There",
"time_stamp": "TIME",
"receiver": {
"username": "John Doe",
"id": 2",
"profile_picture": "LINK"
}
}
当我尝试连接 websocket 时,现在在我的前端应用程序中
socket = new WebSocket("ws://<SERVER_IP>:4001/ws/chat/person/?token=<MY_TOKEN>")
socket.onopen = functon(){
...
}
...
但是下面的脚本返回
WebSocket connection to 'ws://<SERVER_IP>/ws/chat/person/?token=<TOKEN> failed:
【问题讨论】:
标签: django amazon-web-services amazon-ec2 websocket daphne