Created
August 22, 2023 07:07
-
-
Save ankurkumartech/be27a182680f0598d95bfe556e281b4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from channels.consumer import SyncConsumer, AsyncConsumer | |
from channels.exceptions import StopConsumer | |
from asgiref.sync import async_to_sync | |
class MySyncConsumer(SyncConsumer): | |
def websocket_connect(self, event): | |
async_to_sync(self.channel_layer.group_add)('programmers', | |
self.channel_name) | |
self.send({ | |
'type':'websocket.accept' | |
}) | |
def websocket_receive(self, event): | |
async_to_sync(self.channel_layer.group_send)('programmers', { | |
'type':'chat.message', | |
'message':event['text'] | |
}) | |
def websocket_disconnect(self, event): | |
print('websocker disconnect', event) | |
def chat_message(self, event): | |
self.send({'type':'websocket.send', | |
'text':event['message']}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment