Channels: rejecting web socket connections with a custom close code

447 views
Skip to first unread message

Matt F

unread,
Oct 3, 2018, 7:23:30 AM10/3/18
to Django users
Hello all,

I have a question about the process of accepting/rejecting web socket connections in Channels consumers (v2.1.3).  I have found that if I have code like this:

class MyConsumer(JsonWebsocketConsumer):
   
def connect(self):
       
if authenticated:
           
self.accept()
       
else:
           
self.close(4001)


then when rejecting a connection the close code always comes through on the client side as 1006 (abnormal closure) instead of 4001.  There is a stack overflow post about the same issue that led me to an answer that solves this: https://stackoverflow.com/questions/50009440/django-channels-2-0-websocketbridge-js-403-when-self-codecode-1007

And a code snippet that works correctly is as follows:

class MyConsumer(JsonWebsocketConsumer):
   
def connect(self):
       
self.accept()
       
if not authenticated:
            self.close(4001)


The Channels documentation is ambiguous as to which of these is correct.  Now I think I know the answer as the first way breaks and the second way works, but I wanted to check with someone more knowledgeable that this way of doing things is by design and not just working by accident.  It feels a bit wrong to accept a connection and then immediately close it, but I understand if you have to do it this way, to get the custom close code returned perhaps you have to have an open connection in the first place.  Can someone please confirm?

Many thanks!
Matt

Andrew Godwin

unread,
Oct 6, 2018, 5:47:46 AM10/6/18
to django...@googlegroups.com
Hi Matt,

This is because the two ways of closing are different - closing before the socket is open sends a HTTP error code (what browsers report for this is different in terms of websocket errors), whereas closing once it is open allows a websocket error to be used.

This is referenced in the spec at 
https://asgi.readthedocs.io/en/latest/specs/www.html#websocket but somewhat obliquely; I’ll try to make it a bit clearer.

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...@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/6a4a797c-e590-4f4d-a718-074a3cf704bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

reply...@gmail.com

unread,
Oct 6, 2018, 9:28:09 AM10/6/18
to Django users
hello someone help me i am getting django 404 error ..how fix it ...please help me i am so frustrated

Matt F

unread,
Oct 8, 2018, 3:12:42 AM10/8/18
to Django users
Hi Andrew,

Thanks for clarifying.  I didn't think to look at the ASGI spec but it is described quite well there under the WebSocket/Close section.  Thanks for your hard work on this project.

Regards,
Matt
Reply all
Reply to author
Forward
0 new messages