-
Notifications
You must be signed in to change notification settings - Fork 11
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
4b1bffd
commit 3a74f65
Showing
3 changed files
with
46 additions
and
30 deletions.
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 |
---|---|---|
@@ -1,39 +1,22 @@ | ||
A server connects multiple `YDoc` through a [WebsocketServer](../reference/WebSocket_server.md). | ||
A server connects multiple `Doc` through a [WebsocketServer](../reference/WebSocket_server.md). | ||
|
||
Here is a code example using the [websockets](https://websockets.readthedocs.io) library: | ||
Pycrdt-websocket can be used with an [ASGI](https://asgi.readthedocs.io) server. Here is a code example using [Hypercorn](https://hypercorn.readthedocs.io): | ||
```py | ||
import asyncio | ||
from websockets import serve | ||
from pycrdt_websocket import WebsocketServer | ||
|
||
async def server(): | ||
async with ( | ||
WebsocketServer() as websocket_server, | ||
serve(websocket_server.serve, "localhost", 1234), | ||
): | ||
await asyncio.Future() # run forever | ||
|
||
asyncio.run(server()) | ||
``` | ||
Pycrdt-websocket can also be used with an [ASGI](https://asgi.readthedocs.io) server. Here is a code example using [Uvicorn](https://www.uvicorn.org): | ||
```py | ||
# main.py | ||
import asyncio | ||
import uvicorn | ||
from hypercorn import Config | ||
from hypercorn.asyncio import serve | ||
from pycrdt_websocket import ASGIServer, WebsocketServer | ||
|
||
websocket_server = WebsocketServer() | ||
app = ASGIServer(websocket_server) | ||
|
||
async def main(): | ||
config = uvicorn.Config("main:app", port=5000, log_level="info") | ||
server = uvicorn.Server(config) | ||
websocket_server = WebsocketServer() | ||
app = ASGIServer(websocket_server) | ||
config = Config() | ||
config.bind = ["localhost:1234"] | ||
async with websocket_server: | ||
task = asyncio.create_task(server.serve()) | ||
while not server.started: | ||
await asyncio.sleep(0) | ||
|
||
await asyncio.Future() # run forever | ||
await serve(app, config, mode="asgi") | ||
|
||
asyncio.run(main()) | ||
``` |
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