Skip to content

Commit

Permalink
Send timestamp as message on example
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermef committed Dec 23, 2020
1 parent 32f9e5a commit acbdfa5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
28 changes: 17 additions & 11 deletions example/simple_event.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
<html>
<head>
<title>Simple Event</title>

<script>
var evtSource = new EventSource("http://localhost:8888/");
evtSource.onmessage = function(e) {
console.log(e.lastEventId);
console.log(e.data);
};
evtSource.addEventListener("doge", function(e) {
console.log(e);
}, false);
</script>
</head>
<body>
<ul id="message-list">
</ul>
<script>
var appendEvent = function (source, e, ul) {
var li = document.createElement('li');
li.innerHTML = "<div>event name: " + source + "</div> <div> Message: " + e.data + "</div>";
ul.appendChild(li);
};

var evtSource = new EventSource("http://localhost:8888/");
var ul = document.querySelector("ul");
evtSource.onmessage = function (e) {
appendEvent("missing", e, ul)
};
evtSource.addEventListener("doge", function (e) {
appendEvent("doge", e, ul)
}, false);
</script>
</body>
</html>
9 changes: 7 additions & 2 deletions example/simple_event.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import uuid
import datetime
import json

import tornado.ioloop
import tornado.web
Expand All @@ -10,15 +12,18 @@ def open(self):
ioloop = tornado.ioloop.IOLoop.instance()
self.heart_beat = tornado.ioloop.PeriodicCallback(self._simple_callback, 5000, ioloop)
self.heart_beat.start()
self.write_message(msg="Wow much nameless", evt_id=uuid.uuid4())
self._simple_callback()
print('Connection open')

def close(self):
print('Connection closed')

def _simple_callback(self):
self.write_message(name="doge", msg="Wow much alive\nSuch message", evt_id=uuid.uuid4())
self.write_message(msg="Wow much nameless", evt_id=uuid.uuid4())
self.write_message(msg=json.dumps({
"timestamp": datetime.datetime.now().isoformat(),
"json_message": "message",
}), evt_id=uuid.uuid4())

application = tornado.web.Application([
(r"/", MainHandler),
Expand Down

0 comments on commit acbdfa5

Please sign in to comment.