Re: Channels: rejecting web socket connections with a custom close code
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 On Saturday, 6 October 2018 10:47:46 UTC+1, Andrew Godwin wrote: > > 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+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/12417d20-f76f-4d3b-ba04-2ed55b22757e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Channels: rejecting web socket connections with a custom close code
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 -- 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/6a4a797c-e590-4f4d-a718-074a3cf704bc%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.