Gateway working, beta

This commit is contained in:
2025-12-07 17:11:37 +01:00
parent de03c1fe3d
commit 3d6774586e
13 changed files with 616 additions and 506 deletions

36
sim.go
View File

@@ -18,6 +18,8 @@ const (
gatewayURL = "ws://localhost:3333/sync"
apiKey = "gateway"
serverID = "test-server-001"
// THE CHANNEL ID the mod says it serves. Must match gateway expectation.
channelID = "123456789"
)
type Handshake struct {
@@ -25,8 +27,10 @@ type Handshake struct {
Data json.RawMessage `json:"data"`
}
// ModHandshake now includes ChannelID
type ModHandshake struct {
ServerID string `json:"server_id"`
ServerID string `json:"server_id"`
ChannelID string `json:"channel_id"`
}
type GatewayAck struct {
@@ -58,7 +62,6 @@ func main() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
// Build WebSocket URL with API key
u, err := url.Parse(gatewayURL)
if err != nil {
log.Fatalf("Failed to parse URL: %v", err)
@@ -69,7 +72,6 @@ func main() {
log.Printf("Connecting to %s", u.String())
// Connect to WebSocket
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatalf("Failed to connect: %v", err)
@@ -78,7 +80,8 @@ func main() {
log.Println("Connected to gateway")
// Set up ping handler - respond to pings from server
// respond to pings (server ping -> client must pong). Using SetPingHandler is fine,
// but WriteControl for Pong is acceptable too.
conn.SetPingHandler(func(appData string) error {
log.Println("Received ping from server, sending pong")
err := conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(5*time.Second))
@@ -89,8 +92,11 @@ func main() {
return nil
})
// Send handshake
modHS := ModHandshake{ServerID: serverID}
// Build and send handshake including channel id
modHS := ModHandshake{
ServerID: serverID,
ChannelID: channelID,
}
modHSData, err := json.Marshal(modHS)
if err != nil {
log.Fatalf("Failed to marshal mod handshake: %v", err)
@@ -104,21 +110,17 @@ func main() {
if err := conn.WriteJSON(handshake); err != nil {
log.Fatalf("Failed to send handshake: %v", err)
}
log.Println("Handshake sent")
// Read acknowledgment
// Read acknowledgment (some servers might not reply with JSON; handle errors)
var ack GatewayAck
if err := conn.ReadJSON(&ack); err != nil {
log.Fatalf("Failed to read acknowledgment: %v", err)
}
log.Printf("Received acknowledgment: status=%q, type=%q", ack.Status, ack.Type)
log.Printf("Received acknowledgment: status=%s, type=%s", ack.Status, ack.Type)
// Channel for incoming messages
done := make(chan struct{})
// Read loop - handles incoming messages and processes control frames
go func() {
defer close(done)
for {
@@ -131,21 +133,19 @@ func main() {
}
return
}
// Only log text/binary messages (ping/pong handled by handlers)
if messageType == websocket.TextMessage || messageType == websocket.BinaryMessage {
log.Printf("Received from server: %s", string(message))
}
}
}()
// Optional: Send a test message after connecting
time.Sleep(2 * time.Second)
// Optional: send a test message after connecting
time.Sleep(1 * time.Second)
testMsg := GatewayMessageIn{
MsgID: "test-msg-001",
ID: serverID,
Destination: Destination{
ID: "123456789",
ID: channelID,
},
Author: User{
ID: "player-uuid-123",
@@ -163,7 +163,6 @@ func main() {
log.Println("Connection established. Responding to pings. Press Ctrl+C to disconnect.")
// Wait for interrupt or connection close
for {
select {
case <-done:
@@ -172,7 +171,6 @@ func main() {
case <-interrupt:
log.Println("Interrupt received, closing connection...")
// Send close message
err := conn.WriteMessage(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),