Hi, Andrew! Thanks a lot for your response.
I've changed my consumer's connect method like this:

def connect(self):
    user = self.scope.get('user')
    super(NotificationsConsumer, self).connect()
    if user.is_anonymous:
        self.close(code=4003)



I've tested it in Chrome, Firefox, Safari and IE 11 and here is the result 
that I've got:
Chrome - 1006
IE 11 - 1005
Firefox - 4003
Safari - 1006

It doesn't return 403 HTTP status code. Seems it works fine, but I think I 
need to test it more.
So the expected close code appears only in Firefox. Should I consider it as 
the browser issue?


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 <[email protected] 
> <javascript:>> 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 [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/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 [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/b758b6f7-91f0-43b8-8ea8-087c137598c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to