Django 频道使用 WS:// 但不使用 WSS://

问题描述

我听从了 django-channels tutorial 的解释,并且能够在本地进行聊天。

但是,它在生产中不起作用。

这是调试器的输出

WebSocket connection to 'wss://www.example.com/ws/chat/lobby/' Failed: Error during WebSocket handshake: Unexpected response code: 404
    (anonymous) @ (index):23
(index):40 Chat socket closed unexpectedly
    chatSocket.onclose @ (index):40
(index):56 WebSocket is already in CLOSING or CLOSED state.
    document.querySelector.onclick @ (index):56
    document.querySelector.onkeyup @ (index):47

这是我用来尝试让它与我们的 https 网站一起使用的 javascript :

const roomName = JSON.parse(document.getElementById('room-name').textContent);
var loc = window.location;
var wsstart = 'ws://';
if (loc.protocol === 'https:') {
    wsstart = 'wss://'
}
const chatSocket = new WebSocket(
    wsstart
    + window.location.host
    + '/ws/chat/'
    + roomName
    + '/'
);

我使用 redisgreen 作为 redis :

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer','CONfig': {
            "hosts": [('redis://:xxxxxxx@great-whale-0077a76ca7.redisgreen.net:11042/')]
        },},}

其余代码与教程一致。

我不确定我应该如何解决这个问题。任何线索?是否需要实施 daphne 才能在生产中运行渠道?还是我做错了什么?

非常感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)