working Gateway for incoming Mod/Bot ws-conn's; simulated Mod client via sim.go
This commit is contained in:
106
ws/handlers.go
106
ws/handlers.go
@@ -1 +1,107 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
func (wsg *WebsocketGateway) handlePush(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := wsg.validateAndUpgradeConnection(w, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if wsg.bodySizeBytes > 0 {
|
||||
conn.SetReadLimit(wsg.bodySizeBytes)
|
||||
} else {
|
||||
conn.SetReadLimit(1 << 20) // sensible default 1MiB
|
||||
}
|
||||
|
||||
_ = conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
||||
conn.SetPongHandler(func(appData string) error {
|
||||
_ = conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
||||
return nil
|
||||
})
|
||||
|
||||
typ, data, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
wsg.sendWebsocketError(conn, "Internal Server Error", 500)
|
||||
wsg.logger.Error("Failed to read handshake.", "remote", conn.RemoteAddr().String(), "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
if typ != websocket.TextMessage && typ != websocket.BinaryMessage {
|
||||
wsg.sendWebsocketError(conn, "First message must be a handshake.", 400)
|
||||
wsg.logger.Warn("Invalid handshake message type.", "remote", conn.RemoteAddr().String())
|
||||
return
|
||||
}
|
||||
|
||||
var handshake Handshake
|
||||
if err := json.Unmarshal(data, &handshake); err != nil {
|
||||
wsg.sendWebsocketError(conn, "Malformed handshake.", 400)
|
||||
wsg.logger.Warn("Malformed handshake.", "remote", conn.RemoteAddr().String(), "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
meta := connectionMetaData{connectionType: handshake.Type}
|
||||
|
||||
switch handshake.Type {
|
||||
case "mod":
|
||||
var mhs ModHandshake
|
||||
|
||||
if err := json.Unmarshal(handshake.Data, &mhs); err != nil {
|
||||
wsg.sendWebsocketError(conn, "Malformed mod handshake.", 400)
|
||||
wsg.logger.Warn("Malformed mod handshake.", "remote", conn.RemoteAddr().String(), "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
meta.id = mhs.ServerID
|
||||
|
||||
if err = wsg.sendWebsocketResponse(conn, GatewayAck{Status: "connected", Type: "mod"}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
wsg.registerConn(conn, meta)
|
||||
wsg.logger.Info("Mod connected via Websocket.", "remote", conn.RemoteAddr().String(), "server_id", mhs.ServerID)
|
||||
|
||||
go wsg.modReadLoop(conn, meta) // replace with external handler mayhaps
|
||||
|
||||
case "bot":
|
||||
var bhs BotHandshake
|
||||
|
||||
if err := json.Unmarshal(handshake.Data, &bhs); err != nil {
|
||||
wsg.sendWebsocketError(conn, "Malformed bot handshake.", 400)
|
||||
wsg.logger.Warn("Malformed bot handshake.", "remote", conn.RemoteAddr().String(), "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
meta.id = bhs.BotID
|
||||
|
||||
if err = wsg.sendWebsocketResponse(conn, GatewayAck{Status: "connected", Type: "bot"}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
wsg.registerConn(conn, meta)
|
||||
wsg.logger.Info("Bot connected via Websocket.", "remote", conn.RemoteAddr().String(), "bot_id", bhs.BotID)
|
||||
|
||||
go wsg.botReadLoop(conn, meta) // replace with external handler mayhaps
|
||||
|
||||
default:
|
||||
wsg.sendWebsocketError(conn, "Unknown handshake.", 400)
|
||||
wsg.logger.Warn("Unknown connection type.", "remote", conn.RemoteAddr().String(), "type", handshake.Type)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (wsg *WebsocketGateway) handleReady(w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
func (wsg *WebsocketGateway) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{"status": "healthy"})
|
||||
}
|
||||
|
||||
func (wsg *WebsocketGateway) handleRegister(w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
Reference in New Issue
Block a user