本文整理汇总了Python中ws4py.manager.WebSocketManager.add方法的典型用法代码示例。如果您正苦于以下问题:Python WebSocketManager.add方法的具体用法?Python WebSocketManager.add怎么用?Python WebSocketManager.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ws4py.manager.WebSocketManager
的用法示例。
在下文中一共展示了WebSocketManager.add方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_websocket_close_all
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [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 add [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 add [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: test_broadcast
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [as 别名]
def test_broadcast(self, MockSelectPoller):
m = WebSocketManager(poller=MockSelectPoller())
ws = MagicMock()
ws.terminated = False
m.add(ws)
m.broadcast(b'hello there')
ws.send.assert_call_once_with(b'hello there')
示例5: test_cannot_add_websocket_more_than_once
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [as 别名]
def test_cannot_add_websocket_more_than_once(self, MockSelectPoller):
m = WebSocketManager(poller=MockSelectPoller())
ws = MagicMock()
ws.sock.fileno.return_value = 1
m.add(ws)
self.assertEqual(len(m), 1)
m.add(ws)
self.assertEqual(len(m), 1)
示例6: test_add_and_remove_websocket
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [as 别名]
def test_add_and_remove_websocket(self, MockSelectPoller):
m = WebSocketManager(poller=MockSelectPoller())
ws = MagicMock()
ws.sock.fileno.return_value = 1
m.add(ws)
m.poller.register.assert_call_once_with(ws)
m.remove(ws)
m.poller.unregister.assert_call_once_with(ws)
示例7: test_broadcast_failure_must_not_break_caller
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [as 别名]
def test_broadcast_failure_must_not_break_caller(self, MockSelectPoller):
m = WebSocketManager(poller=MockSelectPoller())
ws = MagicMock()
ws.terminated = False
ws.send.side_effect = RuntimeError
m.add(ws)
try:
m.broadcast(b'hello there')
except:
self.fail("Broadcasting shouldn't have failed")
示例8: test_websocket_terminated_from_mainloop
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [as 别名]
def test_websocket_terminated_from_mainloop(self, MockSelectPoller):
m = WebSocketManager(poller=MockSelectPoller())
m.poller.poll.return_value = [1]
ws = MagicMock()
ws.terminated = False
ws.sock.fileno.return_value = 1
ws.once.return_value = False
m.add(ws)
m.start()
ws.terminate.assert_call_once_with()
m.stop()
示例9: WebSocketPlugin
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [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)
示例10: test_cannot_remove_unregistered_websocket
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [as 别名]
def test_cannot_remove_unregistered_websocket(self, MockSelectPoller):
m = WebSocketManager(poller=MockSelectPoller())
ws = MagicMock()
ws.sock.fileno.return_value = 1
m.remove(ws)
self.assertEqual(len(m), 0)
self.assertFalse(m.poller.unregister.called)
m.add(ws)
self.assertEqual(len(m), 1)
m.remove(ws)
self.assertEqual(len(m), 0)
m.poller.unregister.assert_call_once_with(ws)
m.poller.reset_mock()
m.remove(ws)
self.assertEqual(len(m), 0)
self.assertFalse(m.poller.unregister.called)
示例11: FileServer
# 需要导入模块: from ws4py.manager import WebSocketManager [as 别名]
# 或者: from ws4py.manager.WebSocketManager import add [as 别名]
class FileServer(object):
""" Serves static files from a directory.
"""
def __init__(self, path):
""" path is directory where static files are stored
"""
self.path = path
self.wsapp = WebSocketWSGIApplication(handler_cls=EchoWebSocket)
self.manager = WebSocketManager()
self.manager.start()
def __call__(self, environ, start_response):
""" WSGI entry point
"""
# Upgrade header means websockets...
upgrade_header = environ.get('HTTP_UPGRADE', '').lower()
if upgrade_header:
environ['ws4py.socket'] = get_connection(environ['wsgi.input'])
# This will make a websocket, hopefully!
ret = self.wsapp(environ, start_response)
if 'ws4py.websocket' in environ:
self.manager.add(environ.pop('ws4py.websocket'))
return ret
# Find path to file to server
path_info = environ["PATH_INFO"]
if not path_info or path_info == "/":
path_info = "/index.html"
file_path = os.path.join(self.path, path_info[1:])
# If file does not exist, return 404
if not os.path.exists(file_path):
return self._not_found(start_response)
# Guess mimetype of file based on file extension
mimetype = mimetypes.guess_type(file_path)[0]
# If we can't guess mimetype, return a 403 Forbidden
if mimetype is None:
return self._forbidden(start_response)
# Get size of file
size = os.path.getsize(file_path)
# Create headers and start response
headers = [
("Content-type", mimetype),
("Content-length", str(size)),
]
start_response("200 OK", headers)
# Send file
return self._send_file(file_path, size)
def _send_file(self, file_path, size):
""" A generator function which returns the blocks in a file, one at
a time.
"""
with open(file_path) as f:
block = f.read(BLOCK_SIZE)
while block:
yield block
block = f.read(BLOCK_SIZE)
def _not_found(self, start_response):
start_response("404 NOT FOUND", [("Content-type", "text/plain")])
return ["Not found", ]
def _forbidden(self, start_response):
start_response("403 FORBIDDEN", [("Content-type", "text/plain")])
return ["Forbidden", ]