What This Example Demonstrates
- Basic server/client communication patterns
- Broadcasting messages to all connected clients
- Managing player lists and connection events
- Using
Network_methods to handle different message types - Running a client pump loop with threading for input handling
Complete Code
How to Run
The client uses a separate thread for reading user input to avoid blocking the
pump() loop. This pattern is useful when integrating with game engines like pygame that need a continuous update loop.Key Takeaways
- Network methods: Define
Network_<action>methods to automatically handle messages with{"action": "<action>"} - Broadcasting: Use a custom
broadcast()method to send data to all connected clients - Player management: Track connected clients in a dictionary and clean up on disconnect via
on_close() - Threading pattern: The client demonstrates how to use threading to handle input while maintaining a pump loop
- Type safety: The
ChannelandServerclasses use generic types for better IDE support