echo.py (1334B)
1 streams_dict = {} 2 3 4 def session_established(session): 5 # When a WebTransport session is established, a bidirectional stream is 6 # created by the server, which is used to echo back stream data from the 7 # client. 8 session.create_bidirectional_stream() 9 10 11 def stream_data_received(session, 12 stream_id: int, 13 data: bytes, 14 stream_ended: bool): 15 # If a stream is unidirectional, create a new unidirectional stream and echo 16 # back the data on that stream. 17 if session.stream_is_unidirectional(stream_id): 18 if (session.session_id, stream_id) not in streams_dict.keys(): 19 new_stream_id = session.create_unidirectional_stream() 20 streams_dict[(session.session_id, stream_id)] = new_stream_id 21 session.send_stream_data(streams_dict[(session.session_id, stream_id)], 22 data, 23 end_stream=stream_ended) 24 if (stream_ended): 25 del streams_dict[(session.session_id, stream_id)] 26 return 27 # Otherwise (e.g. if the stream is bidirectional), echo back the data on the 28 # same stream. 29 session.send_stream_data(stream_id, data, end_stream=stream_ended) 30 31 32 def datagram_received(session, data: bytes): 33 session.send_datagram(data)