我使用 nginx 作为 daphne 的反向代理来使用 django 频道。
这是我的 nginx.conf
server {
listen 80;
server_name <ip_addr>;
location / {
proxy_pass http://0.0.0.0:8001;
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;
}
}
在达芙妮的日志中:
2017-07-17 08:45:35,011 DEBUG WebSocket daphne.response.xxx open and established
2017-07-17 08:45:35,011 DEBUG WebSocket daphne.response.xxx accepted by application
但是WebSocket在客户端的连接没有收到任何消息
你能帮助我吗?
更新:
客户端代码(我在 Swift 中使用 Starscream 库进行 WebSocket 连接):
socket = WebSocket(url: URL(string: "ws://site/proxy?token="+token!)!)
func websocketDidConnect(socket: WebSocket) {
print("websocket is connected")
}
socket.connect()
路由.py:
channel_routing = [
route("websocket.receive", 'project.consumers.ws_message'),
route("websocket.connect", 'project.consumers.ws_connect'),
]
消费者.py:
@rest_token_user
def ws_connect(message):
message.reply_channel.send({"accept": True})
res_user 来自:https ://gist.github.com/leonardoo/9574251b3c7eefccd84fc38905110ce4