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
@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?