Skip to content

Commit

Permalink
added demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
Waidhoferj committed Sep 13, 2022
1 parent 5ea9903 commit 5313c59
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Web Socket Example
# Collaborative Drawing

Shows how you can use the Python WebSocket library to Ypy exchange updates between two users.
![drawing-demo](https://user-images.githubusercontent.com/5553757/189190756-21c2bc61-1816-488e-b2cd-bc910fece6d9.gif)

A basic collaborative drawing application using Ypy and WebSockets. Left click on the canvas to leave a mark.

## Getting Started

1. Install Python dependencies:
Expand All @@ -10,10 +12,8 @@ Shows how you can use the Python WebSocket library to Ypy exchange updates betwe
pip install -r requirements.txt
```

2. Start server
2. Run the demo

```
python main.py <server-number>
python demo.py
```

3. TODO
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,30 @@

class YDocWSClient:

def __init__(self, uri = "ws://localhost:8765"):
def __init__(self, uri = "ws://localhost:7654"):
self.send_q = queue.Queue()
self.recv_q = queue.Queue()
self.uri = uri
def between_callback():
def async_loop():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

loop.run_until_complete(self.start_ws_client())
loop.close()
_thread = threading.Thread(target=between_callback)
_thread.start()



ws_thread = threading.Thread(target=async_loop, daemon=True)
ws_thread.start()

def send_updates(self, txn_event: Y.AfterTransactionEvent):
update = txn_event.get_update()
# Sometimes transactions don't write, which means updates are empty.
# We only care about updates with meaningful mutations.
if update != b'\x00\x00':
self.send_q.put_nowait(update)

def apply_updates(self, doc: Y.YDoc):
while not self.recv_q.empty():
update = self.recv_q.get_nowait()
Y.apply_update(doc, update)

def _send(self, thing):
self.send_q.put_nowait(thing)

async def client_handler(self, websocket):
consumer_task = asyncio.create_task(self.consumer_handler(websocket))
Expand All @@ -62,10 +58,4 @@ async def producer_handler(self, websocket):
async def start_ws_client(self):
async with websockets.connect(self.uri) as websocket:
await self.client_handler(websocket)


if __name__ == "__main__":
client = YDocWSClient()
while True:
sleep(1)
client._send("hello!")

33 changes: 33 additions & 0 deletions examples/drawing/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import subprocess
from typing import List


def demo():
"""
Spawns a server and two drawing clients.
"""
processes: List[subprocess.Popen] = []
# Server
processes.append(subprocess.Popen(["python", "server.py"]))

# Clients
for _ in range(2):
processes.append(subprocess.Popen(["python", "draw.py"]))


wait_until_done()

for p in processes:
p.kill()



def wait_until_done():
print("waiting")
while input("Enter 'q' to quit: ").lower() != 'q':
continue



if __name__ == "__main__":
demo()
10 changes: 8 additions & 2 deletions examples/websocket-provider/draw.py → examples/drawing/draw.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from turtle import position
from p5 import *
from y_py import YDoc, YArray, AfterTransactionEvent
from client import YDocWSClient
Expand All @@ -8,9 +9,13 @@


def setup():
"""
Initialization logic that runs before the `draw()` loop.
"""
global strokes
global doc
global client
title("Ypy Drawing Demo")
size(720, 480)
doc = YDoc(0)
strokes = doc.get_array("strokes")
Expand All @@ -19,9 +24,10 @@ def setup():





def draw():
"""
Handles user input and updates the canvas.
"""
global strokes
global doc
global client
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
y_py
websockets
glfw; sys_platform != 'win32'
numpy
vispy
p5
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async def server_handler(websocket):


async def main():
async with websockets.serve(server_handler, "localhost", 8765):
async with websockets.serve(server_handler, "localhost", 7654):
await asyncio.Future() # run forever

if __name__ == "__main__":
Expand Down
4 changes: 0 additions & 4 deletions examples/websocket-provider/config.json

This file was deleted.

16 changes: 0 additions & 16 deletions examples/websocket-provider/utils.py

This file was deleted.

0 comments on commit 5313c59

Please sign in to comment.