首页 > 解决方案 > Django 频道使用 WS:// 但不使用 WSS://

问题描述

我遵循了django-channels 教程中的解释,并且能够在本地运行聊天。

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

这是调试器的输出:

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 才能在生产中运行渠道?还是我做错了什么?

非常感谢你的帮助。

标签: djangowebsocketdjango-channelsdaphne

解决方案


推荐阅读