-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
hub.go
205 lines (174 loc) · 4.47 KB
/
hub.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package room
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/EliCDavis/vector/vector3"
"github.com/EliCDavis/vector/vector4"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
var defaultDisplayNames = []string{
"Peggy Hill",
"Madara Uchiha",
"Dr. Eggman",
"Eustace Bagge",
"Buttercup",
"Professor Utonium",
"Schnitzel",
"Eduardo",
"Mandy",
"Captain K'nuckles",
"Snufkin",
"Mortimer",
"Bella",
}
type Player struct {
Name string `json:"name"`
Position vector3.Float64 `json:"position"`
Rotation vector4.Float64 `json:"rotation"`
Pointer *vector3.Float64 `json:"pointer,omitempty"`
}
type RoomState struct {
Players map[string]*Player
WebScene *WebScene
ModelVersion int
}
type clientUpdate struct {
client *Client
update Message
}
// Hub maintains the set of active clients and broadcasts messages to the
// clients.
type Hub struct {
// Registered clients.
clients map[*Client]string
// Inbound messages from the clients.
broadcast chan []byte
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
clientUpdates chan clientUpdate
sceneUpdate chan time.Time
state RoomState
modelVersion *int
}
func NewHub(webScene *WebScene, modelVersion *int) *Hub {
return &Hub{
broadcast: make(chan []byte),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[*Client]string),
clientUpdates: make(chan clientUpdate),
sceneUpdate: make(chan time.Time),
state: RoomState{
Players: map[string]*Player{},
WebScene: webScene,
},
modelVersion: modelVersion,
}
}
func (h *Hub) Run() {
go func() {
for {
time.Sleep(time.Millisecond * 200)
h.sceneUpdate <- time.Now()
}
}()
for {
select {
case client := <-h.register:
clientID := randString(10)
h.clients[client] = clientID
h.state.Players[clientID] = &Player{
Name: defaultDisplayNames[rand.Intn(len(defaultDisplayNames))],
}
client.send <- Message{
Type: ServerSetClientIDMessageType,
Data: []byte(fmt.Sprintf("\"%s\"", clientID)),
}
case client := <-h.unregister:
if id, ok := h.clients[client]; ok {
delete(h.clients, client)
delete(h.state.Players, id)
close(client.send)
}
case message := <-h.broadcast:
for client := range h.clients {
select {
case client.send <- Message{
Type: ServerBroadcastMessageType,
Data: message,
}:
default:
close(client.send)
delete(h.clients, client)
}
}
case clientUpdate := <-h.clientUpdates:
clientID := h.clients[clientUpdate.client]
update := clientUpdate.update
switch update.Type {
case ClientSetDisplayNameMessageType:
h.state.Players[clientID].Name = update.ClientSetDisplayNameData()
case ClientSetOrientationMessageType:
orientation, err := update.ClientSetOrientationData()
if err != nil {
panic(fmt.Errorf("unable to set orientation data: %w", err))
}
h.state.Players[clientID].Position = orientation.Position
h.state.Players[clientID].Rotation = orientation.Rotation
case ClientSetSceneMessageType:
scene, err := update.ClientSetSceneData()
if err != nil {
panic(fmt.Errorf("unable to set scene data: %w", err))
}
h.state.WebScene = &scene
}
case <-h.sceneUpdate:
h.state.ModelVersion = *h.modelVersion
message, err := json.Marshal(h.state)
if err != nil {
panic(err)
}
for client := range h.clients {
select {
case client.send <- Message{
Type: ServerRoomStateUpdateMessageType,
Data: message,
}:
default:
close(client.send)
delete(h.clients, client)
}
}
}
}
}
// serveWs handles websocket requests from the peer.
func (hub *Hub) ServeWs(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
client := &Client{hub: hub, conn: conn, send: make(chan Message, 256)}
client.hub.register <- client
// Allow collection of memory referenced by the caller by doing all work in
// new goroutines.
go client.writePump()
go client.readPump()
}