Browse Source

auto commit

master
jfinn 3 years ago
parent
commit
01204fbae6
2 changed files with 43 additions and 1 deletions
  1. 25
    1
      djc_srv/asgi.py
  2. 18
    0
      wsps/consumers.py

+ 25
- 1
djc_srv/asgi.py View File

@@ -11,6 +11,30 @@ import os
11 11
 
12 12
 from django.core.asgi import get_asgi_application
13 13
 
14
+from channels.auth import AuthMiddlewareStack
15
+from channels.routing import ProtocolTypeRouter, URLRouter
16
+from channels.security.websocket import AllowedHostsOriginValidator
17
+
18
+import wsps.routing
19
+
20
+
14 21
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djc_srv.settings')
15 22
 
16
-application = get_asgi_application()
23
+# application = get_asgi_application()
24
+
25
+
26
+application = ProtocolTypeRouter({
27
+    "http": get_asgi_application(),
28
+    # Just HTTP for now. (We can add other protocols later.)
29
+
30
+  "websocket": AllowedHostsOriginValidator(
31
+        AuthMiddlewareStack(
32
+            URLRouter(
33
+                wsps.routing.websocket_urlpatterns
34
+            )
35
+        )
36
+    ),
37
+
38
+})
39
+
40
+

+ 18
- 0
wsps/consumers.py View File

@@ -0,0 +1,18 @@
1
+# chat/consumers.py
2
+import json
3
+from channels.generic.websocket import WebsocketConsumer
4
+
5
+class ChatConsumer(WebsocketConsumer):
6
+    def connect(self):
7
+        self.accept()
8
+
9
+    def disconnect(self, close_code):
10
+        pass
11
+
12
+    def receive(self, text_data):
13
+        text_data_json = json.loads(text_data)
14
+        message = text_data_json['message']
15
+
16
+        self.send(text_data=json.dumps({
17
+            'message': message
18
+        }))

Loading…
Cancel
Save