From d926c1c894049857c6b832c774ffaec3183d708b Mon Sep 17 00:00:00 2001 From: snowmerak Date: Mon, 26 Jun 2023 21:58:51 +0900 Subject: [PATCH] Update README.md --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 78b52db..36bd35c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ sona is a simple broadcast server for sending messages to multiple clients. go get github.com/snowmerak/sona ``` -## Usage +## Example ```go package main @@ -39,3 +39,49 @@ func main() { ``` This code is sending a message("hello, world") to all clients every second. + +## Events + +```go +package main + +import ( + "context" + "github.com/snowmerak/sona/lib/sona" + "log" + "net/http" + "time" +) + +func main() { + ctx := context.Background() + app := sona.New(). + EnableSSE(ctx, "0.0.0.0:8080"). + EnableWS(ctx, "0.0.0.0:8081"). + OnConnect(func(w http.ResponseWriter, r *http.Request) { + log.Println("connect") + }). + OnSend(func(w http.ResponseWriter, r *http.Request) { + log.Println("send") + }). + OnDisconnect(func(w http.ResponseWriter, r *http.Request) { + log.Println("disconnect") + }) + + go func() { + ticker := time.NewTicker(time.Second) + for range ticker.C { + app.Broadcast("/hello", []byte("hello world")) + log.Println("broadcast") + } + }() + + if err := app.Run(); err != nil { + panic(err) + } +} +``` + +1. OnConnect: Triggered when a client connects to the server. +2. OnSend: Triggered when the server sends a message to the client. +3. OnDisconnect: Triggered when a client disconnects from the server.