Advertisement
for-mile

Article-generic

May 13th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. from channels.generic.websockets import JsonWebsocketConsumer
  2.  
  3. class MyConsumer(JsonWebsocketConsumer):
  4.  
  5.     # Set to True if you want it, else leave it out
  6.     strict_ordering = True
  7.    
  8.     def connection_groups(self, **kwargs):
  9.         """
  10.         Called to return the list of groups to automatically add/remove
  11.         this connection to/from.
  12.         """
  13.         person_name = self.message.channel_session['username']
  14.         player = Player.objects.get(user__username=person_name)
  15.         game_name = player.game.name
  16.         return [game_name]
  17.  
  18.     @channel_token_auth
  19.     def connect(self, message, **kwargs):
  20.         """
  21.         Perform things on connection start
  22.         """
  23.         message.reply_channel.send({"accept": True})
  24.        
  25.  
  26.     @channel_session
  27.     def receive(self, content, **kwargs):
  28.             """
  29.             Called when a message is received with decoded JSON content
  30.             """
  31.             # Simple echo
  32.             self.send({'text': 'You can not send messages'})
  33.  
  34.     def disconnect(self, message, **kwargs):
  35.         """
  36.         Perform things on connection close
  37.         """
  38.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement