backend that supports asgi and status update sockets with channels

This commit is contained in:
Jonas Winkler
2020-11-07 11:30:45 +01:00
parent e45208bf01
commit 572e40ca27
7 changed files with 613 additions and 92 deletions

37
src/paperless/asgi.py Normal file
View File

@@ -0,0 +1,37 @@
import json
import os
from asgiref.sync import async_to_sync
from channels.auth import AuthMiddlewareStack
from channels.generic.websocket import WebsocketConsumer
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from django.urls import re_path
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'paperless.settings')
class StatusConsumer(WebsocketConsumer):
def connect(self):
self.accept()
async_to_sync(self.channel_layer.group_add)('status_updates', self.channel_name)
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)('status_updates', self.channel_name)
def status_update(self, event):
self.send(json.dumps(event['data']))
websocket_urlpatterns = [
re_path(r'ws/status/$', StatusConsumer.as_asgi()),
]
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
)
),
})