带有Web套接字的Django Channels连接

时间:2019-01-29 14:42:08

标签: python django websocket django-channels

我已经实现了tutorial中给出的Django Channels。聊天正常,我可以在一个小组中发送和接收消息。

但是,当我尝试从网络套接字向通道聊天发送消息时,它不会显示该消息。而是会填充错误。但是,我能够从Channels聊天接收到我的Web套接字html页面的消息。

websocket.html

<!DOCTYPE html>
  <meta charset="utf-8" />
  <title>WebSocket Test</title>
  <script language="javascript" type="text/javascript">

  var wsUri = "ws://localhost:8000/ws/power/room/";
  var output;

  function init()
  {
    output = document.getElementById("output");
    testWebSocket();
    testWebSocket();
  }

  function testWebSocket()
  {
    websocket = new WebSocket(wsUri);
    websocket.onopen = function(evt) { onOpen(evt) };
    websocket.onclose = function(evt) { onClose(evt) };
    websocket.onmessage = function(evt) { onMessage(evt) };
    websocket.onerror = function(evt) { onError(evt) };
  }

  function onOpen(evt)
  {
    writeToScreen("CONNECTED");
    doSend("WebSocket rocks");
  }

  function onClose(evt)
  {
    writeToScreen("DISCONNECTED");
  }

  function onMessage(evt)
  {
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
    websocket.close();
  }

  function onError(evt)
  {
    writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
  }

  function doSend(message)
  {
    writeToScreen("SENT: " + message);
    websocket.send(message);
  }

  function writeToScreen(message)
  {
    var pre = document.createElement("p");
    pre.style.wordWrap = "break-word";
    pre.innerHTML = message;
    output.appendChild(pre);
  }

  window.addEventListener("load", init, false);

  </script>

  <h2>WebSocket Test</h2>

  <div id="output"></div>

consumers.py

class EchoConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        # Leave room group
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message
            }
        )

    # Receive message from room group
    async def chat_message(self, event):
        message = event['message']

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message
        }))

我在频道服务器上收到的错误是

WebSocket HANDSHAKING /ws/power/room/ [127.0.0.1:56879]
WebSocket CONNECT /ws/power/room/ [127.0.0.1:56879]
Exception inside application: Expecting value: line 1 column 1 (char 0)
  File "C:\Program Files\Python37\lib\site-packages\channels\sessions.py", line 179, in __call__
    return await self.inner(receive, self.send)
  File "C:\Program Files\Python37\lib\site-packages\channels\middleware.py", line 41, in coroutine_call
    await inner_instance(receive, send)
  File "C:\Program Files\Python37\lib\site-packages\channels\consumer.py", line 59, in __call__
    [receive, self.channel_receive], self.dispatch
  File "C:\Program Files\Python37\lib\site-packages\channels\utils.py", line 52, in await_many_dispatch
    await dispatch(result)
  File "C:\Program Files\Python37\lib\site-packages\channels\consumer.py", line 73, in dispatch
    await handler(message)
  File "C:\Program Files\Python37\lib\site-packages\channels\generic\websocket.py", line 196, in websocket_receive
    await self.receive(text_data=message["text"])
  File "C:\Users\Suleman\PycharmProjects\power\myChannels\consumers.py", line 39, in receive
    text_data_json = json.loads(text_data)
  File "C:\Program Files\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\Python37\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
  Expecting value: line 1 column 1 (char 0)
WebSocket DISCONNECT /ws/power/room/ [127.0.0.1:56879]

2 个答案:

答案 0 :(得分:2)

有效负载必须是有效的JSON对象,您只发送一个字符串。 尝试更改您的功能:

function doSend(message) {
    writeToScreen("SENT: " + message);
    websocket.send(JSON.stringify({message: message}();
}

答案 1 :(得分:0)

您需要对doSend函数中的消息进行JSON字符串化:

websocket.send(JSON.stringify({
  'message': message
}));