BareRTC/pkg/websocket.go

83 lines
2.2 KiB
Go
Raw Permalink Normal View History

2023-01-11 06:38:48 +00:00
package barertc
import (
"context"
"fmt"
"net/http"
"time"
2023-03-22 04:29:24 +00:00
"git.kirsle.net/apps/barertc/pkg/config"
2023-01-11 06:38:48 +00:00
"git.kirsle.net/apps/barertc/pkg/log"
2023-08-14 02:21:27 +00:00
"git.kirsle.net/apps/barertc/pkg/messages"
"git.kirsle.net/apps/barertc/pkg/util"
2023-01-11 06:38:48 +00:00
"nhooyr.io/websocket"
)
2023-03-22 04:29:24 +00:00
// WebSocket handles the /ws websocket connection endpoint.
2023-01-11 06:38:48 +00:00
func (s *Server) WebSocket() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip := util.IPAddress(r)
log.Info("WebSocket connection from %s - %s", ip, r.Header.Get("User-Agent"))
log.Debug("Headers: %+v", r.Header)
2023-03-31 19:40:55 +00:00
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
CompressionMode: websocket.CompressionDisabled,
})
2023-01-11 06:38:48 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Could not accept websocket connection: %s", err)
2023-01-11 06:38:48 +00:00
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
log.Debug("WebSocket: %s has connected", ip)
2023-03-22 04:29:24 +00:00
c.SetReadLimit(config.Current.WebSocketReadLimit)
2023-01-11 06:38:48 +00:00
// CloseRead starts a goroutine that will read from the connection
// until it is closed.
// ctx := c.CloseRead(r.Context())
ctx, cancel := context.WithCancel(r.Context())
2023-01-11 06:38:48 +00:00
2023-12-11 02:43:18 +00:00
sub := s.NewWebSocketSubscriber(ctx, c, cancel)
2023-01-11 06:38:48 +00:00
s.AddSubscriber(sub)
defer s.DeleteSubscriber(sub)
2023-01-11 06:38:48 +00:00
go sub.ReadLoop(s)
pinger := time.NewTicker(PingInterval)
2023-01-11 06:38:48 +00:00
for {
select {
case msg := <-sub.messages:
2023-09-30 19:46:45 +00:00
err = writeTimeout(ctx, time.Second*time.Duration(config.Current.WebSocketSendTimeout), c, msg)
2023-01-11 06:38:48 +00:00
if err != nil {
return
}
2023-02-11 06:46:39 +00:00
case <-pinger.C:
// Send a ping, and a refreshed JWT token if the user sent one.
var token string
if sub.JWTClaims != nil {
if jwt, err := sub.JWTClaims.ReSign(); err != nil {
log.Error("ReSign JWT token for %s#%d: %s", sub.Username, sub.ID, err)
} else {
token = jwt
}
}
2023-08-14 02:21:27 +00:00
sub.SendJSON(messages.Message{
Action: messages.ActionPing,
JWTToken: token,
})
2023-01-11 06:38:48 +00:00
case <-ctx.Done():
pinger.Stop()
2023-01-11 06:38:48 +00:00
return
}
}
})
}
func writeTimeout(ctx context.Context, timeout time.Duration, c *websocket.Conn, msg []byte) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return c.Write(ctx, websocket.MessageText, msg)
}