-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathconsumers.py
More file actions
35 lines (28 loc) · 1.18 KB
/
consumers.py
File metadata and controls
35 lines (28 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import json
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from ..constants import TRANSPORT_WS_PROTOCOL, WS_PROTOCOL
from .subscriptions import subscription_server
class GraphQLSubscriptionConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
self.connection_context = None
found_protocol = None
for protocol in [WS_PROTOCOL, TRANSPORT_WS_PROTOCOL]:
if protocol in self.scope["subprotocols"]:
found_protocol = protocol
break
if not found_protocol:
await self.close()
return
self.connection_context = await subscription_server.handle(
ws=self, request_context=self.scope
)
await self.accept(subprotocol=found_protocol)
async def disconnect(self, code):
if self.connection_context:
self.connection_context.socket_closed = True
await subscription_server.on_close(self.connection_context)
async def receive_json(self, content):
subscription_server.on_message(self.connection_context, content)
@classmethod
async def encode_json(cls, content):
return json.dumps(content)