def test_send_message_without_masking(self): tm = TextMessage(b'hello world') m = MagicMock() ws = WebSocket(sock=m) ws.send(tm) m.sendall.assert_called_once_with(tm.single())
def test_send_bytes_with_masking(self): tm = TextMessage(b'hello world').single(mask=True) m = MagicMock() ws = WebSocket(sock=m) ws.stream = MagicMock() ws.stream.always_mask = True ws.stream.text_message.return_value.single.return_value = tm ws.send(b'hello world') m.sendall.assert_called_once_with(tm)
def join_queue(socket:WebSocket, data): # keep this order to avoid state conflict channel, pubsub = pub_sub_pool.join() r_queue.put(channel) # {'pattern': None, 'type': 'message', 'data': b'30ae154a-2397-4945-aeed-48dad6c603b6', 'channel': 'queue_channel:19'} msg = pub_sub_pool.next_message(channel, pubsub) uid = msg['data'] if not uid in games: games[uid] = make_game_engine() games[uid].join_game(data["player"]) socket.send(uid)
def test_send_generator_without_masking(self): tm0 = b'hello' tm1 = b'world' def datasource(): yield tm0 yield tm1 gen = datasource() m = MagicMock() ws = WebSocket(sock=m) ws.send(gen) self.assertEqual(m.sendall.call_count, 2) self.assertRaises(StopIteration, next, gen)
def send(self, **message): message = {k: v for k, v in message.items() if v is not None} if "data" in message and "client" in message: fingerprint = _fingerprint(message["data"]) client, callback = message["client"], message.get("callback") repeat_send = callback in self.cached_fingerprints[client] cached_fingerprint = self.cached_fingerprints[client].get(callback) self.cached_fingerprints[client][callback] = fingerprint if cached_fingerprint == fingerprint and repeat_send: return self.log.debug("sending {}", message) message = json.dumps(message, cls=sideboard.lib.serializer, separators=(",", ":"), sort_keys=True) with self.send_lock: WebSocket.send(self, message)
def send(self, **message): message = {k: v for k, v in message.items() if v is not None} if 'data' in message and 'client' in message: fingerprint = _fingerprint(message['data']) client, callback = message['client'], message.get('callback') repeat_send = callback in self.cached_fingerprints[client] cached_fingerprint = self.cached_fingerprints[client].get(callback) self.cached_fingerprints[client][callback] = fingerprint if cached_fingerprint == fingerprint and repeat_send: return log.debug('sending {}', message) message = json.dumps(message, cls=sideboard.lib.serializer, separators=(',', ':'), sort_keys=True) with self.send_lock: WebSocket.send(self, message)
def send(self, **message): """ This overrides the ws4py-provided send to implement three new features: 1) Instead of taking a string, this method treats its keyword arguments as the message, serializes them to JSON, and sends that. 2) For subscription responses, we keep track of the most recent response we sent for the given subscription. If neither the request or response have changed since the last time we pushed data back to the client for this subscription, we don't send anything. 3) We lock when sending to ensure that our sends are thread-safe. Surprisingly, the "ws4py.threadedclient" class isn't thread-safe! 4) Subscriptions firing will sometimes trigger a send on a websocket which has already been marked as closed. When this happens we log a debug message and then exit without error. """ if self.is_closed: log.debug('ignoring send on an already closed websocket: {}', message) self.unsubscribe_all() return message = {k: v for k, v in message.items() if v is not None} if 'data' in message and 'client' in message: fingerprint = _fingerprint(message['data']) client, callback = message['client'], message.get('callback') repeat_send = callback in self.cached_fingerprints[client] cached_fingerprint = self.cached_fingerprints[client].get(callback) self.cached_fingerprints[client][callback] = fingerprint if cached_fingerprint == fingerprint and repeat_send: return log.debug('sending {}', message) message = json.dumps(message, cls=sideboard.lib.serializer, separators=(',', ':'), sort_keys=True) with self.send_lock: if not self.is_closed: WebSocket.send(self, message)
def send(self, payload, binary=False): WebSocket.send(self, payload, binary) sleep(2)
def send(self, *a, **k): with self.widget_wslock: WebSocket.send(self, *a, **k, binary=isinstance(a[0], bytes))