Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
snowmerak committed Jun 26, 2023
1 parent bf0c332 commit d926c1c
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

0 comments on commit d926c1c

Please sign in to comment.