Re: Server-Sent Events
- Posted by acEduardo May 17, 2017
- 3230 views
I'd adapted a example I found in PHP, and it simply works.
sse.html:
<!DOCTYPE html> <html> <head> <title> SSE Example </title> <meta charset="utf-8" /> </head> <body> <script> var source = new EventSource('sse.ex'); source.onmessage = function(e) { document.body.innerHTML += e.data + '<br>'; }; </script> </body> </html>
sse.ex:
include std/datetime.e constant months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"} constant days_week = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"} export enum year, month, day, hour, minute, second, weekday export function now() sequence _now = now_gmt() _now &= weeks_day(_now) return _now end function export function timestamp (sequence _date, sequence timezone = "UTC", integer offset = 0) integer i = 1 sequence weekday_name = days_week[_date[7]], month_name = months[_date[2]] return sprintf("%s, %02d %s %04d %02d:%02d:%02d %s", {weekday_name, _date[3], month_name, _date[1], _date[4], _date[5], _date[6], timezone} ) end function puts (1, "Content-Type: text/event-stream\r\n" & "Cache-Control: no-cache\r\n" & "\r\n") printf (1, "id: %d\n" & "data: %s\n", { time(), timestamp(now()) })