gpt4 book ai didi

python - Daphne 服务器无法连接 HTTPS 上的 websockets

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:24 26 4
gpt4 key购买 nike

我正在 Openshift 云上部署一个 Django 项目。该项目使用 channels和 Websockets 使其异步工作。问题是我无法从浏览器成功地将 websockets 连接到我在服务器端运行的 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 绑定(bind)到 OPENSHIFT_HOST 地址,因为在 Openshift 中无法将其绑定(bind)到 0.0.0.0(权限问题)

输出看起来像这样:

first

second

third

在客户端一切看起来都很好,但如果你还记得的话,在 consumers.py 中我有这个:

def ws_receive(message):
print "Receiving: '%s' from %s" % (message.content['text'], message.content['reply_channel'])

所以在我的终端中,服务器应该打印出类似:“Receiving: from ”的内容,但事实并非如此。我在这里缺少什么?

tl;dr:客户端 websocket 看起来连接正确,但服务器没有打印出消息来确认它。

最佳答案

我设法让它发挥作用。该问题似乎与端口转发有关,这使我无法通过 openshift 云上的 apache 服务器将 websocket 连接到我的 daphne 服务器。

解决这个问题:

1) 使用 django 项目的默认墨盒,我无法修改 apache conf 文件,甚至无法更新 apache 以安装 mod_proxy_wstunnel 以支持 websockets,所以我决定更改它。此外,mod_proxy_wstunnel 适用于 apache 2.4,但默认卡式盒使用 2.2。

channel 文档,推荐使用nginx。所以我找到了一个 cartridge这让我可以将它与 uwsgi 和 django 一起使用。

我遵循了那个 repo 中的说明,但在推送我的代码之前,我稍微调整了操作 Hook 以获取这些包的最新版本,并将示例 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!');
}

现在我可以看到:

terminal

在浏览器中:

alert

所以我知道这是有效的。我希望这可以帮助除我之外的其他人。

关于python - Daphne 服务器无法连接 HTTPS 上的 websockets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38415209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com