-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrealtimemsg.py
47 lines (39 loc) · 1.46 KB
/
realtimemsg.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
#!/usr/bin/env python
# coding: utf-8
# A restful HTTP API for ansible
# Base on ansible-runner and sanic
# Github <https://github.com/lfbear/ansible-api>
# Author: lfbear
import json
from .report import Reporter
from .tool import Tool
class RealTimeMessage:
UserList = {}
@staticmethod
def set(group, handler):
if group not in RealTimeMessage.UserList:
RealTimeMessage.UserList[group] = []
RealTimeMessage.UserList[group].append(handler)
Tool.LOGGER.debug('new connection from WebSocket [%s]' % group)
return True
@staticmethod
def get(group):
if group in RealTimeMessage.UserList:
return RealTimeMessage.UserList[group]
else:
return []
@staticmethod
async def send(data):
group, msg = Reporter.fmt_realtime(data)
Tool.LOGGER.debug('[%s@websocket] %s' % (group, msg))
if group in RealTimeMessage.UserList:
for ws in RealTimeMessage.UserList.get(group, []):
if ws.open:
try:
await ws.send(json.dumps(msg))
# FOR websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006
except BaseException as e:
Tool.LOGGER.exception(e)
else:
# WSUser.UserList = [u for u in WSUser.UserList if u != x]
RealTimeMessage.UserList.get(group).remove(ws)