-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d153d53
commit 96b9dda
Showing
5 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ test = [ | |
"pytest", | ||
"pytest-asyncio", | ||
"websockets >=10.0", | ||
"uvicorn", | ||
] | ||
|
||
[project.urls] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import asyncio | ||
|
||
import pytest | ||
import uvicorn | ||
import y_py as Y | ||
from websockets import connect # type: ignore | ||
|
||
from ypy_websocket import ASGIServer, WebsocketProvider, WebsocketServer | ||
|
||
websocket_server = WebsocketServer(auto_clean_rooms=False) | ||
app = ASGIServer(websocket_server) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_asgi(unused_tcp_port): | ||
# server | ||
config = uvicorn.Config("test_asgi:app", port=unused_tcp_port, log_level="info") | ||
server = uvicorn.Server(config) | ||
server_task = asyncio.create_task(server.serve()) | ||
while not server.started: | ||
await asyncio.sleep(0) | ||
|
||
# clients | ||
# client 1 | ||
ydoc1 = Y.YDoc() | ||
ymap1 = ydoc1.get_map("map") | ||
with ydoc1.begin_transaction() as t: | ||
ymap1.set(t, "key", "value") | ||
async with connect(f"ws://localhost:{unused_tcp_port}/my-roomname") as websocket1: | ||
WebsocketProvider(ydoc1, websocket1) | ||
await asyncio.sleep(0.1) | ||
|
||
# client 2 | ||
ydoc2 = Y.YDoc() | ||
async with connect(f"ws://localhost:{unused_tcp_port}/my-roomname") as websocket2: | ||
WebsocketProvider(ydoc2, websocket2) | ||
await asyncio.sleep(0.1) | ||
|
||
ymap2 = ydoc2.get_map("map") | ||
assert ymap2.to_json() == '{"key":"value"}' | ||
|
||
server_task.cancel() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Any, Awaitable, Callable, Dict | ||
|
||
from .websocket_server import WebsocketServer | ||
|
||
|
||
class WebSocket: | ||
def __init__( | ||
self, | ||
receive: Callable[[], Awaitable[Dict[str, Any]]], | ||
send: Callable[[Dict[str, Any]], Awaitable[None]], | ||
path: str, | ||
): | ||
self._receive = receive | ||
self._send = send | ||
self._path = path | ||
|
||
@property | ||
def path(self) -> str: | ||
return self._path | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self) -> bytes: | ||
return await self.recv() | ||
|
||
async def send(self, message: bytes) -> None: | ||
await self._send( | ||
dict( | ||
type="websocket.send", | ||
bytes=message, | ||
) | ||
) | ||
|
||
async def recv(self) -> bytes: | ||
message = await self._receive() | ||
if message["type"] == "websocket.receive": | ||
return message["bytes"] | ||
if message["type"] == "websocket.disconnect": | ||
raise StopAsyncIteration() | ||
return b"" | ||
|
||
|
||
class Server: | ||
def __init__(self, websocket_server: WebsocketServer): | ||
self._websocket_server = websocket_server | ||
|
||
async def __call__( | ||
self, | ||
scope: Dict[str, Any], | ||
receive: Callable[[], Awaitable[Dict[str, Any]]], | ||
send: Callable[[Dict[str, Any]], Awaitable[None]], | ||
): | ||
msg = await receive() | ||
if msg["type"] == "websocket.connect": | ||
await send({"type": "websocket.accept"}) | ||
websocket = WebSocket(receive, send, scope["path"]) | ||
await self._websocket_server.serve(websocket) |