Django-channels disconnects right after sending a message

808 views
Skip to first unread message

Nikoleta Misheva

unread,
Feb 21, 2017, 3:27:34 AM2/21/17
to Django users
When I send message the websocket disconnects. I am able to get the message, but I can't proceed further since disconnect is supposed to delete the room. I tested it with basic prints after every statement and it disconnects right after the receiver gets the message because everything that is in ws_receive is printed after it disconnects

My consumer:
@channel_session_user
def ws_receive(message):
 username
= message.user.username
 text
= json.loads(message['text']).get('text')
 
# Use my algorithm here
 score
= score_argument.get_rating(text)
 
# find the room with our users
 
# print(type(username))
 
# print(username)
 current_room
= get_object_or_404(PairUsers, Q(username_a=username) | Q(username_b=username))

 
# current_room = PairUsers.objects.filter(Q(username_a=username) | Q(username_b=username)).first()

 
# check which user you got and send the message to the other
 
if current_room.username_b == username:
 current_room
.score_b = score
 other_channel
= Channel(current_room.reply_channel_a)
 message
.reply_channel.send({'text': json.dumps({
 
"message": text,
 
"user": username, }),
 
})
 message
.reply_channel.send({'text': json.dumps({
 
"score": score,
 
"user": username, }),
 
})
 other_channel
.send({'text': json.dumps({
 
"message": text,
 
"user": username, }),
 
})
 other_channel
.send({'text': json.dumps({
 
"score": score,
 
"user": username, }),
 
})
 
else:
 current_room
.score_a = score
 other_channel
= Channel(current_room.reply_channel_b)
 message
.reply_channel.send({'text': json.dumps({
 
"message": text,
 
"user": username, }),
 
})
 message
.reply_channel.send({'text': json.dumps({
 
"score": score,
 
"user": username, }),
 
})
 other_channel
.send({'text': json.dumps({
 
"message": text,
 
"user": username, }),
 
})
 other_channel
.send({'text': json.dumps({
 
"score": score,
 
"user": username, }),
 
})
The JS:
$(function () {
 
// Correctly decide between ws:// and wss://
 
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
 
var ws_path = ws_scheme + '://' + window.location.host + window.location.pathname;
 console
.log("Connecting to " + ws_path);
 
var socket = new ReconnectingWebSocket(ws_path);

 socket
.onmessage = function(message){
 
var data = JSON.parse(message.data);
 
if(!data.score){
 
var element = $([
 
"<tr>",
 
"<td>" + data.message + "</td>",
 
"<td>",
 
"<div class='center-block'>" + data.user + "</div>",
 
"</td>",
 
"</tr>"
 
].join("\n"));
 $
("#chat_table tbody").append(element);
 
}
 
else {
 
var element = $([
 
"<tr>",
 
"<td>" + data.user + "'s score is:" + data.score + "</td>",
 
"</tr>"
 
].join("\n"));
 $
("#chat_table tbody").append(element);

 
}
 
}

 $
('#arg_form').on('submit',function () {
 socket
.send(JSON.stringify({
 
"text": $('#argument').val()
 
}))})

 
});
What might be the cause?


Andrew Godwin

unread,
Feb 21, 2017, 1:45:13 PM2/21/17
to django...@googlegroups.com
How does it disconnect? What WebSocket close code do you get? (You'll need to add JS to log it) What prints on the Python console?

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/301c0b6a-822b-46b5-b7b6-72263786361f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nikoleta Misheva

unread,
Feb 21, 2017, 2:46:48 PM2/21/17
to Django users
I don't know how to get the close code. It disconnects right after you hit the send button but it manages to send the message, I had prints and the message was right and etc but was always after disconnect. My disconnect function basically deletes the room. And python's console logs:
[2017/02/21 21:42:50] WebSocket DISCONNECT /play [127.0.0.1:65273]
[2017/02/21 21:42:50] WebSocket HANDSHAKING /play [127.0.0.1:65282]
It reconnects.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andrew Godwin

unread,
Feb 21, 2017, 8:08:09 PM2/21/17
to django...@googlegroups.com
I'm afraid I don't really know what's going on then - the logs don't reveal very much. I'd try reducing it down to simpler code until you can replicate it in only a few lines and work from there.

Andrew

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.

Nikoleta Misheva

unread,
Feb 22, 2017, 1:15:19 AM2/22/17
to Django users
How do I log an WebsocketCloseExeption it might deliver more useful information?

Nikoleta Misheva

unread,
Feb 22, 2017, 2:03:19 AM2/22/17
to Django users
I tried  the simplest example and it works fine and does not disconnect. 
def websocket_receive(message):
 text
= message.content.get('text')
 
if text:
 message
.reply_channel.send({"text": "You said: {}".format(text)})

And using the JS console 
socket = new WebSocket("ws://" + window.location.host + "/chat/");
socket
.onmessage = function(e) {
    alert
(e.data);
}
socket
.onopen = function() {
    socket
.send("hello world");
}

сряда, 22 февруари 2017 г., 3:08:09 UTC+2, Andrew Godwin написа:

Andrew Godwin

unread,
Feb 22, 2017, 2:27:32 AM2/22/17
to django...@googlegroups.com
Then you need to add more and more back until it does disconnect and you should be able to find the line that's doing it!

Andrew

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.

Nikoleta Misheva

unread,
Feb 22, 2017, 3:08:29 AM2/22/17
to Django users
I tried it and it works fine, but as soon as I try to hang this to a button it disconnects, so this line 
$('#arg_form').on('submit',function ()

is the problem, if I send message without hanging it to a button submit function it is ok with or without JSON.stringify
Reply all
Reply to author
Forward
0 new messages