當前位置: 首頁>>代碼示例>>Python>>正文


Python WebSocketManager.broadcast方法代碼示例

本文整理匯總了Python中ws4py.manager.WebSocketManager.broadcast方法的典型用法代碼示例。如果您正苦於以下問題:Python WebSocketManager.broadcast方法的具體用法?Python WebSocketManager.broadcast怎麽用?Python WebSocketManager.broadcast使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ws4py.manager.WebSocketManager的用法示例。


在下文中一共展示了WebSocketManager.broadcast方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_broadcast

# 需要導入模塊: from ws4py.manager import WebSocketManager [as 別名]
# 或者: from ws4py.manager.WebSocketManager import broadcast [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')
開發者ID:17dakmue,項目名稱:WebSocket-for-Python,代碼行數:11,代碼來源:test_manager.py

示例2: test_broadcast_failure_must_not_break_caller

# 需要導入模塊: from ws4py.manager import WebSocketManager [as 別名]
# 或者: from ws4py.manager.WebSocketManager import broadcast [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")
開發者ID:17dakmue,項目名稱:WebSocket-for-Python,代碼行數:14,代碼來源:test_manager.py

示例3: WebSocketPlugin

# 需要導入模塊: from ws4py.manager import WebSocketManager [as 別名]
# 或者: from ws4py.manager.WebSocketManager import broadcast [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)
開發者ID:Lawouach,項目名稱:WebSocket-for-Python,代碼行數:47,代碼來源:cherrypyserver.py


注:本文中的ws4py.manager.WebSocketManager.broadcast方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。