mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 11:15:13 -06:00
35 lines
518 B
Go
35 lines
518 B
Go
package websocket
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
type BaseMessage struct {
|
|
Type string `json:"type"`
|
|
Id int64 `json:"id"`
|
|
}
|
|
|
|
type ChanMsg struct {
|
|
Id int64
|
|
Type string
|
|
Raw []byte
|
|
}
|
|
|
|
func ListenWebsocket(conn *websocket.Conn, ctx context.Context, c chan ChanMsg) {
|
|
for {
|
|
bytes, _ := ReadMessage(conn, ctx)
|
|
base := BaseMessage{}
|
|
json.Unmarshal(bytes, &base)
|
|
chanMsg := ChanMsg{
|
|
Type: base.Type,
|
|
Id: base.Id,
|
|
Raw: bytes,
|
|
}
|
|
|
|
c <- chanMsg
|
|
}
|
|
}
|