fixed logs and config locations, updated connection closure handling
This commit is contained in:
@@ -2,6 +2,7 @@ package ws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"homestead/homestead_gateway/util"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -92,8 +93,7 @@ func (r *Registry) RegisterMod(channelID, serverID string, conn *websocket.Conn)
|
||||
defer e.mu.Unlock()
|
||||
|
||||
if e.Mod != nil && e.Mod.Conn != nil {
|
||||
|
||||
_ = e.Mod.Conn.Close()
|
||||
util.CloseConn(e.Mod.Conn)
|
||||
}
|
||||
|
||||
e.Mod = &ConnWrapper{Conn: conn, ServerID: serverID, LastSeen: time.Now()}
|
||||
@@ -106,7 +106,7 @@ func (r *Registry) RegisterBot(conn *websocket.Conn) {
|
||||
r.botMu.Lock()
|
||||
|
||||
if r.bot != nil && r.bot.Conn != nil {
|
||||
_ = r.bot.Conn.Close()
|
||||
r.UnregisterBot()
|
||||
}
|
||||
|
||||
r.bot = &ConnWrapper{Conn: conn, LastSeen: time.Now()}
|
||||
@@ -127,7 +127,7 @@ func (r *Registry) UnregisterMod(channelID string) {
|
||||
e.mu.Unlock()
|
||||
|
||||
if modConn != nil && modConn.Conn != nil {
|
||||
closeConn(modConn.Conn)
|
||||
util.CloseConn(modConn.Conn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ func (r *Registry) UnregisterBot() {
|
||||
r.botMu.Unlock()
|
||||
|
||||
if botConn != nil && botConn.Conn != nil {
|
||||
closeConn(botConn.Conn)
|
||||
util.CloseConn(botConn.Conn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +152,6 @@ func (r *Registry) Send(channelID string, out GatewayMessageOut, sendOverConn fu
|
||||
if err := sendOverConn(b.Conn, out); err == nil {
|
||||
return true, false, nil
|
||||
}
|
||||
_ = b.Conn.Close()
|
||||
r.UnregisterBot()
|
||||
}
|
||||
|
||||
@@ -176,7 +175,6 @@ func (r *Registry) Send(channelID string, out GatewayMessageOut, sendOverConn fu
|
||||
if err := sendOverConn(mod.Conn, out); err == nil {
|
||||
return true, false, nil
|
||||
}
|
||||
_ = mod.Conn.Close()
|
||||
r.UnregisterMod(channelID)
|
||||
}
|
||||
|
||||
|
||||
15
ws/util.go
15
ws/util.go
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"homestead/homestead_gateway/util"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -55,7 +56,7 @@ func (wsg *WebsocketGateway) sendWebsocketError(conn *websocket.Conn, message st
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(5 * time.Second))
|
||||
_ = conn.WriteJSON(map[string]interface{}{"message": message, "code": code})
|
||||
if close {
|
||||
_ = conn.Close()
|
||||
util.CloseConn(conn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +65,7 @@ func (wsg *WebsocketGateway) sendWebsocketResponse(conn *websocket.Conn, content
|
||||
|
||||
if err := conn.WriteJSON(content); err != nil {
|
||||
wsg.logger.Error("Failed to respond to connection.", "remote", conn.RemoteAddr().String(), "err", err)
|
||||
_ = conn.Close()
|
||||
util.CloseConnWithControlMessage(conn, websocket.CloseAbnormalClosure, "Connection error.")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -108,16 +109,6 @@ func (wsg *WebsocketGateway) loggingMiddleware(next http.Handler) http.Handler {
|
||||
|
||||
// connections
|
||||
|
||||
func closeConn(conn *websocket.Conn) {
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(time.Second))
|
||||
_ = conn.WriteControl(
|
||||
websocket.CloseMessage,
|
||||
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Disconnecting."),
|
||||
time.Now().Add(time.Second),
|
||||
)
|
||||
_ = conn.Close()
|
||||
}
|
||||
|
||||
func (wsg *WebsocketGateway) registerConn(conn *websocket.Conn, typ, channelId, serverId string) bool {
|
||||
if typ == "bot" {
|
||||
wsg.registry.botMu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user