awful-sse/examples/example1.scm

28 lines
793 B
Scheme
Raw Normal View History

2014-07-08 02:37:35 +00:00
;; Run with 'awful example1.scm'.
;; On web browser open http://localhost:8080/client and watch the
2014-07-08 02:01:53 +00:00
;; new time coming each second from the server.
2020-12-29 02:35:33 +00:00
(import (chicken time posix) (chicken random) (awful sse) awful spiffy srfi-18)
2014-07-08 02:01:53 +00:00
(define (sse-proc)
(let loop ()
2020-12-29 02:35:33 +00:00
(send-sse-data (seconds->string) id: (pseudo-random-integer 10) event: "message")
2014-07-08 02:01:53 +00:00
(thread-sleep! 1)
(loop)))
(define-page/sse "/client"
(lambda ()
(add-javascript
"var source = new EventSource('/sse');
source.onmessage = function (event) {
display = document.getElementById('display');
display.innerHTML = event.data;
};")
(add-css "#display { color: blue; }")
`(div "Current time: "
(span (@ (id "display")) "")))
"/sse"
sse-proc
use-sxml: #t)