temp push
This commit is contained in:
29
ws/util.go
29
ws/util.go
@@ -105,17 +105,33 @@ func loggingMiddleware(logger *slog.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
next.ServeHTTP(w, r)
|
||||
logger.Info("http request", "remote", r.RemoteAddr, "method", r.Method, "path", r.URL.Path, "duration", time.Since(start))
|
||||
logger.Info("Incoming HTTP request.", "remote", r.RemoteAddr, "path", r.URL.Path, "duration", time.Since(start))
|
||||
})
|
||||
}
|
||||
|
||||
// connections
|
||||
|
||||
func (wsg *WebsocketGateway) registerConn(conn *websocket.Conn, meta cache.ConnectionMetaData) {
|
||||
func (wsg *WebsocketGateway) registerConn(conn *websocket.Conn, meta cache.ConnectionMetaData, typ string) bool {
|
||||
if typ == "bot" {
|
||||
if wsg.bot != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
wsg.bot = conn
|
||||
return true
|
||||
}
|
||||
|
||||
wsg.conns.Set(meta.ID, conn, &meta)
|
||||
return true
|
||||
}
|
||||
|
||||
func (wsg *WebsocketGateway) unregisterConn(conn *websocket.Conn, meta cache.ConnectionMetaData) {
|
||||
func (wsg *WebsocketGateway) unregisterConn(conn *websocket.Conn, meta cache.ConnectionMetaData, typ string) {
|
||||
if typ == "bot" {
|
||||
_ = wsg.bot.Close()
|
||||
wsg.bot = nil
|
||||
return
|
||||
}
|
||||
|
||||
wsg.conns.RemoveById(meta.ID)
|
||||
_ = conn.Close()
|
||||
}
|
||||
@@ -123,8 +139,15 @@ func (wsg *WebsocketGateway) unregisterConn(conn *websocket.Conn, meta cache.Con
|
||||
func (wsg *WebsocketGateway) closeAll() {
|
||||
wsg.logger.Info("Closing all websocket connections.")
|
||||
|
||||
if wsg.bot != nil {
|
||||
_ = wsg.bot.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Shutting down."), time.Now().Add(time.Second))
|
||||
_ = wsg.bot.Close()
|
||||
}
|
||||
|
||||
wsg.conns.Range(func(id string, c *websocket.Conn, meta *cache.ConnectionMetaData) {
|
||||
_ = c.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Shutting down."), time.Now().Add(time.Second))
|
||||
_ = c.Close()
|
||||
})
|
||||
|
||||
wsg.conns.Clear()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user