def InitiateConnection(self):
        """Initiate the WebSocket connection."""
        utils.CheckPythonVersion(self._ignore_certs)
        utils.ValidateParameters(self._tunnel_target)
        self._ca_certs = utils.CheckCACertsFile(self._ignore_certs)

        self._connect_url = utils.CreateWebSocketUrl(CONNECT_ENDPOINT,
                                                     self._tunnel_target)
        headers = [
            'User-Agent: ' + http.MakeUserAgentString(),
            'Sec-WebSocket-Protocol: ' + utils.SUBPROTOCOL_NAME
        ]
        if self._access_token:
            headers += ['Authorization: Bearer ' + self._access_token]
        log.info('Connecting to with URL %r', self._connect_url)
        self._websocket_errors = []
        self._connection_sid = None

        if log.GetVerbosity() == logging.DEBUG:
            websocket.enableTrace(True)
        else:
            websocket_logger = logging.getLogger('websocket')
            websocket_logger.setLevel(logging.CRITICAL)

        self._websocket = websocket.WebSocketApp(self._connect_url,
                                                 header=headers,
                                                 on_error=self._OnError,
                                                 on_close=self._OnClose,
                                                 on_data=self._OnData)
        log.info('Starting WebSocket receive thread.')
        self._websocket_thread = threading.Thread(
            target=self._ReceiveFromWebSocket)
        self._websocket_thread.daemon = True
        self._websocket_thread.start()
    def InitiateConnection(self):
        """Initiate the WebSocket connection."""
        utils.CheckPythonVersion(self._ignore_certs)
        utils.ValidateParameters(self._tunnel_target)

        self._StartNewWebSocket()
        self._WaitForOpenOrRaiseError()
        self._send_and_reconnect_thread = threading.Thread(
            target=self._SendDataAndReconnectWebSocket)
        self._send_and_reconnect_thread.daemon = True
        self._send_and_reconnect_thread.start()
示例#3
0
 def testInit_MissingTargetParam(self, target, expected_error,
                                 expected_message):
     tunnel_target = utils.IapTunnelTargetInfo(*target)
     with self.AssertRaisesExceptionMatches(expected_error,
                                            expected_message):
         utils.ValidateParameters(tunnel_target)