adding a small prototype here

master
manetta 4 years ago
commit 5aa34b5e7c

@ -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

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>WebSocket demo</title>
</head>
<body>
<script>
var ws = new WebSocket("ws://127.0.0.1:5678/")
ws.onmessage = function (event) {
document.body.innerHTML = event.data;
};
document.body.appendChild(messages);
</script>
</body>
</html>

@ -0,0 +1,28 @@
hello
hello
hello
hello
and now
yes
now
it
works
sort of <br>
i need to save my file though
but <br>
i<br>
can<br>
write<br>
HTML!
<span style="color:blue;">yes</span>
<span style="background-color:yellow;">yes</span>
<span style="background-color:pink;">yes</span>
and now what happens?
it works
but i still need to save<br>
yesss
<br><br>
but it is nice somehow
<br><br>
it feels intimate

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

@ -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()
Loading…
Cancel
Save