Skip to content

Commit

Permalink
special fir events: onsocketconnected, onsocketdisconnected (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
adnaan authored Mar 3, 2024
1 parent 56184ee commit 0f08ac9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/counter-ticker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ func (i *index) Options() fir.RouteOptions {
fir.OnEvent("inc", i.inc),
fir.OnEvent("dec", i.dec),
fir.OnEvent("updated", i.updated),
fir.OnEvent(fir.EventSocketConnected, func(ctx fir.RouteContext) error {
var status fir.SocketStatus
err := ctx.Bind(&status)
if err != nil {
return err
}
fmt.Printf("onevent: socket connected for user %s\n", status.User)

return nil
}),
fir.OnEvent(fir.EventSocketDisconnected, func(ctx fir.RouteContext) error {
var status fir.SocketStatus
err := ctx.Bind(&status)
if err != nil {
return err
}
fmt.Printf("onevent: socket disconnected for user %s\n", status.User)
return nil
}),

fir.EventSender(i.eventSender),
}
}
Expand Down
67 changes: 67 additions & 0 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ const (

// Maximum message size allowed from peer.
maxMessageSize = 1024

EventSocketConnected = "fir_socket_connected"
EventSocketDisconnected = "fir_socket_disconnected"
)

type SocketStatus struct {
Connected bool
User string
}

// RedirectUnauthorisedWebSocket sends a 4001 close message to the client
// It sends the redirect url in the close message payload
// If the request is not a websocket request or has error upgrading and writing the close message, it returns false
Expand Down Expand Up @@ -216,6 +224,41 @@ func onWebsocket(w http.ResponseWriter, r *http.Request, cntrl *controller) {
return nil
})

for _, route := range cntrl.routes {
if route.onEvents[EventSocketConnected] == nil {
continue
}

connectedParams := SocketStatus{
Connected: true,
User: connectedUser,
}
paramBytes, err := json.Marshal(connectedParams)
if err != nil {
logger.Errorf("error: marshaling connectedParams %+v, err %v", connectedParams, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

connectedEvent := Event{
ID: EventSocketConnected,
SessionID: &sessionID,
Params: paramBytes,
Timestamp: time.Now().UTC().UnixMilli(),
}

go func(ev Event) {
for {
select {
case route.eventSender <- ev:
return
default:
time.Sleep(10 * time.Millisecond)
}
}
}(connectedEvent)
}

writePumpDone := make(chan struct{})
go writePump(conn, writePumpDone, send)

Expand Down Expand Up @@ -323,6 +366,30 @@ loop:

close(writePumpDone)
conn.Close()
for _, route := range cntrl.routes {

if route.onEvents[EventSocketDisconnected] == nil {
continue
}
connectedParams := SocketStatus{
Connected: false,
User: connectedUser,
}
paramBytes, err := json.Marshal(connectedParams)
if err != nil {
logger.Errorf("error: marshaling connectedParams %+v, err %v", connectedParams, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

route.eventSender <- Event{
ID: EventSocketDisconnected,
SessionID: &sessionID,
Params: paramBytes,
Timestamp: time.Now().UTC().UnixMilli(),
}
}

}

func renderAndWriteEvent(send chan []byte, channel string, ctx RouteContext, pubsubEvent pubsub.Event) error {
Expand Down

0 comments on commit 0f08ac9

Please sign in to comment.