|
| 1 | +"""Echo WebSocket handler for real time collaboration with Yjs""" |
| 2 | + |
| 3 | +# Copyright (c) Jupyter Development Team. |
| 4 | +# Distributed under the terms of the Modified BSD License. |
| 5 | + |
| 6 | +import uuid |
| 7 | +import time |
| 8 | + |
| 9 | +from tornado.ioloop import IOLoop |
| 10 | +from tornado.websocket import WebSocketHandler |
| 11 | + |
| 12 | +acquireLockMessageType = 127 |
| 13 | +releaseLockMessageType = 126 |
| 14 | +requestInitializedContentMessageType = 125 |
| 15 | +putInitializedContentMessageType = 124 |
| 16 | + |
| 17 | +class YjsRoom: |
| 18 | + def __init__(self): |
| 19 | + self.lock = None |
| 20 | + self.clients = {} |
| 21 | + self.content = bytes([]) |
| 22 | + |
| 23 | +class YJSEchoWS(WebSocketHandler): |
| 24 | + rooms = {} |
| 25 | + |
| 26 | + def open(self, guid): |
| 27 | + #print("[YJSEchoWS]: open", guid) |
| 28 | + cls = self.__class__ |
| 29 | + self.id = str(uuid.uuid4()) |
| 30 | + self.room_id = guid |
| 31 | + room = cls.rooms.get(self.room_id) |
| 32 | + if room is None: |
| 33 | + room = YjsRoom() |
| 34 | + cls.rooms[self.room_id] = room |
| 35 | + room.clients[self.id] = ( IOLoop.current(), self.hook_send_message ) |
| 36 | + # Send SyncStep1 message (based on y-protocols) |
| 37 | + self.write_message(bytes([0, 0, 1, 0]), binary=True) |
| 38 | + |
| 39 | + def on_message(self, message): |
| 40 | + #print("[YJSEchoWS]: message, ", message) |
| 41 | + cls = self.__class__ |
| 42 | + room = cls.rooms.get(self.room_id) |
| 43 | + if message[0] == acquireLockMessageType: # tries to acquire lock |
| 44 | + now = int(time.time()) |
| 45 | + if room.lock is None or now - room.lock > 15: # no lock or timeout |
| 46 | + room.lock = now |
| 47 | + # print('Acquired new lock: ', room.lock) |
| 48 | + # return acquired lock |
| 49 | + self.write_message(bytes([acquireLockMessageType]) + room.lock.to_bytes(4, byteorder = 'little'), binary=True) |
| 50 | + elif message[0] == releaseLockMessageType: |
| 51 | + releasedLock = int.from_bytes(message[1:], byteorder = 'little') |
| 52 | + # print("trying release lock: ", releasedLock) |
| 53 | + if room.lock == releasedLock: |
| 54 | + # print('released lock: ', room.lock) |
| 55 | + room.lock = None |
| 56 | + elif message[0] == requestInitializedContentMessageType: |
| 57 | + # print("client requested initial content") |
| 58 | + self.write_message(bytes([requestInitializedContentMessageType]) + room.content, binary=True) |
| 59 | + elif message[0] == putInitializedContentMessageType: |
| 60 | + # print("client put initialized content") |
| 61 | + room.content = message[1:] |
| 62 | + elif room: |
| 63 | + for client_id, (loop, hook_send_message) in room.clients.items() : |
| 64 | + if self.id != client_id : |
| 65 | + loop.add_callback(hook_send_message, message) |
| 66 | + |
| 67 | + def on_close(self): |
| 68 | + # print("[YJSEchoWS]: close") |
| 69 | + cls = self.__class__ |
| 70 | + room = cls.rooms.get(self.room_id) |
| 71 | + room.clients.pop(self.id) |
| 72 | + if len(room.clients) == 0 : |
| 73 | + cls.rooms.pop(self.room_id) |
| 74 | + # print("[YJSEchoWS]: close room " + self.room_id) |
| 75 | + |
| 76 | + return True |
| 77 | + |
| 78 | + def check_origin(self, origin): |
| 79 | + #print("[YJSEchoWS]: check origin") |
| 80 | + return True |
| 81 | + |
| 82 | + def hook_send_message(self, msg): |
| 83 | + self.write_message(msg, binary=True) |
0 commit comments