wip: send peers list on connect/disconnect

This commit is contained in:
Yohan Boniface 2024-12-26 14:07:49 +01:00
parent 99ce09bb2f
commit a5db41595b

View file

@ -8,6 +8,7 @@ from .websocket_server import (
Request, Request,
ValidationError, ValidationError,
PeerMessage, PeerMessage,
ListPeersResponse,
) )
@ -27,6 +28,10 @@ class TokenMiddleware:
class SyncConsumer(AsyncWebsocketConsumer): class SyncConsumer(AsyncWebsocketConsumer):
@property
def peers(self):
return self.channel_layer.groups[self.map_id].keys()
async def connect(self): async def connect(self):
print("connect") print("connect")
self.map_id = self.scope["url_route"]["kwargs"]["map_id"] self.map_id = self.scope["url_route"]["kwargs"]["map_id"]
@ -35,17 +40,31 @@ class SyncConsumer(AsyncWebsocketConsumer):
await self.channel_layer.group_add(self.map_id, self.channel_name) await self.channel_layer.group_add(self.map_id, self.channel_name)
await self.accept() await self.accept()
await self.send_peers_list()
async def disconnect(self, close_code): async def disconnect(self, close_code):
print("disconnect") print("disconnect")
await self.channel_layer.group_discard(self.map_id, self.channel_name) await self.channel_layer.group_discard(self.map_id, self.channel_name)
await self.send_peers_list()
async def broadcast(self, event): async def send_peers_list(self):
print("broadcast", event) message = ListPeersResponse(peers=self.peers)
await self.send(event["message"]) await self.broadcast(message.model_dump_json())
async def pair_to_pair(self, event): async def broadcast(self, message):
print("pair_to_pair", event) print("broadcast", message)
await self.channel_layer.group_send(
self.map_id, {"message": message, "type": "on_message"}
)
async def send_to(self, channel, message):
print("pair to pair", channel, message)
await self.channel_layer.send(
channel, {"message": message, "type": "on_message"}
)
async def on_message(self, event):
# This is what the consummers does for a single channel
await self.send(event["message"]) await self.send(event["message"])
async def receive(self, text_data): async def receive(self, text_data):
@ -74,27 +93,15 @@ class SyncConsumer(AsyncWebsocketConsumer):
match incoming.root: match incoming.root:
# Broadcast all operation messages to connected peers # Broadcast all operation messages to connected peers
case JoinRequest(): case JoinRequest():
response = JoinResponse( response = JoinResponse(uuid=self.channel_name, peers=self.peers)
uuid=self.channel_name,
peers=self.channel_layer.groups[self.map_id].keys(),
)
await self.send(response.model_dump_json()) await self.send(response.model_dump_json())
case OperationMessage(): case OperationMessage():
await self.channel_layer.group_send( await self.broadcast(text_data)
self.map_id,
{"message": text_data, "type": "broadcast"},
)
# Send peer messages to the proper peer # Send peer messages to the proper peer
case PeerMessage(): case PeerMessage():
print("Received peermessage", incoming.root) print("Received peermessage", incoming.root)
await self.channel_layer.send( await self.send_to(incoming.root.recipient, text_data)
incoming.root.recipient,
{
"message": text_data,
"type": "pair_to_pair",
},
)
# Send peer messages to the proper peer # Send peer messages to the proper peer
# case PeerMessage(recipient=_id): # case PeerMessage(recipient=_id):