2024-07-23 17:30:29 +00:00
|
|
|
package relay
|
2024-07-20 12:41:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-07-23 17:57:21 +00:00
|
|
|
"grain/relay/handlers"
|
2024-07-22 17:50:43 +00:00
|
|
|
|
2024-07-20 12:41:47 +00:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
2024-07-23 17:24:44 +00:00
|
|
|
func Listener(ws *websocket.Conn) {
|
2024-07-20 12:41:47 +00:00
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
var msg string
|
|
|
|
for {
|
|
|
|
err := websocket.Message.Receive(ws, &msg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error receiving message:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Received message:", msg)
|
|
|
|
|
|
|
|
var message []interface{}
|
|
|
|
err = json.Unmarshal([]byte(msg), &message)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error parsing message:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(message) < 2 {
|
|
|
|
fmt.Println("Invalid message format")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
messageType, ok := message[0].(string)
|
|
|
|
if !ok {
|
|
|
|
fmt.Println("Invalid message type")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch messageType {
|
|
|
|
case "EVENT":
|
2024-07-23 17:57:21 +00:00
|
|
|
handlers.HandleEvent(ws, message)
|
2024-07-20 12:41:47 +00:00
|
|
|
case "REQ":
|
2024-07-23 17:57:21 +00:00
|
|
|
handlers.HandleReq(ws, message)
|
2024-07-20 12:41:47 +00:00
|
|
|
case "CLOSE":
|
2024-07-23 17:57:21 +00:00
|
|
|
handlers.HandleClose(ws, message)
|
2024-07-20 12:41:47 +00:00
|
|
|
default:
|
|
|
|
fmt.Println("Unknown message type:", messageType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|