def recv(self):

        if ExceptionThrowingWS.exceptions < self.times_to_except:
            ExceptionThrowingWS.exceptions += 1
            time.sleep(1)
            raise websocket.WebSocketConnectionClosedException()

        else:
            return super(ExceptionThrowingWS, self).recv()
Exemplo n.º 2
0
    def recv(self, *args, **kwargs):
        if not self.logged_in:
            self.logged_in = True
            return FAKE_CONTENT

        for i in range(10):
            if self.stopped:
                raise websocket.WebSocketConnectionClosedException()
            time.sleep(1)
    def receive_server_message(self):
        server_msg = ServerMsg()
        opcode, data = self._connection.recv_data()
        if opcode == websocket.ABNF.OPCODE_TEXT:
            raise Exception("Received unexpected text message from WebAPI server")
        elif opcode == websocket.ABNF.OPCODE_CLOSE:
            raise websocket.WebSocketConnectionClosedException(
                "Can't receive message - WebAPI server closed connection")

        server_msg.ParseFromString(data)

        if self._need_to_log:
            print("Server message received:\n" + str(server_msg))
        return server_msg
Exemplo n.º 4
0
    def send(self, data, opcode=None):
        """
        send message.
        data: message to send. If you set opcode to OPCODE_TEXT, data must be utf-8 string or unicode.
        opcode: operation code of data. default is OPCODE_TEXT.
        """

        if opcode is None:
            opcode = websocket.ABNF.OPCODE_TEXT

        # encode JSON
        data = json.dumps(data)

        if self.sock.send(data, opcode) == 0:
            raise websocket.WebSocketConnectionClosedException()
Exemplo n.º 5
0
    def recv_raw(self, timeout, opcodes, **kwargs):
        """this is very internal, it will return the raw opcode and data if they
        match the passed in opcodes"""
        orig_timeout = self.get_timeout(timeout)
        timeout = orig_timeout

        while timeout > 0.0:
            start = time.time()
            if not self.connected: self.connect(timeout=timeout, **kwargs)
            with self.wstimeout(timeout, **kwargs) as timeout:
                logger.debug('{} waiting to receive for {} seconds'.format(
                    self.client_id, timeout))
                try:
                    opcode, data = self.ws.recv_data()
                    if opcode in opcodes:
                        timeout = 0.0
                        break

                    else:
                        if opcode == websocket.ABNF.OPCODE_CLOSE:
                            raise websocket.WebSocketConnectionClosedException(
                            )

                except websocket.WebSocketTimeoutException:
                    pass

                except websocket.WebSocketConnectionClosedException:
                    # bug in Websocket.recv_data(), this should be done by Websocket
                    try:
                        self.ws.shutdown()
                    except AttributeError:
                        pass
                    #raise EOFError("websocket closed by server and reconnection did nothing")

            if timeout:
                stop = time.time()
                timeout -= (stop - start)

            else:
                break

        if timeout < 0.0:
            raise IOError("recv timed out in {} seconds".format(orig_timeout))

        return opcode, data
Exemplo n.º 6
0
def test_copes_with_slack_disconnect():
    frontend = make_timeout_frontend(1)
    with mock.patch("tests.test_helpers.WebSocketForTest.recv") as mock_post:
        mock_post.side_effect = websocket.WebSocketConnectionClosedException()
        frontend.doSlackRead({})