-
Notifications
You must be signed in to change notification settings - Fork 88
/
websockets_lib.py
47 lines (36 loc) · 1.41 KB
/
websockets_lib.py
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
36
37
38
39
40
41
42
43
44
45
46
47
import json
from asyncio import shield
from websockets import ConnectionClosed
from .base import ConnectionClosedException
from .base_async import BaseAsyncConnectionContext, BaseAsyncSubscriptionServer
class WsLibConnectionContext(BaseAsyncConnectionContext):
async def receive(self):
try:
msg = await self.ws.recv()
return msg
except ConnectionClosed:
raise ConnectionClosedException()
async def send(self, data):
if self.closed:
return
await self.ws.send(json.dumps(data))
@property
def closed(self):
return self.ws.open is False
async def close(self, code):
await self.ws.close(code)
class WsLibSubscriptionServer(BaseAsyncSubscriptionServer):
async def _handle(self, ws, request_context):
connection_context = WsLibConnectionContext(ws, request_context)
await self.on_open(connection_context)
while True:
try:
if connection_context.closed:
raise ConnectionClosedException()
message = await connection_context.receive()
except ConnectionClosedException:
break
self.on_message(connection_context, message)
await self.on_close(connection_context)
async def handle(self, ws, request_context=None):
await shield(self._handle(ws, request_context), loop=self.loop)