How to use the wsproto.events function in wsproto

To help you get started, we’ve selected a few wsproto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pgjones / hypercorn / tests / asyncio / test_sanity.py View on Github external
assert events[0].headers == [
        (b":status", b"200"),
        (b"date", b"Thu, 01 Jan 1970 01:23:20 GMT"),
        (b"server", b"hypercorn-h2"),
    ]
    client = wsproto.connection.Connection(wsproto.ConnectionType.CLIENT)
    h2_client.send_data(stream_id, client.send(wsproto.events.BytesMessage(data=SANITY_BODY)))
    await server.reader.send(h2_client.data_to_send())  # type: ignore
    events = h2_client.receive_data(await server.writer.receive())  # type: ignore
    client.receive_data(events[0].data)
    assert list(client.events()) == [wsproto.events.TextMessage(data="Hello & Goodbye")]
    h2_client.send_data(stream_id, client.send(wsproto.events.CloseConnection(code=1000)))
    await server.reader.send(h2_client.data_to_send())  # type: ignore
    events = h2_client.receive_data(await server.writer.receive())  # type: ignore
    client.receive_data(events[0].data)
    assert list(client.events()) == [wsproto.events.CloseConnection(code=1000, reason="")]
    await server.reader.send(b"")  # type: ignore
github pgjones / quart / quart / serving / websocket.py View on Github external
def data_received(self, data: bytes) -> None:
        super().data_received(data)
        self.connection.receive_bytes(data)
        for event in self.connection.events():
            if isinstance(event, wsproto.events.ConnectionRequested):
                self.handle_websocket(event)
            elif isinstance(event, wsproto.events.DataReceived):
                if self._buffer is None:
                    if isinstance(event, wsproto.events.TextReceived):
                        self._buffer = ''
                    else:
                        self._buffer = b''
                self._buffer += event.data
                if len(self._buffer) > self.app.config['MAX_CONTENT_LENGTH']:
                    self.write(self.connection.bytes_to_send())
                    self.close()
                if event.message_finished:
                    self.queue.put_nowait(self._buffer)
                    self._buffer = None
            elif isinstance(event, wsproto.events.ConnectionClosed):
                self.write(self.connection.bytes_to_send())
                self.close()
            self.write(self.connection.bytes_to_send())
github pgjones / quart / quart / serving / websocket.py View on Github external
def data_received(self, data: bytes) -> None:
        super().data_received(data)
        self.connection.receive_bytes(data)
        for event in self.connection.events():
            if isinstance(event, wsproto.events.ConnectionRequested):
                self.handle_websocket(event)
            elif isinstance(event, wsproto.events.DataReceived):
                if self._buffer is None:
                    if isinstance(event, wsproto.events.TextReceived):
                        self._buffer = ''
                    else:
                        self._buffer = b''
                self._buffer += event.data
                if len(self._buffer) > self.app.config['MAX_CONTENT_LENGTH']:
                    self.write(self.connection.bytes_to_send())
                    self.close()
                if event.message_finished:
                    self.queue.put_nowait(self._buffer)
                    self._buffer = None
            elif isinstance(event, wsproto.events.ConnectionClosed):
                self.write(self.connection.bytes_to_send())
                self.close()
            self.write(self.connection.bytes_to_send())
github aiortc / aioquic / examples / http3_server.py View on Github external
def websocket_event_received(self, event: wsproto.events.Event) -> None:
        if isinstance(event, wsproto.events.TextMessage):
            self.queue.put_nowait({"type": "websocket.receive", "text": event.data})
        elif isinstance(event, wsproto.events.Message):
            self.queue.put_nowait({"type": "websocket.receive", "bytes": event.data})
        elif isinstance(event, wsproto.events.CloseConnection):
            self.queue.put_nowait({"type": "websocket.disconnect", "code": event.code})
github aiortc / aioquic / examples / http3_server.py View on Github external
def websocket_event_received(self, event: wsproto.events.Event) -> None:
        if isinstance(event, wsproto.events.TextMessage):
            self.queue.put_nowait({"type": "websocket.receive", "text": event.data})
        elif isinstance(event, wsproto.events.Message):
            self.queue.put_nowait({"type": "websocket.receive", "bytes": event.data})
        elif isinstance(event, wsproto.events.CloseConnection):
            self.queue.put_nowait({"type": "websocket.disconnect", "code": event.code})
github pgjones / hypercorn / hypercorn / trio / h2.py View on Github external
async def data_received(self, data: bytes) -> None:
        self.connection.receive_data(data)
        for event in self.connection.events():
            if isinstance(event, wsproto.events.TextMessage):
                await self.app_send_channel.send({"type": "websocket.receive", "text": event.data})
            elif isinstance(event, wsproto.events.BytesMessage):
                await self.app_send_channel.send({"type": "websocket.receive", "bytes": event.data})
            elif isinstance(event, wsproto.events.Ping):
                await self.asend(Data(self.connection.send(event.response())))
            elif isinstance(event, wsproto.events.CloseConnection):
                if self.connection.state == wsproto.connection.ConnectionState.REMOTE_CLOSING:
                    await self.asend(Data(self.connection.send(event.response())))
                await self.app_send_channel.send({"type": "websocket.disconnect"})
                break
github encode / uvicorn / uvicorn / protocols / websockets / wsproto_impl.py View on Github external
def handle_events(self):
        for event in self.conn.events():
            if isinstance(event, events.Request):
                self.handle_connect(event)
            elif isinstance(event, events.TextMessage):
                self.handle_text(event)
            elif isinstance(event, events.BytesMessage):
                self.handle_bytes(event)
            elif isinstance(event, events.RejectConnection):
                self.handle_no_connect(event)
            elif isinstance(event, events.RejectData):
                self.handle_no_connect(event)
            elif isinstance(event, events.CloseConnection):
                self.handle_close(event)
            elif isinstance(event, events.Ping):
                self.handle_ping(event)