Thursday, August 31, 2017

django-channels get the json post on connect

I have this in routing.py for django-channels

channel_routing = [
   # Wire up websocket channels to our consumers:
   route("websocket.connect", ws_connect),
   route("websocket.receive", ws_message),
]

and the ws_connect looks like this

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

and when the page loads this socket connection is made correctly

<script type="text/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(response) {
   console.log(response.data);
}
socket.onopen = function() {
   socket.send({"test":"data"});
}
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();
</script>

I want to start testing if I can have a background process running from this, so I set a test json input to socket.send however I haven't found anything on the documentation, apologies if I missed it. How to access this info in ws_connect it should be in message same way there's request I believe.

--
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-users@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/375ec390-a4fb-4112-a378-3a1d1a56eb6d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment