【问题标题】:Daphne server cannot connect with websockets on HTTPSDaphne 服务器无法通过 HTTPS 连接到 websocket
【发布时间】:2016-11-19 18:59:34
【问题描述】:

我正在 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 绑定到 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'])

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

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

【问题讨论】:

    标签: python django websocket openshift django-channels


    【解决方案1】:

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

    解决这个问题:

    1) 使用 django 项目的默认墨盒,我无法修改 apache conf 文件,甚至无法更新 apache 以安装 mod_proxy_wstunnel 以支持 websocket,因此我决定更改它。 此外,mod_proxy_wstunnel 在 apache 2.4 上工作,但默认墨盒使用 2.2。

    频道文档,推荐使用 nginx。所以我找到了一个cartridge,它允许我将它与 uwsgi 和 django 一起使用。

    我按照该仓库中的说明进行操作,但在推送我的代码之前,我稍微调整了操作挂钩以获取这些包的最新版本,并用我的替换示例 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!');
     }
    

    现在我可以看到了:

    在浏览器中:

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

    【讨论】:

      猜你喜欢
      • 2017-11-30
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      相关资源
      最近更新 更多