%%javascript
var ws = new WebSocket("ws://localhost:8678/websocket");
ws.onopen = function() {
alert("Hello, world");
};
ws.onmessage = function (evt) {
let dat = JSON.parse(event.data);
console.log(dat);
alert(evt.data);
};
ws.onclose = function (evt) {
alert( "ByBye!!!");
}
python script producer.py
# WS server that sends messages at random intervals
import asyncio
import random
import numpy as np
import json
import websockets
async def produce(websocket, path):
l = [1.1,2.2,'troglodyte', -3.3,4.4,5.,6.,7.,8.8]
while True:
n = random.randint(1,len(l));
await websocket.send( json.dumps(l[0:n], ensure_ascii=False) )
await asyncio.sleep(random.random() * 3)
start_server = websockets.serve( produce, "127.0.0.1", 8678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
but I need to run the producer.py code before executing the notebook cell.
No idea yet how to close/reopen the connection, and how to make the startup order abitrary…