From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- .../vweb/server_sent_events/assets/site.css | 19 +++++++++++ .../vweb/server_sent_events/assets/v-logo.svg | 1 + .../v/examples/vweb/server_sent_events/favicon.ico | Bin 0 -> 15406 bytes .../v/examples/vweb/server_sent_events/index.html | 38 +++++++++++++++++++++ .../v/examples/vweb/server_sent_events/server.v | 37 ++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 v_windows/v/examples/vweb/server_sent_events/assets/site.css create mode 100644 v_windows/v/examples/vweb/server_sent_events/assets/v-logo.svg create mode 100644 v_windows/v/examples/vweb/server_sent_events/favicon.ico create mode 100644 v_windows/v/examples/vweb/server_sent_events/index.html create mode 100644 v_windows/v/examples/vweb/server_sent_events/server.v (limited to 'v_windows/v/examples/vweb/server_sent_events') diff --git a/v_windows/v/examples/vweb/server_sent_events/assets/site.css b/v_windows/v/examples/vweb/server_sent_events/assets/site.css new file mode 100644 index 0000000..4ad9eb8 --- /dev/null +++ b/v_windows/v/examples/vweb/server_sent_events/assets/site.css @@ -0,0 +1,19 @@ +body { + font-family: Arial, Helvetica, sans-serif; + color: #eee; + background-color: #333; + background-image: url("v-logo.svg"); + background-repeat: no-repeat; + background-size: 10em; + margin: 0; + padding-left: 11em; +} + +h1 { + color: #6699CC; +} + +img.logo { + float: left; + width: 10em; +} diff --git a/v_windows/v/examples/vweb/server_sent_events/assets/v-logo.svg b/v_windows/v/examples/vweb/server_sent_events/assets/v-logo.svg new file mode 100644 index 0000000..9a4ec60 --- /dev/null +++ b/v_windows/v/examples/vweb/server_sent_events/assets/v-logo.svg @@ -0,0 +1 @@ + diff --git a/v_windows/v/examples/vweb/server_sent_events/favicon.ico b/v_windows/v/examples/vweb/server_sent_events/favicon.ico new file mode 100644 index 0000000..fa834c3 Binary files /dev/null and b/v_windows/v/examples/vweb/server_sent_events/favicon.ico differ diff --git a/v_windows/v/examples/vweb/server_sent_events/index.html b/v_windows/v/examples/vweb/server_sent_events/index.html new file mode 100644 index 0000000..7e500c9 --- /dev/null +++ b/v_windows/v/examples/vweb/server_sent_events/index.html @@ -0,0 +1,38 @@ + +
+ @title + + @css 'assets/site.css' +
+ +

@title

+ + + + + 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) +} -- cgit v1.2.3