async def test():
     try:
         async with open_connection('http://foo.bar/baz') as con:
             self.assertIsInstance(con, WebSocketConnection)
     except ValueError:
         # ValueError tells us that our WebSocket URI
         # doesn't have a "ws" or "wss" scheme, so
         # everything is fine with this error.
         pass
        async def test():
            async with open_connection('wss://echo.websocket.org') as con:
                self.assertIsInstance(con, WebSocketConnection)
                assert not con.closed
                assert con.is_client
                assert str(con).startswith('client-')
                self.assertIsNone(con.subprotocol)

                self.assertEqual(con.host, 'echo.websocket.org')
                self.assertEqual(con.path, '/')

            # Has the connection terminated without any issues?
            self.assertEqual(con.close_code.value, 1000)
Exemple #3
0
async def main():
    async with open_connection('ws://echo.websocket.org') as con:
        print('Connection established!')

        # First, let's send some text to the server.
        text = input('What to send? ')
        await con.send(text)

        # Now, we receive and verify the server's response.
        message = await con.get_message()
        assert message == text, "Received {}, expected {}".format(
            message, text)

    print('Connection closed with code {}'.format(con.close_code.value))
        async def test():
            async with open_connection('wss://echo.websocket.org') as con:
                self.assertIsInstance(con, WebSocketConnection)
                assert not con.closed
                assert con.is_client
                assert str(con).startswith('client-')
                self.assertIsNone(con.subprotocol)

            try:
                await con.get_message()
            except ConnectionClosed as error:
                self.assertEqual(error.name, 'NORMAL_CLOSURE')
                self.assertEqual(error.code, 1000)
                self.assertEqual(error.reason, '')
        async def test():
            data = 'test'

            async with open_connection('wss://echo.websocket.org') as con:
                self.assertIsInstance(con, WebSocketConnection)
                assert not con.closed
                assert con.is_client
                assert str(con).startswith('client-')
                self.assertIsNone(con.subprotocol)

                await con.send_message(data)
                message = await con.get_message()

                self.assertEqual(message, data)

            # Has the connection terminated without any issues?
            self.assertEqual(con.close_code.value, 1000)