/
websockserver.go
93 lines (79 loc) · 1.95 KB
/
websockserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package model
import (
"encoding/json"
"log"
"net/http"
"unsafe"
"github.com/google/uuid"
"github.com/gorilla/websocket"
)
type WebSocketServer struct {
socketClient SockClient[Action]
wsConnections map[string]websocket.Conn
}
func NewWebSocketServer(config SockClientConfig) WebSocketServer {
return WebSocketServer{
wsConnections: map[string]websocket.Conn{},
socketClient: SockClient[Action]{
Config: config,
Handler: wsHandler,
},
}
}
type IdExchange struct {
ConnectionId string `json:"connectionId"`
}
var wsHandler = CreateHandler(
"UiConnector",
func(action Action) bool {
return action.IsGroup("ui")
},
func(action Action) (Action, bool) {
return action, true
},
func(res Action) []Action {
f := (*Action)(unsafe.Pointer(&res))
return []Action{*f}
},
"",
)
func wsConnectionHandler(wss *WebSocketServer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var upgrader = websocket.Upgrader{}
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
connection, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
defer connection.Close()
connId := uuid.New().String()
connection.WriteJSON(Action{
Type: "UI:WELCOME",
Payload: IdExchange{
ConnectionId: connId,
}},
)
wss.wsConnections[connId] = *connection
// SockClient writes message to the web socket
wss.socketClient.SendFn = func(action Action) {
connection.WriteJSON(action)
}
for {
_, message, err := connection.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
var uiAction Action
json.Unmarshal(message, &uiAction)
log.Printf("recv: %s\n", message)
wss.socketClient.Send(uiAction)
}
}
}
func (wss *WebSocketServer) Run() {
go wss.socketClient.Connect()
http.HandleFunc("/ws", wsConnectionHandler(wss))
log.Println("Starting WebSocket server on 0.0.0.0:8484")
log.Fatal(http.ListenAndServe("0.0.0.0:8484", nil))
}