【问题标题】:Django channels using secured WebSocket connection - WSS://使用安全 WebSocket 连接的 Django 通道 - WSS://
【发布时间】:2020-03-04 18:45:43
【问题描述】:

当我尝试使用sslserver 运行 Django 应用程序时,如下所示,

python manage.py runsslserver

错误:

追溯:

Validating models...

System check identified no issues (0 silenced).
November 08, 2019 - 11:17:26
Django version 2.0.7, using settings 'dashboard_channels.settings'
Starting development server at https://127.0.0.1:8000/
Using SSL certificate: \lib\site-packages\sslserver\certs\development.crt
Using SSL key: \lib\site-packages\sslserver\certs\development.key
Quit the server with CTRL-BREAK.
[08/Nov/2019 11:18:33] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:34] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:35] "GET /static/js/jquery.js HTTP/1.1" 200 270575
Not Found: /ws/home
[08/Nov/2019 11:18:36] "GET /ws/home HTTP/1.1" 404 2134

浏览器控制台:

(index):31 WebSocket connection to 'wss://127.0.0.1:8000/ws/home' failed: Error during WebSocket handshake: Unexpected response code: 404
(index):41 error Event
(index):44 close CloseEvent

代码:

Javascript:

 var loc = window.location;
 var wsStart = 'ws://';
 if (loc.protocol == 'https:') {
     wsStart = 'wss://'
 }
 var endpoint = wsStart + loc.host + '/ws/home';

 var socket = new WebSocket(endpoint);

使用python manage.py runserver 命令可以正常工作,这意味着http 可以正常工作,但不能使用https

如何解决这个问题? (如何调试才能解决这个问题?)

还有其他方法可以在https 门户上部署 WebSockets 吗?

仍然面临这个问题。有人可以帮忙吗?

无论如何这是为了测试目的,最后,我需要将它部署在windows server机器上的Apache2.4上。我已经为https 设置了,但没有为网络套接字设置。

【问题讨论】:

  • 它说 Not Found: /ws/home。 http 也一样吗?
  • 不,它工作正常。 http 没有问题。
  • @shaikmoeed 请检查您是否在 settings.py 中配置了 CHANNEL_LAYERS
  • @wowkin2 CHANNEL_LAYERS 已配置并且与http 完全兼容。如何在https上运行?
  • @shaikmoed 你试过用达芙妮吗?如果没有 - 真的建议这样做:github.com/django/daphne

标签: javascript python ssl websocket wss


【解决方案1】:

我找到了答案,runserver 命令正确检测到asgi.py 文件并使用 daphne 在 WebSockets 上运行 Django 通道应用程序。不知何故,runsslserver 没有做同样的工作,它运行的是wsgi.py 文件而不是asgi.py 文件。

在阅读了不同的方法后,我知道我们可以使用我们的普通开发服务器(即使用wsgi.py文件)处理HTTPS请求和使用Daphne处理wss请求(即使用@987654330 @文件)。

Daphne 是官方设计的用于处理 django-channels 的服务器(构建在 twisted 模块的顶部)。

所以,最后,我们需要运行两台服务器来分别处理httpswss

# In command prompt 1 (For production, use Apache or Nginx to serve HTTP requests)
python manage.py runsslserver 0.0.0.0:8000

# In command prompt 2 (This works for production as well).
daphne -e ssl:8001:privateKey=cert\\private.pem:certKey=cert\\public.pem real_time_table.asgi:application

我们应该使用与runsslserver 相同的 SSL 证书 测试。

最后,在 JavaScript 中:

var loc = window.location;
var wsStart = 'ws://';
if (loc.protocol == 'https:') {
     wsStart = 'wss://'
}
// var endpoint = wsStart + 'your_ip_address:port_given_to_daphne_server' + '/ws/home';
// For above command, it look like this
var endpoint = wsStart + 'xxx.xx.xx.xxx:8001' + '/ws/home';
// Note the websocket port is 8001
var socket = new WebSocket(endpoint);

我希望,这会节省一些人的时间。

【讨论】:

  • 提示 2 中的命令是否使用第一个命令 runsslserver 生成的证书密钥?
  • 是的,我们需要为两台服务器使用相同的证书,即wsgiasgi 服务器。
猜你喜欢
  • 2014-07-06
  • 2014-10-27
  • 2021-08-06
  • 2019-05-31
  • 2020-07-17
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
相关资源
最近更新 更多