examples: minor (see previous commits)

examples: 3rd-party sse (2):  add the same client-side as the original 3rd-party example
This commit is contained in:
Gerasimos (Makis) Maropoulos 2021-01-27 04:51:53 +02:00
parent bccd5eb388
commit 61f04ee4ff
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
2 changed files with 27 additions and 2 deletions

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>SSE Examples</title>
</head>
<body>
<strong>Messages</strong>
<br>
<div id="messages"></div>
<script type="text/javascript">
e1 = new EventSource('/events/channel-1');
e1.onmessage = function(event) {
document.getElementById('messages').innerHTML += 'Message on channel-1: ' + event.data + '<br>';
};
e2 = new EventSource('/events/channel-2');
e2.onmessage = function(event) {
document.getElementById('messages').innerHTML += 'Message on channel-2: ' + event.data + '<br>';
};
</script>
</body>
</html>

View File

@ -11,8 +11,10 @@ import (
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
) )
// Install the sse third-party package // Install the sse third-party package.
// $ go get -u github.com/alexandrevicenzi/go-sse // $ go get -u github.com/alexandrevicenzi/go-sse
//
// Documentation: https://pkg.go.dev/github.com/alexandrevicenzi/go-sse
func main() { func main() {
s := sse.NewServer(&sse.Options{ s := sse.NewServer(&sse.Options{
// Increase default retry interval to 10s. // Increase default retry interval to 10s.
@ -35,7 +37,7 @@ func main() {
app := iris.New() app := iris.New()
app.Get("/", func(ctx iris.Context) { app.Get("/", func(ctx iris.Context) {
ctx.ServeFile("../static/index.html") ctx.ServeFile("./index.html")
}) })
app.Get("/events/{channel}", iris.FromStd(s)) app.Get("/events/{channel}", iris.FromStd(s))