get json sent to channels in view

11 views
Skip to first unread message

Samuel Muiruri

unread,
Aug 30, 2017, 1:08:51 PM8/30/17
to django...@googlegroups.com

I have this javascript code that send data to channels

// 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();

I'm curios how from message I can get the json {"test":"data"}

here's the view

# Connected to websocket.connect
@channel_session
def ws_connect(message, key):
    # Accept connection
    message.reply_channel.send({"accept": True})

​if i try message.content['test'] I get a key error

​    key = message['test']
  File "/usr/local/lib/python2.7/dist-packages/channels/message.py", line 36, in __getitem__
    return self.content[key]
KeyError: 'test'



--
Best Regards,
Samuel Muiruri.
Web Designer | +254 738 940064

Andrew Godwin

unread,
Aug 31, 2017, 2:57:43 AM8/31/17
to django...@googlegroups.com
You want to look for incoming data on the "websocket.receive" channel, which will call you once per message you send down with the right data. That's "websocket.connect", which happens after the socket opens but before data is sent down it.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJZFZXpbC%2BZ9xSWumRVALKMnt29DkTLchUtKBvkBJyXBh43nQw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages