Created
December 27, 2023 00:56
-
-
Save stodge/245a224b23631b20bc6c94e6a6a49a09 to your computer and use it in GitHub Desktop.
Simple websocket server test using Tornado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
import tornado.web | |
import tornado.websocket | |
class WebSocketServer(tornado.websocket.WebSocketHandler): | |
"""Simple WebSocket handler to serve clients.""" | |
# Note that `clients` is a class variable and `send_message` is a | |
# classmethod. | |
clients = set() | |
def open(self): | |
logger.debug("Client connected") | |
WebSocketServer.clients.add(self) | |
def on_close(self): | |
logger.debug("Client gone") | |
WebSocketServer.clients.remove(self) | |
def on_message(self, msg): | |
logger.debug("Rx: " + msg) | |
def main(): | |
app = tornado.web.Application( | |
[(r"/ws", WebSocketServer)], | |
websocket_ping_interval=10, | |
websocket_ping_timeout=30, | |
autoreload=True, | |
debug=True | |
) | |
app.listen(9000) | |
tornado.ioloop.IOLoop.instance().start() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment