我使用django频道开发了一个聊天应用程序,现在我想在webfaction服务器中使用Dephne和django频道进行部署。
我设置了所有内容,包括:
settings.py
redis_host = os.environ.get('REDIS_HOST', 'localhost')
CHANNEL_LAYERS = {
"default": {
# This example app uses the Redis channel layer implementation asgi_redis
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {"hosts": [(redis_host, 6379)],},
"ROUTING": "chatbot.routing.channel_routing",
},
}
asgi.py
import os
from channels.asgi import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chatbot.settings")
channel_layer = get_channel_layer()
routing.py
from channels.routing import route, include
from channels import include
channel_routing = [
# Include sub-routing from an app.
include("Pchat.routing.websocket_routing", path=r"^/Pchat"),
# Custom handler for message sending (see Room.send_message).
# Can't go in the include above as it's not got a `path` attribute to match on.
include("Pchat.routing.custom_routing"),
]
PP_chat_index.js
$(window).load(function() {
$messages.mCustomScrollbar();
setTimeout(function() {
welcomingMessage();
var ws_path = "/Pchat/webscok/";
socket = new WebSocket("ws://" + window.location.host);
socket.onmessage = function(e) {
var data = JSON.parse(e.data);
chatMessage(data);} }, 100);});
但是现在当我尝试使用ws://连接时,我在Safari错误控制台中得到了这个错误:
WebSocket connection to 'ws://gadgetron.store/Pchat/webscok/' failed: Unexpected response code: 502
当我尝试将Dephne运行到终端中的特定端口号时:
daphne -b 0.0.0.0 -p 22783 chatbot.asgi:channel_layer
我在日志中得到了这个错误:
ERROR Error trying to receive messages: Error 111 connecting to localhost:6379. Connection refused