Python 使用Channel和Dephne部署Django

Python 使用Channel和Dephne部署Django,python,django,websocket,Python,Django,Websocket,我已经开发了一个使用django频道的聊天应用程序,现在我想在WebParty服务器中使用Dephne和django频道来部署它 我设定了一切,包括: 设置.py redis_host = os.environ.get('REDIS_HOST', 'localhost') CHANNEL_LAYERS = { "default": { # This example app uses the Redis channel layer implementation asgi_r

我已经开发了一个使用django频道的聊天应用程序,现在我想在WebParty服务器中使用Dephne和django频道来部署它

我设定了一切,包括:

设置.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",
    },
}
import os
from channels.asgi import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chatbot.settings")
channel_layer = get_channel_layer()
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"),
]
asgi.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",
    },
}
import os
from channels.asgi import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chatbot.settings")
channel_layer = get_channel_layer()
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"),
]
routing.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",
    },
}
import os
from channels.asgi import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chatbot.settings")
channel_layer = get_channel_layer()
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\u chat\u 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://request进行连接时,我在Safari错误控制台中得到了以下erorr:

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
我在日志中找到了这个erorr:

ERROR    Error trying to receive messages: Error 111 connecting to localhost:6379. Connection refused

可能您的redis服务器在6379端口未正确运行。可能您的redis服务器在6379端口未正确运行。