def pub(cls, cont, str_data): room = cls.room.get(cont.id, []) for ws_id in room: ws = WebSocketServer.get_websocket_by_id(ws_id) if ws is None: room.remove(ws_id) else: ws.send(str_data)
def sub(userws, controller_id): try: cont = Controller.objects.get(id=int(controller_id)) except Controller.DoesNotExist: data = {'error': {'code': 404, 'msg': 'controller with id {} doesnot exist'.format(controller_id)}} print(json.dumps(data)) ws = WebSocketServer.get_websocket_by_id(userws) if ws is not None: ws.send(json.dumps(data)) return room = ControllerPubSub.room.get(int(controller_id)) if room is None: room = [] room.append(userws) ControllerPubSub.room[int(controller_id)] = room print(ControllerPubSub.room) data = ControllerSerializer(cont).data data['sub'] = True data['type'] = 'update' ws = WebSocketServer.get_websocket_by_id(userws) if ws is not None: ws.send(json.dumps(data)) print(json.dumps(data))
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.websocket = WebSocketServer.get_websocket_by_id(self.ws_id) self.online = self.websocket is not None
def publish(self, message): for user in self.users.all(): ws = WebSocketServer.get_websocket_by_id(user.ws_id) ws.send(message)