我有这个将数据发送到频道的JavaScript代码
// Note that the path doesn't matter for routing; any WebSocket
// connection gets bumped over to WebSocket consumers
socket = new WebSocket("ws://" + window.location.host + "/chat/");
socket.onmessage = function(e) {
alert(e.data);
}
socket.onopen = function() {
socket.send({"test":"data"});
}
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();
我很好奇message
我如何得到json {"test":"data"}
这是视图
# Connected to websocket.connect
@channel_session
def ws_connect(message, key):
# Accept connection
message.reply_channel.send({"accept": True})
答案 0 :(得分:0)
您实现了连接回调,但未实现消息到达服务器端点时应发生的情况。添加添加消息接收功能:
def on_receive(message):
print('test received: {}'.format(message.content['test']))
在routing.py
注册该功能:
channel_routing = [
route("websocket.connect", ws_connect),
route("websocket.receive", on_receive),
]
您发送的JSON消息将存储在message.content
中,这基本上只是一个python dict
。