socktools.tools.examples.gen_proto.chat_server¶
Note
This example requires you to first build the chat_protocol.json file using gen_proto.
-
class
socktools.tools.examples.gen_proto.chat_server.ChatHandlers(bind=None, connect=None, handlers={}, timeout=10, tick_interval=0.25, sock=None, meta_sock=None, logger=None, no_async=False)[source]¶ Bases:
socktools.tools.examples.gen_proto.generated.ChatProtocolServer implementation for the chat protocol
This class inherits from the generated protocol class rendered by gen_proto and adds handlers. Note that there is no need to do anything beyond adding a handle_MSGTYPE method to the class. Adding handlers to the handlers dict of the parent is done automatically, take a look at gen_proto’s output to see the generated stubs.
-
handle_msg(from_addr, msg_text=None)[source]¶ Handle chat messages
This is the only handler in this example that does useful work (since ping/pong are not actually used for timeouts directly). The incoming message text is simply broadcast to all other peers.
Parameters: from_addr (tuple) – Represents the TCP/IP endpoint the message came from in (address,port) format Keyword Arguments: msg_text (str) – The text of the message
-
handle_ping(from_addr, ping_id=None)[source]¶ Handle ping packets
The first message type handler in this example demonstrates making use of the keyword arguments sent to it by the generated class. Ping packets contain a ping_id field which can be used to acknowledge the packet in a PONG packet sent back to the remote peer.
Parameters: from_addr (tuple) – Represents the TCP/IP endpoint the message came from in (address,port) format Keyword Arguments: ping_id (int) – In a “real” server this would be the ping ID that needs to be acknowledged with a corresponding PONG message back.
-