-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip(sync): websocket server with ASGI and PostgreSQL LISTEN/NOTIFY
- Loading branch information
1 parent
c6c965a
commit f78e16d
Showing
9 changed files
with
239 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import os | ||
|
||
from channels.routing import ProtocolTypeRouter, URLRouter | ||
from channels.security.websocket import AllowedHostsOriginValidator | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "umap.settings") | ||
|
||
from django.core.asgi import get_asgi_application | ||
from django.urls import re_path | ||
|
||
from .sync import consumers | ||
from .sync.app import application as ws_application | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "umap.settings") | ||
# Initialize Django ASGI application early to ensure the AppRegistry | ||
# is populated before importing code that may import ORM models. | ||
django_asgi_app = get_asgi_application() | ||
|
||
urlpatterns = (re_path(r"ws/sync/(?P<map_id>\w+)/$", consumers.SyncConsumer.as_asgi()),) | ||
|
||
application = ProtocolTypeRouter( | ||
{ | ||
"http": django_asgi_app, | ||
"websocket": AllowedHostsOriginValidator(URLRouter(urlpatterns)), | ||
} | ||
) | ||
async def application(scope, receive, send): | ||
if scope["type"] == "http": | ||
await django_asgi_app(scope, receive, send) | ||
elif scope["type"] == "websocket": | ||
await ws_application(scope, receive, send) | ||
else: | ||
raise NotImplementedError(f"Unknown scope type {scope['type']}") |
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
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,45 @@ | ||
import uuid | ||
|
||
from django.urls.resolvers import RoutePattern | ||
|
||
ws_pattern = RoutePattern("/ws/sync/<str:map_id>") | ||
|
||
|
||
async def application(scope, receive, send): | ||
from .models import Peer | ||
|
||
matched = ws_pattern.match(scope["path"]) | ||
print(matched) | ||
if not matched: | ||
print("Wrong path") | ||
return | ||
_, _, kwargs = matched | ||
|
||
map_id = kwargs["map_id"] | ||
room_id = f"room{map_id}" | ||
peer = await Peer.objects.acreate(uuid=uuid.uuid4(), name="FooBar", room_id=room_id) | ||
print(peer) | ||
peer._send = send | ||
while True: | ||
event = await receive() | ||
print("EVENT", event) | ||
|
||
if event["type"] == "websocket.connect": | ||
try: | ||
print("Let's accept") | ||
await send({"type": "websocket.accept"}) | ||
await peer.connect() | ||
except ValueError: | ||
await send({"type": "websocket.close"}) | ||
|
||
if event["type"] == "websocket.disconnect": | ||
print("Closing", event) | ||
await peer.disconnect() | ||
print("Closed") | ||
break | ||
|
||
if event["type"] == "websocket.receive": | ||
if event["text"] == "ping": | ||
await send({"type": "websocket.send", "text": "pong"}) | ||
else: | ||
await peer.receive(event["text"]) |
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class UmapConfig(AppConfig): | ||
name = "umap.sync" | ||
verbose_name = "uMap Sync" |
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
# Generated by Django 5.1.4 on 2024-12-27 16:14 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Peer", | ||
fields=[ | ||
( | ||
"uuid", | ||
models.UUIDField(primary_key=True, serialize=False, unique=True), | ||
), | ||
("name", models.CharField(max_length=200)), | ||
("room_id", models.CharField(max_length=200)), | ||
], | ||
), | ||
] |
Empty file.
Oops, something went wrong.