javascript - 使用安全 WebSocket 连接的 Django channel - WSS ://

标签 javascript python ssl websocket wss

当我尝试使用 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 服务器机器上的 Apache2.4 上。我已经为 https 设置但没有为网络套接字设置的地方。

最佳答案

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

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

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

We should use the same SSL certificates used by runsslserver for testing.

最后,在 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);

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

关于javascript - 使用安全 WebSocket 连接的 Django channel - WSS ://,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58761399/

相关文章:

javascript - Coffeescript 中带有两个参数的 For 循环

javascript - 模态在大屏幕上不起作用

python - 命名空间、argparse 和用法

python - 根据日期列范围将列添加到数据框

python - python的time.sleep()有多准确?

node.js - 如何使用 CA 证书保护 Node Red

javascript - 如何将两个 "on submit="值添加到表单中?

javascript - JS 隐藏标题元素并调整滚动条上的导航栏大小

特定目录的 SSL 证书

ssl - 使用自签名证书是否可以防止中间人攻击?