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 написа:
>
> 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
>
> On Tue, Feb 21, 2017 at 11:46 AM, Nikoleta Misheva <[email protected]
> <javascript:>> wrote:
>
>> 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.
>>
>>
>> вторник, 21 февруари 2017 г., 20:45:13 UTC+2, Andrew Godwin написа:
>>>
>>> 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
>>>
>>> On Tue, Feb 21, 2017 at 12:27 AM, Nikoleta Misheva <[email protected]>
>>> wrote:
>>>
>>>> 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?
>>>>
>>>>
>>>>
>>>> --
>>>> 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 [email protected].
>>>> To post to this group, send email to [email protected].
>>>> 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
>>>>
>>>> <https://groups.google.com/d/msgid/django-users/301c0b6a-822b-46b5-b7b6-72263786361f%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> 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/9b7feff4-4f60-48bc-a6bf-b2ab6891658a%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/django-users/9b7feff4-4f60-48bc-a6bf-b2ab6891658a%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
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/7255e2aa-565c-4446-b4d7-8cdb8bd884c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.