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


Python websocket.WebSocketServerProtocol方法代碼示例

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


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

示例1: __init__

# 需要導入模塊: from autobahn.twisted import websocket [as 別名]
# 或者: from autobahn.twisted.websocket import WebSocketServerProtocol [as 別名]
def __init__(self):
        websocket.WebSocketServerProtocol.__init__(self)
        self._app = None
        self._side = None
        self._did_allocate = False # only one allocate() per websocket
        self._listening = False
        self._did_claim = False
        self._nameplate_id = None
        self._did_release = False
        self._did_open = False
        self._mailbox = None
        self._mailbox_id = None
        self._did_close = False 
開發者ID:warner,項目名稱:magic-wormhole-mailbox-server,代碼行數:15,代碼來源:server_websocket.py

示例2: __init__

# 需要導入模塊: from autobahn.twisted import websocket [as 別名]
# 或者: from autobahn.twisted.websocket import WebSocketServerProtocol [as 別名]
def __init__(self, manager, port, secure=False, key_path=None, crt_path=None, unix_socket_path=None):
        self.manager = manager
        from twisted.internet import reactor, ssl

        from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol

        class MyServerProtocol(WebSocketServerProtocol):
            def onConnect(self, request):
                # log.info(self.factory)
                # log.info('Client connecting: {0}'.format(request.peer))
                pass

            def onOpen(self):
                log.info("WebSocket connection open")
                WebSocketServer.clients.append(self)

            def onMessage(self, payload, isBinary):
                if isBinary:
                    log.info(f"Binary message received: {len(payload)} bytes")
                else:
                    log.info(f"Text message received: {payload.decode('utf8')}")

            def onClose(self, wasClean, code, reason):
                log.info(f"WebSocket connection closed: {reason}")
                try:
                    WebSocketServer.clients.remove(self)
                except:
                    pass

        factory = WebSocketServerFactory()
        factory.setProtocolOptions(autoPingInterval=15, autoPingTimeout=5)
        factory.protocol = MyServerProtocol

        def reactor_run(reactor, factory, port, context_factory=None, unix_socket_path=None):
            if unix_socket_path:
                sock_file = Path(unix_socket_path)
                if sock_file.exists():
                    sock_file.unlink()
                reactor.listenUNIX(unix_socket_path, factory)
            else:
                if context_factory:
                    log.info("wss secure")
                    reactor.listenSSL(port, factory, context_factory)
                else:
                    log.info("ws unsecure")
                    reactor.listenTCP(port, factory)
            reactor.run(installSignalHandlers=0)

        if secure:
            context_factory = ssl.DefaultOpenSSLContextFactory(key_path, crt_path)
        else:
            context_factory = None

        reactor_thread = threading.Thread(
            target=reactor_run, args=(reactor, factory, port, context_factory, unix_socket_path), name="WebSocketThread"
        )
        reactor_thread.daemon = True
        reactor_thread.start() 
開發者ID:pajbot,項目名稱:pajbot,代碼行數:60,代碼來源:websocket.py


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