mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 11:06:27 +01:00
examples: server-sent events: add a second 3rd-party pkg usage example
This commit is contained in:
parent
ce25e698f8
commit
bccd5eb388
|
@ -184,8 +184,9 @@
|
|||
* [HTTP/2 Server Push](response-writer/http2push/main.go)
|
||||
* [Stream Writer](response-writer/stream-writer/main.go)
|
||||
* [Transactions](response-writer/transactions/main.go)
|
||||
* [SSE](response-writer/sse/main.go)
|
||||
* [SSE (third-party package usage for server sent events)](response-writer/sse-third-party/main.go)
|
||||
* [Server-Sent Events](response-writer/sse/main.go)
|
||||
* [SSE 3rd-party (r3labs/sse)](response-writer/sse-third-party/main.go)
|
||||
* [SSE 3rd-party (alexandrevicenzi/go-sse)](response-writer/sse-third-party-2/main.go)
|
||||
* Cache
|
||||
* [Simple](response-writer/cache/simple/main.go)
|
||||
* [Client-Side (304)](response-writer/cache/client-side/main.go)
|
||||
|
|
59
_examples/response-writer/sse-third-party-2/main.go
Normal file
59
_examples/response-writer/sse-third-party-2/main.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/alexandrevicenzi/go-sse"
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// Install the sse third-party package
|
||||
// $ go get -u github.com/alexandrevicenzi/go-sse
|
||||
func main() {
|
||||
s := sse.NewServer(&sse.Options{
|
||||
// Increase default retry interval to 10s.
|
||||
RetryInterval: 10 * 1000,
|
||||
// CORS headers
|
||||
Headers: map[string]string{
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "Keep-Alive,X-Requested-With,Cache-Control,Content-Type,Last-Event-ID",
|
||||
},
|
||||
// Custom channel name generator
|
||||
ChannelNameFunc: func(request *http.Request) string {
|
||||
return request.URL.Path
|
||||
},
|
||||
// Print debug info
|
||||
Logger: log.New(os.Stdout, "go-sse: ", log.Ldate|log.Ltime|log.Lshortfile),
|
||||
})
|
||||
|
||||
defer s.Shutdown()
|
||||
|
||||
app := iris.New()
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ServeFile("../static/index.html")
|
||||
})
|
||||
app.Get("/events/{channel}", iris.FromStd(s))
|
||||
|
||||
go func() {
|
||||
for {
|
||||
s.SendMessage("/events/channel-1", sse.SimpleMessage(time.Now().Format("2006/02/01/ 15:04:05")))
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
i := 0
|
||||
for {
|
||||
i++
|
||||
s.SendMessage("/events/channel-2", sse.SimpleMessage(strconv.Itoa(i)))
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
app.Listen(":3000")
|
||||
}
|
Loading…
Reference in New Issue
Block a user