本文整理汇总了Python中ws4py.manager.WebSocketManager.close_all方法的典型用法代码示例。如果您正苦于以下问题:Python WebSocketManager.close_all方法的具体用法?Python WebSocketManager.close_all怎么用?Python WebSocketManager.close_all使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ws4py.manager.WebSocketManager
的用法示例。
在下文中一共展示了WebSocketManager.close_all方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_websocket_close_all
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import close_all [as 别名]
def test_websocket_close_all(self, MockSelectPoller):
m = WebSocketManager(poller=MockSelectPoller())
ws = MagicMock()
m.add(ws)
m.close_all()
ws.terminate.assert_call_once_with(1001, 'Server is shutting down')
示例2: WSGIServer
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import close_all [as 别名]
class WSGIServer(_WSGIServer):
def initialize_websockets_manager(self):
"""
Call thos to start the underlying websockets
manager. Make sure to call it once your server
is created.
"""
self.manager = WebSocketManager()
self.manager.start()
def shutdown_request(self, request):
"""
The base class would close our socket
if we didn't override it.
"""
pass
def link_websocket_to_server(self, ws):
"""
Call this from your WSGI handler when a websocket
has been created.
"""
self.manager.add(ws)
def server_close(self):
"""
Properly initiate closing handshakes on
all websockets when the WSGI server terminates.
"""
if hasattr(self, 'manager'):
self.manager.close_all()
self.manager.stop()
self.manager.join()
delattr(self, 'manager')
_WSGIServer.server_close(self)
示例3: test_websocket_close_all
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import close_all [as 别名]
def test_websocket_close_all(self, MockSelectPoller):
m = WebSocketManager(poller=MockSelectPoller())
ws = MagicMock()
m.add(ws)
m.close_all()
ws.close.assert_called_once_with(code=1001, reason='Server is shutting down')
示例4: WebSocketPlugin
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import close_all [as 别名]
class WebSocketPlugin(plugins.SimplePlugin):
def __init__(self, bus):
plugins.SimplePlugin.__init__(self, bus)
self.manager = WebSocketManager()
def start(self):
self.bus.log("Starting WebSocket processing")
self.bus.subscribe('stop', self.cleanup)
self.bus.subscribe('handle-websocket', self.handle)
self.bus.subscribe('websocket-broadcast', self.broadcast)
self.manager.start()
def stop(self):
self.bus.log("Terminating WebSocket processing")
self.bus.unsubscribe('stop', self.cleanup)
self.bus.unsubscribe('handle-websocket', self.handle)
self.bus.unsubscribe('websocket-broadcast', self.broadcast)
def handle(self, ws_handler, peer_addr):
"""
Tracks the provided handler.
:param ws_handler: websocket handler instance
:param peer_addr: remote peer address for tracing purpose
"""
self.manager.add(ws_handler)
def cleanup(self):
"""
Terminate all connections and clear the pool. Executed when the engine stops.
"""
self.manager.close_all()
self.manager.stop()
self.manager.join()
def broadcast(self, message, binary=False):
"""
Broadcasts a message to all connected clients known to
the server.
:param message: a message suitable to pass to the send() method
of the connected handler.
:param binary: whether or not the message is a binary one
"""
self.manager.broadcast(message, binary)
示例5: run
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import close_all [as 别名]
def run(script_options):
global logger
level = logging.DEBUG if script_options.verbose else logging.INFO
logger = configure_logger(level = level)
mgr = WebSocketManager()
try:
mgr.start()
clients = []
# Connect
for connection_idx in range(script_options.concurrency):
client = EchoClient(script_options.url, mgr,
script_options.ca, script_options.key, script_options.cert)
client.connect()
clients.append(client)
logger.info("%d clients are connected" % (connection_idx + 1))
# Send
msg = getMessage(script_options)
if msg:
msg = json.write(msg)
logger.info("Sending messages (num=%d):\n%s", script_options.num, msg)
for client in clients:
for _ in range(script_options.num):
client.send(msg)
time.sleep(SEND_INTERVAL)
logger.info("Done sending")
# Sleep before disconnecting
logger.info("Sleeping for %d s before disconnecting",
script_options.interval)
time.sleep(script_options.interval)
except KeyboardInterrupt:
logger.info("Interrupted by user")
finally:
logger.info("Disconnecting!")
mgr.close_all(code = 1000,
message = "Client is closing the connection")
mgr.stop()
mgr.join()
示例6: tempread
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import close_all [as 别名]
for pin in pins:
gpio.pinMode(pin, gpio.OUTPUT)
gpio.digitalWrite(pin,int(pins[pin]))
def tempread(pin):
value = analog_read(pin)
tempr = (value * 3.3)/4096*100
return tempr
if __name__ == '__main__':
import time
try:
m.start()
client = EchoClient('ws://tairy.me:8888/ws')
client.connect()
sendmes = {"userid":userid}
while True:
for ws in m.websockets.itervalues():
if not ws.terminated:
sendmes["data"] = {"temp":tempread(2)}
ws.send(json.JSONEncoder().encode(sendmes))
break
else:
break
time.sleep(3)
except KeyboardInterrupt:
m.close_all()
m.stop()
m.join()