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)
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