an attempt to get swim processes on asdi-db to work using http server-sent events
summary of WebSockets vs. Server-Sent events/EventSource https://stackoverflow.com/questions/5195452/websockets-vs-server-sent-events-eventsource
a question on stack overflow: https://stackoverflow.com/questions/35927900/how-to-setup-cherrypy-server-sent-events
possible pastebin of above code: https://pastebin.com/xCFBqAAg
description of fields: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Fields
an old (incorrect) blog entry from 2011: http://neokannon.blogspot.com/2011/07/cherrypy-server-sent-events-and-you.html
6-year-old git demo: https://github.com/liyanage/bottlepy-eventsource-test uses bottle (a framework on top of cherrypy?)
blog entry 2018, possible useful: https://medium.com/conectric-networks/a-look-at-server-sent-events-54a77f8d6ff7 (server-side is in Node.js)
dice demo from above, it seems that server is in python, but calls npm for processing: https://github.com/simonprickett/server-sent-events-demo
code is from: https://stackoverflow.com/questions/35927900/how-to-setup-cherrypy-server-sent-events
for the html:
cd ~/webapps/cherry
vim srvsent.html
<copy/paste html index.btml from above link>
and comment out css:
<!-- link rel="stylesheet" href="style.css" -->
and edit this:
//r sse_handler = new EventSource('http://localhost:8080/getUpdate');
var sse_handler = new EventSource('http://here.ilikecarrots.com/getUpdate');
for the server, add this to the cherrypy site.py file:
vim site.py
30 #-------------------- test example of server-sent
31 # from https://stackoverflow.com/questions/35927900/how-to-setup-cherrypy-server-sent-events
32
33 #import datetime
34 from pathlib import Path
35
36 class TestServerSentEvent(object):
37 @cherrypy.expose
38 def index(self):
39 return Path("index.html").read_text()
40
41 @cherrypy.expose
42 def getUpdate(self, * pos, ** nam):
43 cherrypy.response.headers["Content-Type"] = "text/event-stream;charset=utf-8"
44 return 'retry: 1200\ndata: {0}\n\n'.format(self.output())
45
46 def output(self) :
47 #d = datetime.datetime.now()
48 #return f"TEST - {d}"
49 d = datetime.now()
50 return "TEST - %s" % d
51
52 getUpdate._cp_config = {'response.stream': True, 'tools.encode.encoding':'utf-8'}
53
54 #--------------------
then, open this: http://here.ilikecarrots.com/srvsent.html
hmm..., Console complains about
Firefox can't establish a connection to the server at http://here.ilikecarrots.com/getUpdate.
lots of edits, then: it finally works!!!
current message:
retry: 2000
data: TEST2 - 2019-01-17 16:37:17.042353
note: if 'retry' on there, then default is apparently 5 secs.
proposed:
struct: { 'acid' : 'N13285',
'lat' : 38.5,
'lng: -84.5
}
also separated out ss.py for these functions
finally, have
ok, seems working, will proceed to mergint this with actual swim code...