-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
410 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>SSE Example</title> | ||
</head> | ||
<body> | ||
<h1>Server-Sent Events Example</h1> | ||
<button id="toggle-sse">Start SSE Connection</button> | ||
<div id="messages"></div> | ||
<script> | ||
let eventSource = null; | ||
const button = document.getElementById('toggle-sse'); | ||
|
||
button.addEventListener('click', function() { | ||
if (eventSource) { | ||
// If connection exists, close it | ||
eventSource.close(); | ||
eventSource = null; | ||
button.textContent = 'Start SSE Connection'; | ||
return; | ||
} | ||
|
||
// Start new connection | ||
eventSource = new EventSource('/stream'); | ||
button.textContent = 'Stop SSE Connection'; | ||
|
||
eventSource.onmessage = function(event) { | ||
const messagesDiv = document.getElementById('messages'); | ||
messagesDiv.innerHTML += `<p>${event.data}</p>`; | ||
// Auto-scroll to bottom | ||
messagesDiv.scrollTop = messagesDiv.scrollHeight; | ||
}; | ||
|
||
eventSource.onerror = (err) => { | ||
console.error("Error occurred while connecting to SSE:", err); | ||
eventSource.close(); | ||
eventSource = null; | ||
button.textContent = 'Start SSE Connection'; | ||
}; | ||
}); | ||
|
||
// Clean up on page unload | ||
window.addEventListener('unload', () => { | ||
if (eventSource) { | ||
eventSource.close(); | ||
} | ||
}); | ||
</script> | ||
<style> | ||
#messages { | ||
max-height: 400px; | ||
overflow-y: auto; | ||
border: 1px solid #ccc; | ||
padding: 10px; | ||
margin-top: 10px; | ||
} | ||
p { | ||
margin: 5px 0; | ||
} | ||
</style> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const std = @import("std"); | ||
const zzz = @import("zzz"); | ||
const http = zzz.HTTP; | ||
const log = std.log.scoped(.@"examples/sse"); | ||
|
||
const Server = http.Server(.plain, .auto); | ||
const Router = Server.Router; | ||
const Context = Server.Context; | ||
const Route = Server.Route; | ||
const SSE = Server.SSE; | ||
|
||
fn sse_task(sse: *SSE) !void { | ||
log.debug("ctx ptr: {*}", .{sse.context}); | ||
try sse.send(.{ .data = "hi this is a message" }, sse_task); | ||
std.time.sleep(std.time.ns_per_s); | ||
} | ||
|
||
fn sse_handler(ctx: *Context) void { | ||
log.debug("going into sse mode", .{}); | ||
ctx.to_sse(sse_task); | ||
} | ||
|
||
pub fn main() !void { | ||
const host: []const u8 = "0.0.0.0"; | ||
const port: u16 = 9862; | ||
|
||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
const allocator = gpa.allocator(); | ||
defer _ = gpa.deinit(); | ||
|
||
var router = Router.init(allocator); | ||
defer router.deinit(); | ||
|
||
try router.serve_embedded_file("/", http.Mime.HTML, @embedFile("index.html")); | ||
try router.serve_route("/stream", Route.init().get(sse_handler)); | ||
|
||
var server = Server.init(.{ | ||
.router = &router, | ||
.allocator = allocator, | ||
.threading = .single, | ||
}); | ||
defer server.deinit(); | ||
|
||
try server.bind(host, port); | ||
try server.listen(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.