blob: 01e63cbb7604073d8ca51348c87282292ab8361f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
module main
import os
import rand
import time
import vweb
import vweb.sse
struct App {
vweb.Context
}
fn main() {
vweb.run(&App{}, 8081)
}
pub fn (mut app App) init_server() {
app.serve_static('/favicon.ico', 'favicon.ico')
app.mount_static_folder_at(os.resource_abs_path('.'), '/')
}
pub fn (mut app App) index() vweb.Result {
title := 'SSE Example'
return $vweb.html()
}
fn (mut app App) sse() vweb.Result {
mut session := sse.new_connection(app.conn)
// NB: you can setup session.write_timeout and session.headers here
session.start() or { return app.server_error(501) }
session.send_message(data: 'ok') or { return app.server_error(501) }
for {
data := '{"time": "$time.now().str()", "random_id": "$rand.ulid()"}'
session.send_message(event: 'ping', data: data) or { return app.server_error(501) }
println('> sent event: $data')
time.sleep(1 * time.second)
}
return app.server_error(501)
}
|