From ce24c2ff1d4da137a1abd0e64d3725079ae48187 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Tue, 8 Nov 2022 12:09:57 -0300 Subject: [PATCH] Update README.md --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index c358842..7d40bb2 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,33 @@ app.listen(3000, lambda config: print("Listening on port http://localhost:%d now app.run() ``` +WebSockets +```python +from socketify import App, AppOptions, OpCode, CompressOptions + +def ws_open(ws): + print('A WebSocket got connected!') + ws.send("Hello World!", OpCode.TEXT) + +def ws_message(ws, message, opcode): + #Ok is false if backpressure was built up, wait for drain + ok = ws.send(message, opcode) + +app = App() +app.ws("/*", { + 'compression': CompressOptions.SHARED_COMPRESSOR, + 'max_payload_length': 16 * 1024 * 1024, + 'idle_timeout': 12, + 'open': ws_open, + 'message': ws_message, + 'drain': lambda ws: print('WebSocket backpressure: %i' % ws.get_buffered_amount()), + 'close': lambda ws, code, message: print('WebSocket closed') +}) +app.any("/", lambda res,req: res.end("Nothing to see here!'")) +app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port))) +app.run() +``` + We have more than 20 examples [click here](https://github.com/cirospaciari/socketify.py/tree/main/examples) for more ## Build local from source