23 lines
867 B
Python
23 lines
867 B
Python
|
|
import json
|
||
|
|
|
||
|
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
||
|
|
|
||
|
|
|
||
|
|
class SceneConsumer(AsyncWebsocketConsumer):
|
||
|
|
"""One connection per open project. Joins the project's group and relays
|
||
|
|
scene deltas the server broadcasts (see scenes.views.scene). Read-only:
|
||
|
|
edits still go through the PUT endpoint, which is the broadcast source."""
|
||
|
|
|
||
|
|
async def connect(self):
|
||
|
|
self.pk = self.scope['url_route']['kwargs']['pk']
|
||
|
|
self.group = f'project_{self.pk}'
|
||
|
|
await self.channel_layer.group_add(self.group, self.channel_name)
|
||
|
|
await self.accept()
|
||
|
|
|
||
|
|
async def disconnect(self, code):
|
||
|
|
await self.channel_layer.group_discard(self.group, self.channel_name)
|
||
|
|
|
||
|
|
# broadcast handler: {type: "scene.delta", delta: {...}}
|
||
|
|
async def scene_delta(self, event):
|
||
|
|
await self.send(text_data=json.dumps(event['delta']))
|