(require :hunchentoot)
(rename-package :hunchentoot :hunchentoot '(:tbnl :ht))
(defvar $server (make-instance 'ht::easy-acceptor :port 1234 :access-log-destination nil))
(ht:start $server)
(ht:define-easy-handler (sse-test :uri "/sse-test") ()
"<script>
var source=new EventSource('sse-stream');
source.onmessage=function(event) {
document.getElementById('result').innerHTML=event.data + '<br>';
};
</script>
Server-side event demo<br>
<div id=result></div>
")
(defun set-header (name value) (setf (ht:header-out name) value))
(ht:define-easy-handler (sse-stream :uri "/sse-stream") ()
(set-header "content-type" "text/event-stream")
(set-header "cache-control" "no-cache")
(let ((stream (ht:send-headers)))
(dotimes (i 10)
(write-sequence (encode-string-to-octets
(format nil "data: test ~A~A~A" i #\newline #\newline))
stream)
(force-output stream)
(sleep 0.5))))