mirror of
https://github.com/RoboSats/robosats.git
synced 2024-09-11 22:07:08 +03:00
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
||
|
import json
|
||
|
|
||
|
class ChatRoomConsumer(AsyncWebsocketConsumer):
|
||
|
|
||
|
async def connect(self):
|
||
|
self.order_id = self.scope['url_route']['kwargs']['order_id']
|
||
|
self.room_group_name = f'chat_order_{self.order_id}'
|
||
|
|
||
|
|
||
|
await self.channel_layer.group_add(
|
||
|
self.room_group_name,
|
||
|
self.channel_name
|
||
|
)
|
||
|
|
||
|
await self.accept()
|
||
|
|
||
|
async def disconnect(self, close_code):
|
||
|
await self.channel_layer.group_discard(
|
||
|
self.room_group_name,
|
||
|
self.channel_name
|
||
|
)
|
||
|
|
||
|
async def receive(self, text_data):
|
||
|
text_data_json = json.loads(text_data)
|
||
|
message = text_data_json['message']
|
||
|
username = text_data_json['username']
|
||
|
|
||
|
await self.channel_layer.group_send(
|
||
|
self.room_group_name,
|
||
|
{
|
||
|
'type': 'chatroom_message',
|
||
|
'message': message,
|
||
|
'username': username,
|
||
|
}
|
||
|
)
|
||
|
|
||
|
async def chatroom_message(self, event):
|
||
|
message = event['message']
|
||
|
username = event['username']
|
||
|
try:
|
||
|
message = int(message)*42
|
||
|
except:
|
||
|
pass
|
||
|
await self.send(text_data=json.dumps({
|
||
|
'message': message,
|
||
|
'username': username,
|
||
|
}))
|
||
|
|
||
|
pass
|