Hi!

Close code 1006 means that the connection was closed abruptly by the other
end (basically, the socket just got chopped off without an actual close
frame being sent).

This is what happens when you close during the handshake, which is what
closing during connect() does - at that point, the connection is still HTTP
before the upgrade has finished, and so Channels sends a HTTP 403 response
code down instead. Looks like your browser interprets this as code 1006 for
a websocket.

There's no way to change the HTTP response code sent when a handshake is
terminated at the moment - it's hardcoded (as you can see in Daphne's
serverReject method, here:
https://github.com/django/daphne/blob/master/daphne/ws_protocol.py#L200).

Given that no browser I know of will actually tell you the HTTP response
code in this case, I don't think there's much value in letting it be
changed - you're always going to see an aborted WebSocket connection code
instead. If you want to provide more detail to the user, you can instead
accept the socket, letting the handshake finish, and then close it
immediately with a custom WebSocket close code (you can do all of that
inside connect, I think).

Andrew

On Thu, Jun 21, 2018 at 2:52 PM Kirill Tyushchakov <kirthegr...@gmail.com>
wrote:

> Hello everyone!
> I'm new in Django Channels. I'm using Channels 2.0 and I have few
> questions about it.
> I'm using JsonWebsocketConsumer class for my consumer and I wrote my
> definition of connect method like this:
>
> def connect(self):
>     user = self.scope.get('user')
>     if user.is_anonymous:
>         super(NotificationsConsumer, self).connect()
>     else:
>         self.close(code=4003)
>
>
> On client side I'm using native JS Websocket.
> But when I try to connect to this socket as unauthorzied user I get
> code 1006 and HTTP response status code 403.
>
> My questions is:
> 1) How can I send custom close code to client?
> 2) Can I send another HTTP response status code? In my case 401
>
> --
> 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+unsubscr...@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/446d3be6-438b-4e59-ad4f-7841895f4fcb%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/446d3be6-438b-4e59-ad4f-7841895f4fcb%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 django-users+unsubscr...@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/CAFwN1upPzKf65Ju8RhbhMO67eoSxg2q00VgrNu_HApv7XVJtxg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to