Gateway working, beta
This commit is contained in:
@@ -2,7 +2,6 @@ package ws
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"homestead/homestead_gateway/util/cache"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -47,52 +46,53 @@ func (wsg *WebsocketGateway) handleSync(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
meta := cache.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, true)
|
||||
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 {
|
||||
if mhs.ServerID == "" || mhs.ChannelID == "" {
|
||||
wsg.sendWebsocketError(conn, "Malformed mod handshake.", 400, true)
|
||||
return
|
||||
}
|
||||
|
||||
wsg.registerConn(conn, meta, "mod")
|
||||
wsg.logger.Info("Mod connected via Websocket.", "remote", conn.RemoteAddr().String(), "server_id", mhs.ServerID)
|
||||
if !wsg.registerConn(conn, "mod", mhs.ChannelID, mhs.ServerID) {
|
||||
wsg.sendWebsocketError(conn, "Failed to register mod.", 500, true)
|
||||
return
|
||||
}
|
||||
|
||||
go wsg.read(conn, meta, "mod")
|
||||
_ = wsg.sendWebsocketResponse(conn, GatewayAck{Status: "connected", Type: "mod"})
|
||||
wsg.registry.FlushChannelWithSender(mhs.ChannelID, wsg.flush)
|
||||
|
||||
wsg.logger.Info("Mod connected via Websocket.", "remote", conn.RemoteAddr().String())
|
||||
go wsg.read(conn, "mod", mhs.ChannelID)
|
||||
|
||||
case "bot":
|
||||
var bhs BotHandshake
|
||||
|
||||
if err := json.Unmarshal(handshake.Data, &bhs); err != nil {
|
||||
wsg.sendWebsocketError(conn, "Malformed bot handshake.", 400, true)
|
||||
wsg.logger.Warn("Malformed bot handshake.", "remote", conn.RemoteAddr().String(), "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
meta.ID = bhs.BotID
|
||||
if bhs.ChannelId == "" {
|
||||
wsg.sendWebsocketError(conn, "Malformed bot handshake.", 400, true)
|
||||
return
|
||||
}
|
||||
|
||||
if ok := wsg.registerConn(conn, meta, "bot"); !ok {
|
||||
if !wsg.registerConn(conn, "bot", bhs.ChannelId, "") {
|
||||
wsg.sendWebsocketError(conn, "Bot already connected.", 409, true)
|
||||
return
|
||||
}
|
||||
|
||||
if err = wsg.sendWebsocketResponse(conn, GatewayAck{Status: "connected", Type: "bot"}); err != nil {
|
||||
return
|
||||
}
|
||||
_ = wsg.sendWebsocketResponse(conn, GatewayAck{Status: "connected", Type: "bot"})
|
||||
wsg.registry.FlushAllToBotWithSender(wsg.flush)
|
||||
|
||||
wsg.logger.Info("Bot connected via Websocket.", "remote", conn.RemoteAddr().String(), "bot_id", bhs.BotID)
|
||||
|
||||
go wsg.read(conn, meta, "bot")
|
||||
wsg.logger.Info("Bot connected via Websocket.", "remote", conn.RemoteAddr().String())
|
||||
go wsg.read(conn, "bot", bhs.ChannelId)
|
||||
|
||||
default:
|
||||
wsg.sendWebsocketError(conn, "Unknown handshake.", 400, true)
|
||||
|
||||
Reference in New Issue
Block a user