commit 5aa34b5e7cf62c8729649bf4bc82fdada21349c3 Author: manetta Date: Tue Apr 6 19:20:21 2021 +0200 adding a small prototype here diff --git a/websockets/README.md b/websockets/README.md new file mode 100644 index 0000000..e61ccbf --- /dev/null +++ b/websockets/README.md @@ -0,0 +1,19 @@ +A small websocket testing setup: live streaming plain text without refreshing the webpage. + +![](screenshot.png) + +# Running this + +Run a websocket server in Python: + + $ python3 server.py + +View the interface: + + open client.html in a browser + +Stream plain text (with HTML features): + + open mytxtfile.txt in a text editor, type, hit save (CTRL+S), type more, hit save, etc + + diff --git a/websockets/client.html b/websockets/client.html new file mode 100644 index 0000000..310d150 --- /dev/null +++ b/websockets/client.html @@ -0,0 +1,15 @@ + + + + WebSocket demo + + + + + diff --git a/websockets/mytxtfile.txt b/websockets/mytxtfile.txt new file mode 100644 index 0000000..1dd6472 --- /dev/null +++ b/websockets/mytxtfile.txt @@ -0,0 +1,28 @@ +hello +hello +hello +hello +and now +yes +now +it +works +sort of
+i need to save my file though +but
+i
+can
+write
+HTML! +yes +yes +yes +and now what happens? +it works +but i still need to save
+ +yesss +

+but it is nice somehow +

+it feels intimate diff --git a/websockets/screenshot.png b/websockets/screenshot.png new file mode 100644 index 0000000..e87c846 Binary files /dev/null and b/websockets/screenshot.png differ diff --git a/websockets/server.py b/websockets/server.py new file mode 100644 index 0000000..530d4bf --- /dev/null +++ b/websockets/server.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +# WS server that sends messages every second + +import asyncio +import datetime +import random +import websockets + +async def time(websocket, path): + while True: + # now = datetime.datetime.utcnow().isoformat() + "Z" + # myinput = open('mytxtfile.txt') + # txt = myinput.read() + # myinput.close() + with open('mytxtfile.txt') as myinput: + txt = myinput.read() + await websocket.send(txt) + await asyncio.sleep(1) + +start_server = websockets.serve(time, "127.0.0.1", 5678) + +asyncio.get_event_loop().run_until_complete(start_server) +asyncio.get_event_loop().run_forever()