Daphne 服务器无法连接 HTTPS 上的 websockets
Daphne server cannot connect with websockets on HTTPS
我正在 Openshift 云上部署一个 Django 项目。该项目使用 channels 和 Websockets 使其异步工作。问题是我无法成功地将浏览器的 websockets 连接到服务器端 运行ning 的 Daphne 服务器。
我正在使用 django (python2.7) 和 redis 墨盒制作它 运行.
我正在使用的 post_deploy 脚本如下所示:
...
python manage.py runworker -v2 && daphne myapp.asgi:channel_layer -p 8443 -b $OPENSHIFT_REDIS_HOST -v2
...
这是我的 Django 配置。在 settings.py 中:
...
ALLOWED_HOSTS = [
socket.gethostname(),
os.environ.get('OPENSHIFT_APP_DNS'),
]
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [("redis://:{}@{}:{}/0").format(
OPENSHIFT_REDIS_PASSWORD,
OPENSHIFT_REDIS_HOST,
OPENSHIFT_REDIS_PORT
)],
},
"ROUTING": "myapp.routing.channel_routing",
},
}
...
在routing.py:
...
ws_routing = [
routing.route("websocket.connect", ws_connect),
routing.route("websocket.receive", ws_receive),
routing.route("websocket.disconnect", ws_disconnect),
]
channel_routing = [
include(ws_routing, path=r"^/sync"),
]
...
在consumers.py;
def ws_connect(message):
Group("notifications").add(message.reply_channel)
def ws_disconnect(message):
Group("notifications").discard(message.reply_channel)
def ws_receive(message):
print "Receiving: '%s' from %s" % (message.content['text'], message.content['reply_channel'])
在客户端,我运行宁此代码:
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var path = ws_scheme+'://'+window.location.host + ':8443/sync';
var ws = new WebSocket(path);
ws.onmessage = function(message) {
console.log(message.data);
}
ws.onopen = function() {
this.send('WS Connecting to receive updates!');
}
请注意,由于 this 文档,我在 Daphne 设置和 WebSockets 设置中使用端口 8443。此外,Daphne 绑定到 OPENSHIFT_HOST 地址,因为在 Openshift 中无法将其绑定到 0.0.0.0(权限问题)
输出如下所示:
客户端一切正常,但如果你还记得,在 consumers.py 我有这个:
def ws_receive(message):
print "Receiving: '%s' from %s" % (message.content['text'], message.content['reply_channel'])
所以在我的终端中,服务器应该打印出如下内容:"Receiving: from " 但事实并非如此。我在这里缺少什么?
tl;dr:客户端 websocket 看起来连接正确,但服务器没有打印出消息来确认它。
我成功了。这个问题似乎与端口转发有关,这让我无法通过 openshift cloud 上的 apache 服务器将 websockets 连接到我的 daphne 服务器。
解决这个问题:
1) 使用 django 项目的默认墨盒,我无法修改 apache conf 文件,甚至无法更新 apache 以安装 mod_proxy_wstunnel 来支持 websockets,所以我决定更改它。
此外,mod_proxy_wstunnel 适用于 apache 2.4,但默认墨盒使用 2.2。
频道文档,推荐使用nginx。所以我找到了一个 cartridge 允许我将它与 uwsgi 和 django 一起使用。
我遵循了那个 repo 中的说明,但在推送我的代码之前,我稍微调整了操作挂钩以获取这些包的最新版本,并将示例 django 项目替换为我的。我对 requirements.txt.
做了同样的事情
2)推完之后,我加了redis墨盒。
3) 然后我继续调整墨盒作为模板提供的 uwsgi.yaml 和 nginx.conf 以设置正确的值:
uwsgi.yaml
uwsgi:
socket: $OPENSHIFT_DIY_IP:15005
pidfile: $OPENSHIFT_TMP_DIR/uwsgi.pid
pythonpath: $OPENSHIFT_REPO_DIR/$APP_DIR
module: $APP_NAME.wsgi:application
virtualenv: $OPENSHIFT_DATA_DIR/virtualenv
nginx.conf
...
http {
...
server {
listen $OPENSHIFT_DIY_IP:$OPENSHIFT_DIY_PORT;
server_name localhost;
set_real_ip_from $OPENSHIFT_DIY_IP;
real_ip_header X-Forwarded-For;
location / {
uwsgi_pass $OPENSHIFT_DIY_IP:15005;
include uwsgi_params;
}
location /sync {
proxy_pass http://$OPENSHIFT_DIY_IP:8443;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
...
}
}
在我的 post_deploy 脚本中,我有以下内容:
...
python manage.py runworker -v2 &
daphne myapp.asgi:channel_layer -p 8443 -b $OPENSHIFT_DIY_IP -v2 &
...
所以 daphne 正在监听 $OPENSHIFT_DIY_IP:8443,当 nginx 收到来自 websockets 的请求时,如下所示:
var path = 'wss://'+window.location.host + ':8443/sync';
var ws = new WebSocket(path);
ws.onmessage = function(message) {
alert(message.data);
}
ws.onopen = function() {
this.send('WS Connecting to receive updates!');
}
现在我可以看到:
在浏览器中:
所以我知道这是有效的。我希望这可以帮助除我之外的其他人。
我正在 Openshift 云上部署一个 Django 项目。该项目使用 channels 和 Websockets 使其异步工作。问题是我无法成功地将浏览器的 websockets 连接到服务器端 运行ning 的 Daphne 服务器。
我正在使用 django (python2.7) 和 redis 墨盒制作它 运行.
我正在使用的 post_deploy 脚本如下所示:
...
python manage.py runworker -v2 && daphne myapp.asgi:channel_layer -p 8443 -b $OPENSHIFT_REDIS_HOST -v2
...
这是我的 Django 配置。在 settings.py 中:
...
ALLOWED_HOSTS = [
socket.gethostname(),
os.environ.get('OPENSHIFT_APP_DNS'),
]
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [("redis://:{}@{}:{}/0").format(
OPENSHIFT_REDIS_PASSWORD,
OPENSHIFT_REDIS_HOST,
OPENSHIFT_REDIS_PORT
)],
},
"ROUTING": "myapp.routing.channel_routing",
},
}
...
在routing.py:
...
ws_routing = [
routing.route("websocket.connect", ws_connect),
routing.route("websocket.receive", ws_receive),
routing.route("websocket.disconnect", ws_disconnect),
]
channel_routing = [
include(ws_routing, path=r"^/sync"),
]
...
在consumers.py;
def ws_connect(message):
Group("notifications").add(message.reply_channel)
def ws_disconnect(message):
Group("notifications").discard(message.reply_channel)
def ws_receive(message):
print "Receiving: '%s' from %s" % (message.content['text'], message.content['reply_channel'])
在客户端,我运行宁此代码:
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var path = ws_scheme+'://'+window.location.host + ':8443/sync';
var ws = new WebSocket(path);
ws.onmessage = function(message) {
console.log(message.data);
}
ws.onopen = function() {
this.send('WS Connecting to receive updates!');
}
请注意,由于 this 文档,我在 Daphne 设置和 WebSockets 设置中使用端口 8443。此外,Daphne 绑定到 OPENSHIFT_HOST 地址,因为在 Openshift 中无法将其绑定到 0.0.0.0(权限问题)
输出如下所示:
客户端一切正常,但如果你还记得,在 consumers.py 我有这个:
def ws_receive(message):
print "Receiving: '%s' from %s" % (message.content['text'], message.content['reply_channel'])
所以在我的终端中,服务器应该打印出如下内容:"Receiving: from " 但事实并非如此。我在这里缺少什么?
tl;dr:客户端 websocket 看起来连接正确,但服务器没有打印出消息来确认它。
我成功了。这个问题似乎与端口转发有关,这让我无法通过 openshift cloud 上的 apache 服务器将 websockets 连接到我的 daphne 服务器。
解决这个问题:
1) 使用 django 项目的默认墨盒,我无法修改 apache conf 文件,甚至无法更新 apache 以安装 mod_proxy_wstunnel 来支持 websockets,所以我决定更改它。 此外,mod_proxy_wstunnel 适用于 apache 2.4,但默认墨盒使用 2.2。
频道文档,推荐使用nginx。所以我找到了一个 cartridge 允许我将它与 uwsgi 和 django 一起使用。
我遵循了那个 repo 中的说明,但在推送我的代码之前,我稍微调整了操作挂钩以获取这些包的最新版本,并将示例 django 项目替换为我的。我对 requirements.txt.
做了同样的事情2)推完之后,我加了redis墨盒。
3) 然后我继续调整墨盒作为模板提供的 uwsgi.yaml 和 nginx.conf 以设置正确的值:
uwsgi.yaml
uwsgi:
socket: $OPENSHIFT_DIY_IP:15005
pidfile: $OPENSHIFT_TMP_DIR/uwsgi.pid
pythonpath: $OPENSHIFT_REPO_DIR/$APP_DIR
module: $APP_NAME.wsgi:application
virtualenv: $OPENSHIFT_DATA_DIR/virtualenv
nginx.conf
...
http {
...
server {
listen $OPENSHIFT_DIY_IP:$OPENSHIFT_DIY_PORT;
server_name localhost;
set_real_ip_from $OPENSHIFT_DIY_IP;
real_ip_header X-Forwarded-For;
location / {
uwsgi_pass $OPENSHIFT_DIY_IP:15005;
include uwsgi_params;
}
location /sync {
proxy_pass http://$OPENSHIFT_DIY_IP:8443;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
...
}
}
在我的 post_deploy 脚本中,我有以下内容:
...
python manage.py runworker -v2 &
daphne myapp.asgi:channel_layer -p 8443 -b $OPENSHIFT_DIY_IP -v2 &
...
所以 daphne 正在监听 $OPENSHIFT_DIY_IP:8443,当 nginx 收到来自 websockets 的请求时,如下所示:
var path = 'wss://'+window.location.host + ':8443/sync';
var ws = new WebSocket(path);
ws.onmessage = function(message) {
alert(message.data);
}
ws.onopen = function() {
this.send('WS Connecting to receive updates!');
}
现在我可以看到:
在浏览器中:
所以我知道这是有效的。我希望这可以帮助除我之外的其他人。