aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/examples/vweb/server_sent_events/server.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/examples/vweb/server_sent_events/server.v
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/examples/vweb/server_sent_events/server.v')
-rw-r--r--v_windows/v/examples/vweb/server_sent_events/server.v37
1 files changed, 37 insertions, 0 deletions
diff --git a/v_windows/v/examples/vweb/server_sent_events/server.v b/v_windows/v/examples/vweb/server_sent_events/server.v
new file mode 100644
index 0000000..c1139d8
--- /dev/null
+++ b/v_windows/v/examples/vweb/server_sent_events/server.v
@@ -0,0 +1,37 @@
+module main
+
+import os
+import rand
+import time
+import vweb
+import vweb.sse
+
+struct App {
+ vweb.Context
+}
+
+fn main() {
+ mut app := &App{}
+ app.serve_static('/favicon.ico', 'favicon.ico')
+ app.mount_static_folder_at(os.resource_abs_path('.'), '/')
+ vweb.run(app, 8081)
+}
+
+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)
+}