2017-05-24 4 views
0
I am using Django with django channels I am going off of Andrew Godwins django channels examples the "MultiChat" example I am using Webfaction and Putty to try and get it going here is the code I got: 

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, 27411)], 
     }, 
     "ROUTING": "myproject.routing.channel_routing", 
    }, 
} 

を確立する前のWebSocketが閉じて、私はここに疑問を持っている私は変化するであろう、それは私のウェブサイトのための私のIPアドレスにlocalhostを言うかのRedisを使用しているとき、私はローカルホストとしてそれを残していました?Djangoのチャンネルエラー:失敗しました:接続が

私のindex.html私はこれを持っています。このコードを調整したり更新したりする必要がある場合は、感謝します。

{% extends "base.html" %} 

{% block title %}MultiChat Example{% endblock %} 
{% block header_text %}MultiChat Example{% endblock %} 

{% block content %} 

    <ul class="rooms"> 
     {% for room in rooms %} 
      <li class="room-link" data-room-id="{{ room.id }}">{{ room }}</li> 
     {% empty %} 
      <p class="empty">No chat rooms defined. Maybe make some in the <a href="{% url 'admin:index' %}">admin</a>?</p> 
     {% endfor %} 
    </ul> 

    <div id="chats"> 
    </div> 

{% endblock %} 


{% block extra_body %} 
    <script> 
     $(function() { 
      // Correctly decide between ws:// and wss:// 
      var ws_path = "/chat/stream/"; 
      console.log("Connecting to " + ws_path); 

      var webSocketBridge = new channels.WebSocketBridge(); 
      webSocketBridge.connect(ws_path); 
      // Handle incoming messages 
      webSocketBridge.listen(function(data) { 
       // Decode the JSON 
       console.log("Got websocket message", data); 
       // Handle errors 
       if (data.error) { 
        alert(data.error); 
        return; 
       } 
       // Handle joining 
       if (data.join) { 
        console.log("Joining room " + data.join); 
        var roomdiv = $(
          "<div class='room' id='room-" + data.join + "'>" + 
          "<h2>" + data.title + "</h2>" + 
          "<div class='messages'></div>" + 
          "<form><input><button>Send</button></form>" + 
          "</div>" 
        ); 
        // Hook up send button to send a message 
        roomdiv.find("form").on("submit", function() { 
         webSocketBridge.send({ 
          "command": "send", 
          "room": data.join, 
          "message": roomdiv.find("input").val() 
         }); 
         roomdiv.find("input").val(""); 
         return false; 
        }); 
        $("#chats").append(roomdiv); 
        // Handle leaving 
       } else if (data.leave) { 
        console.log("Leaving room " + data.leave); 
        $("#room-" + data.leave).remove(); 
        // Handle getting a message 
       } else if (data.message || data.msg_type != 0) { 
        var msgdiv = $("#room-" + data.room + " .messages"); 
        var ok_msg = ""; 
        // msg types are defined in chat/settings.py 
        // Only for demo purposes is hardcoded, in production scenarios, consider call a service. 
        switch (data.msg_type) { 
         case 0: 
          // Message 
          ok_msg = "<div class='message'>" + 
            "<span class='username'>" + data.username + "</span>" + 
            "<span class='body'>" + data.message + "</span>" + 
            "</div>"; 
          break; 
         case 1: 
          // Warning/Advice messages 
          ok_msg = "<div class='contextual-message text-warning'>" + data.message + 
            "</div>"; 
          break; 
         case 2: 
          // Alert/Danger messages 
          ok_msg = "<div class='contextual-message text-danger'>" + data.message + 
            "</div>"; 
          break; 
         case 3: 
          // "Muted" messages 
          ok_msg = "<div class='contextual-message text-muted'>" + data.message + 
            "</div>"; 
          break; 
         case 4: 
          // User joined room 
          ok_msg = "<div class='contextual-message text-muted'>" + data.username + 
            " joined the room!" + 
            "</div>"; 
          break; 
         case 5: 
          // User left room 
          ok_msg = "<div class='contextual-message text-muted'>" + data.username + 
            " left the room!" + 
            "</div>"; 
          break; 
         default: 
          console.log("Unsupported message type!"); 
          return; 
        } 
        msgdiv.append(ok_msg); 

        msgdiv.scrollTop(msgdiv.prop("scrollHeight")); 
       } else { 
        console.log("Cannot handle message!"); 
       } 
      }); 

      // Says if we joined a room or not by if there's a div for it 
      inRoom = function (roomId) { 
       return $("#room-" + roomId).length > 0; 
      }; 

      // Room join/leave 
      $("li.room-link").click(function() { 
       roomId = $(this).attr("data-room-id"); 
       if (inRoom(roomId)) { 
        // Leave room 
        $(this).removeClass("joined"); 
        webSocketBridge.send({ 
         "command": "leave", 
         "room": roomId 
        }); 
       } else { 
        // Join room 
        $(this).addClass("joined"); 
        webSocketBridge.send({ 
         "command": "join", 
         "room": roomId 
        }); 
       } 
      }); 

      // Helpful debugging 
      webSocketBridge.socket.onopen = function() { 
       console.log("Connected to chat socket"); 
      }; 
      webSocketBridge.socket.onclose = function() { 
       console.log("Disconnected from chat socket"); 
      } 
     }); 
    </script> 
{% endblock %} 

and my error: 
(index):40 Connecting to /chat/stream 
websocketbridge.js:118 WebSocket connection to 'ws://www.openchat.us/chat/stream' failed: Error during WebSocket handshake: Invalid status line 
connect @ websocketbridge.js:118 
(index):161 Disconnected from chat socket 
websocketbridge.js:183 Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. 
    at ReconnectingWebsocket.send (http://www.openchat.us/static/channels/js/websocketbridge.js:183:12) 
    at WebSocketBridge.send (http://www.openchat.us/static/channels/js/websocketbridge.js:364:19) 
    at HTMLLIElement.<anonymous> (http://www.openchat.us/:149:37) 
    at HTMLLIElement.dispatch (http://www.openchat.us/static/channels/js/jquery-1.12.2.min.js:3:12444) 
    at HTMLLIElement.r.handle (http://www.openchat.us/static/channels/js/jquery-1.12.2.min.js:3:9173) 
(index):1 WebSocket connection to 'ws://www.openchat.us/chat/stream' failed: WebSocket is closed before the connection is established. 
2(index):161 Disconnected from chat socket 
2websocketbridge.js:118 WebSocket connection to 'ws://www.openchat.us/chat/stream' failed: Error during WebSocket handshake: Invalid status line 
connect @ websocketbridge.js:118 
(index):161 Disconnected from chat socket 

答えて

0

あなたのRedisは、あなたのwepappよりも同じ場所にある場合は、RedisのIPアドレスとしてlocalhostを設定することができます。

  • あなたはパテについて話しました。あなたはリモートマシンですか?
  • あなたの赤ちゃんは、settingsに定義されているものと同じポートをリッスンしていますか?
+0

こんにちはMathieuはいredisはwebfactionで提供されているマシンにインストールされています私はそこにマシンにsshを使用しています私はredisをインストールしているwebfactionと私はwebfactionマシンと事にsshに私はコマンドを入力するときです./redis-serverそれはそれを127.0.0.1:6379に開始します。しかし、djangoチャンネルを使用するときは、コマンドdaphne -p {}を入力する必要があります。myproject.asgi:channel_layer#ポートはポート私の現在のエラーは:ws://www.openchat.us/chat/stream/に接続しています reconnecting-websocket webfactionからwebsocketアプリケーションから、私はredisを開始したいと思っています./redis-server -p {myportumberhere} – robert

+0

私の現在のエラーは次のとおりです。 .min.js: 'ws://www.openchat.us/chat/stream/'へのWebSocket接続が失敗しました:WebSocketハンドシェイク中にエラーが発生しました:ステータスラインが無効です@@ reconnecting-websocket.min.js:1 @(インデックス):49 i @ jquery-1.12.2.min.js:2 fireWith @jquery-1.12.2.min.js:2 ready @ jquery-1.12.2.min.js:2 K @ jquery-1.12.2.min.js:2 (index):164チャットソケットから切断されました – robert

関連する問題