本文整理汇总了Python中ws4py.websocket.WebSocket.send方法的典型用法代码示例。如果您正苦于以下问题:Python WebSocket.send方法的具体用法?Python WebSocket.send怎么用?Python WebSocket.send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ws4py.websocket.WebSocket
的用法示例。
在下文中一共展示了WebSocket.send方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_send_message_without_masking
# 需要导入模块: from ws4py.websocket import WebSocket [as 别名]
# 或者: from ws4py.websocket.WebSocket import send [as 别名]
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())
示例2: test_send_bytes_with_masking
# 需要导入模块: from ws4py.websocket import WebSocket [as 别名]
# 或者: from ws4py.websocket.WebSocket import send [as 别名]
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)
示例3: join_queue
# 需要导入模块: from ws4py.websocket import WebSocket [as 别名]
# 或者: from ws4py.websocket.WebSocket import send [as 别名]
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)
示例4: test_send_generator_without_masking
# 需要导入模块: from ws4py.websocket import WebSocket [as 别名]
# 或者: from ws4py.websocket.WebSocket import send [as 别名]
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)
示例5: send
# 需要导入模块: from ws4py.websocket import WebSocket [as 别名]
# 或者: from ws4py.websocket.WebSocket import send [as 别名]
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)
示例6: send
# 需要导入模块: from ws4py.websocket import WebSocket [as 别名]
# 或者: from ws4py.websocket.WebSocket import send [as 别名]
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)
示例7: send
# 需要导入模块: from ws4py.websocket import WebSocket [as 别名]
# 或者: from ws4py.websocket.WebSocket import send [as 别名]
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)
示例8: send
# 需要导入模块: from ws4py.websocket import WebSocket [as 别名]
# 或者: from ws4py.websocket.WebSocket import send [as 别名]
def send(self, payload, binary=False):
WebSocket.send(self, payload, binary)
sleep(2)