Skip to content

Commit

Permalink
Add more logging (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart authored Nov 22, 2022
1 parent ebf67f3 commit 25f7404
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ypy_websocket/websocket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, ydoc: Y.YDoc, websocket, log=None):
asyncio.create_task(self._run())

async def _run(self):
await sync(self._ydoc, self._websocket)
await sync(self._ydoc, self._websocket, self.log)
send_task = asyncio.create_task(self._send())
async for message in self._websocket:
if message[0] == YMessageType.SYNC:
Expand Down
4 changes: 2 additions & 2 deletions ypy_websocket/websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def delete_room(self, *, name: Optional[str] = None, room: Optional[YRoom] = Non
async def serve(self, websocket):
room = self.get_room(websocket.path)
room.clients.append(websocket)
await sync(room.ydoc, websocket)
await sync(room.ydoc, websocket, self.log)
async for message in websocket:
# filter messages (e.g. awareness)
skip = False
Expand All @@ -133,7 +133,7 @@ async def serve(self, websocket):
# including itself, because it's used to keep the connection alive
self.log.debug(
"Received %s message from endpoint: %s",
YMessageType.AWARENESS.raw_str(),
YMessageType.AWARENESS.name,
websocket.path,
)
for client in room.clients:
Expand Down
17 changes: 8 additions & 9 deletions ypy_websocket/yutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ class YMessageType(IntEnum):
SYNC = 0
AWARENESS = 1

def raw_str(self) -> str:
return self.name


class YSyncMessageType(IntEnum):
SYNC_STEP1 = 0
SYNC_STEP2 = 1
SYNC_UPDATE = 2

def raw_str(self) -> str:
return self.name


def write_var_uint(num: int) -> bytes:
res = []
Expand Down Expand Up @@ -109,7 +103,7 @@ async def process_sync_message(message: bytes, ydoc: Y.YDoc, websocket, log) ->
msg = message[1:]
log.debug(
"Received %s message from endpoint: %s",
YSyncMessageType(message_type).raw_str(),
YSyncMessageType(message_type).name,
websocket.path,
)
if message_type == YSyncMessageType.SYNC_STEP1:
Expand All @@ -118,7 +112,7 @@ async def process_sync_message(message: bytes, ydoc: Y.YDoc, websocket, log) ->
reply = create_sync_step2_message(update)
log.debug(
"Sending %s message to endpoint: %s",
YSyncMessageType.SYNC_STEP2.raw_str(),
YSyncMessageType.SYNC_STEP2.name,
websocket.path,
)
await websocket.send(reply)
Expand All @@ -130,7 +124,12 @@ async def process_sync_message(message: bytes, ydoc: Y.YDoc, websocket, log) ->
Y.apply_update(ydoc, update)


async def sync(ydoc: Y.YDoc, websocket):
async def sync(ydoc: Y.YDoc, websocket, log):
state = Y.encode_state_vector(ydoc)
msg = create_sync_step1_message(state)
log.debug(
"Sending %s message to endpoint: %s",
YSyncMessageType.SYNC_STEP1.name,
websocket.path,
)
await websocket.send(msg)

0 comments on commit 25f7404

Please sign in to comment.