-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamhub.go
564 lines (497 loc) · 13.2 KB
/
jamhub.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package main
import (
"encoding/json"
"fmt"
"log"
"math"
"os"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"net/http"
"bytes"
"errors"
"math/rand"
"strconv"
"strings"
)
var (
errNotImplemented = errors.New("not implemented")
)
const millisInSecond = 1000
const nsInSecond = 1000000
// FromUnixMilli Converts Unix Epoch from milliseconds to time.Time
func FromUnixMilli(ms int64) time.Time {
return time.Unix(ms/int64(millisInSecond), (ms%int64(millisInSecond))*int64(nsInSecond))
}
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
// Maximum message size allowed from peer.
maxMessageSize = 51200
)
var (
newline = []byte{'\n'}
space = []byte{' '}
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// Rooms is a set of rooms
type Rooms struct {
rooms map[string]*Room
}
var errNotFound = errors.New("not found")
// Get room by room id
func (r *Rooms) Get(roomID string) (*Room, error) {
if room, exists := r.rooms[roomID]; exists {
return room, nil
}
return nil, errNotFound
}
// GetOrCreate creates room if it does not exist
func (r *Rooms) GetOrCreate(roomID string) *Room {
room, err := r.Get(roomID)
if err == nil {
return room
}
newRoom := NewRoom(roomID)
r.AddRoom(roomID, newRoom)
go newRoom.run()
return newRoom
}
// AddRoom adds room to rooms list
func (r *Rooms) AddRoom(roomID string, room *Room) error {
if _, exists := r.rooms[roomID]; exists {
return errors.New("room with id " + roomID + " already exists")
}
r.rooms[roomID] = room
return nil
}
// RemoveRoom remove room from rooms list
func (r *Rooms) RemoveRoom(roomID string) error {
if _, exists := r.rooms[roomID]; exists {
delete(r.rooms, roomID)
return nil
}
return nil
}
// RoomsStats is an app global statistics
type RoomsStats struct {
Online int `json:"online"`
Rooms []*RoomWrap `json:"rooms"`
}
// GetStats get app statistics
func (r *Rooms) GetStats() RoomsStats {
stats := RoomsStats{
Rooms: []*RoomWrap{},
}
for _, room := range r.rooms {
stats.Online += room.GetUsersCount()
stats.Rooms = append(stats.Rooms, room.Wrap(nil))
}
return stats
}
// NewRooms creates rooms instance
func NewRooms() *Rooms {
return &Rooms{
rooms: make(map[string]*Room, 100),
}
}
// User is a middleman between the websocket connection and the hub.
type User struct {
ID string
room *Room
conn *websocket.Conn // The websocket connection.
send chan []byte // Buffered channel of outbound messages.
closed bool
info UserInfo
}
// UserInfo contains some user data
type UserInfo struct {
Name string `json:"name"` // emoji-face like on clients (for test)
Mute bool `json:"mute"`
}
// UserWrap represents user object sent to client
type UserWrap struct {
ID string `json:"id"`
UserInfo
}
// Wrap wraps user
func (u *User) Wrap() *UserWrap {
return &UserWrap{
ID: u.ID,
UserInfo: u.info,
}
}
// readPump pumps messages from the websocket connection to the hub.
func (u *User) readPump() {
defer func() {
u.closed = true
u.room.Leave(u)
u.conn.Close()
}()
u.conn.SetReadLimit(maxMessageSize)
u.conn.SetReadDeadline(time.Now().Add(pongWait))
u.conn.SetPongHandler(func(string) error { u.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, message, err := u.conn.ReadMessage()
if err != nil {
log.Println(err)
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
log.Println(err)
}
break
}
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
go func() {
err := u.HandleEvent(message)
if err != nil {
log.Println(err)
u.SendErr(err)
}
}()
}
}
// writePump pumps messages from the hub to the websocket connection.
//
// A goroutine running writePump is started for each connection. The
// application ensures that there is at most one writer to a connection by
// executing all writes from this goroutine.
func (u *User) writePump() {
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
u.closed = true
u.conn.Close()
}()
for {
select {
case message, ok := <-u.send:
u.conn.SetWriteDeadline(time.Now().Add(writeWait))
if !ok {
// The hub closed the channel.
u.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
w, err := u.conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
w.Write(message)
if err := w.Close(); err != nil {
return
}
case <-ticker.C:
u.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err := u.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return
}
}
}
}
// Event represents web socket user event
type Event struct {
Type string `json:"type"`
UserID string `json:"userId"`
Instrument string `json:"instrument,omitempty"`
MIDI *[]int64 `json:"midi,omitempty"`
User *UserWrap `json:"user,omitempty"`
Room *RoomWrap `json:"room,omitempty"`
Desc string `json:"desc,omitempty"`
State string `json:"state,omitempty"`
Value int64 `json:"value"`
}
// HandleEvent handles user event
func (u *User) HandleEvent(eventRaw []byte) error {
var event *Event
err := json.Unmarshal(eventRaw, &event)
if err != nil {
return err
}
// u.log("handle event:", event.Type)
event.UserID = u.ID
if event.Type == "ping" {
pingTimestamp := FromUnixMilli(event.Value)
pingToServer := time.Now().Sub(pingTimestamp)
pingToServerMs := int64(math.Abs(float64(pingToServer.Milliseconds())))
// fmt.Println("PING MS:", pingToServerMs)
u.SendPingEvent(pingToServerMs)
u.BroadcastEventPing(pingToServerMs)
return nil
} else if event.Type == "midi" {
u.SendEvent(*event)
u.BroadcastEvent(*event)
return nil
} else if event.Type == "mute" {
u.info.Mute = true
u.BroadcastEventMute()
return nil
} else if event.Type == "unmute" {
u.info.Mute = false
u.BroadcastEventUnmute()
return nil
} else if event.Type == "sync" {
u.BroadcastEvent(*event)
return nil
}
return u.SendErr(errNotImplemented)
}
// SendEvent sends json body to web socket
func (u *User) SendEvent(event Event) error {
if u.closed == true { // user already disconnected
return nil
}
json, err := json.Marshal(event)
if err != nil {
return err
}
u.send <- json
return nil
}
// SendPingEvent sends ping event
func (u *User) SendPingEvent(ping int64) error {
return u.SendEvent(Event{Type: "ping", UserID: u.ID, Value: ping})
}
// SendEventUser sends user to client to identify himself
func (u *User) SendEventUser() error {
return u.SendEvent(Event{Type: "user", User: u.Wrap()})
}
// SendEventRoom sends room to client with users except me
func (u *User) SendEventRoom() error {
return u.SendEvent(Event{Type: "room", Room: u.room.Wrap(u)})
}
// BroadcastEvent sends json body to everyone in the room except this user
func (u *User) BroadcastEvent(event Event) error {
json, err := json.Marshal(event)
if err != nil {
return err
}
u.room.Broadcast(json, u)
return nil
}
// BroadcastEventPing sends everyone user ping
func (u *User) BroadcastEventPing(ping int64) error {
return u.BroadcastEvent(Event{Type: "ping", UserID: u.ID, Value: ping})
}
// BroadcastEventJoin sends user_join event
func (u *User) BroadcastEventJoin() error {
return u.BroadcastEvent(Event{Type: "user_join", User: u.Wrap()})
}
// BroadcastEventLeave sends user_leave event
func (u *User) BroadcastEventLeave() error {
return u.BroadcastEvent(Event{Type: "user_leave", User: u.Wrap()})
}
// BroadcastEventMute sends microphone mute event to everyone
func (u *User) BroadcastEventMute() error {
return u.BroadcastEvent(Event{Type: "mute", User: u.Wrap()})
}
// BroadcastEventUnmute sends microphone unmute event to everyone
func (u *User) BroadcastEventUnmute() error {
return u.BroadcastEvent(Event{Type: "unmute", User: u.Wrap()})
}
// SendErr sends error in json format to web socket
func (u *User) SendErr(err error) error {
return u.SendEvent(Event{Type: "error", Desc: fmt.Sprint(err)})
}
func (u *User) log(msg ...interface{}) {
log.Println(
fmt.Sprintf("user %s:", u.ID),
fmt.Sprint(msg...),
)
}
// serveWs handles websocket requests from the peer.
func serveWs(rooms *Rooms, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
roomID := strings.ReplaceAll(r.URL.Path, "/", "")
room := rooms.GetOrCreate(roomID)
log.Println("ws connection to room:", roomID, len(room.GetUsers()), "users")
usernames := []string{
"David Bowie",
}
user := &User{
ID: strconv.FormatInt(time.Now().UnixNano(), 10), // generate random id based on timestamp
room: room,
conn: conn,
send: make(chan []byte, 256),
info: UserInfo{
Name: usernames[rand.Intn(len(usernames))],
Mute: true, // user is muted by default
},
}
user.room.Join(user)
// Allow collection of memory referenced by the caller by doing all work in
// new goroutines.
go user.writePump()
go user.readPump()
user.SendEventUser()
user.SendEventRoom()
}
type broadcastMsg struct {
data []byte
user *User // message will be broadcasted to everyone, except this user
}
// Room maintains the set of active clients and broadcasts messages to the
// clients.
type Room struct {
Name string
users map[string]*User
broadcast chan broadcastMsg
join chan *User // Register requests from the clients.
leave chan *User // Unregister requests from clients.
}
// RoomWrap is a public representation of a room
type RoomWrap struct {
Users []*UserWrap `json:"users"`
Name string `json:"name"`
Online int `json:"online"`
}
// Wrap returns public version of room
func (r *Room) Wrap(me *User) *RoomWrap {
usersWrap := []*UserWrap{}
for _, user := range r.GetUsers() {
if me != nil {
// do not add current user to room
if me.ID == user.ID {
continue
}
}
usersWrap = append(usersWrap, user.Wrap())
}
return &RoomWrap{
Users: usersWrap,
Name: r.Name,
Online: len(usersWrap),
}
}
// NewRoom creates new room
func NewRoom(name string) *Room {
return &Room{
broadcast: make(chan broadcastMsg),
join: make(chan *User),
leave: make(chan *User),
users: make(map[string]*User),
Name: name,
}
}
// GetUsers converts map[int64]*User to list
func (r *Room) GetUsers() []*User {
users := []*User{}
for _, user := range r.users {
users = append(users, user)
}
return users
}
// GetOtherUsers returns other users of room except current
func (r *Room) GetOtherUsers(user *User) []*User {
users := []*User{}
for _, userCandidate := range r.users {
if user.ID == userCandidate.ID {
continue
}
users = append(users, userCandidate)
}
return users
}
// Join connects user and room
func (r *Room) Join(user *User) {
r.join <- user
}
// Leave disconnects user and room
func (r *Room) Leave(user *User) {
r.leave <- user
}
// Broadcast sends message to everyone except user (if passed)
func (r *Room) Broadcast(data []byte, user *User) {
message := broadcastMsg{data: data, user: user}
r.broadcast <- message
}
// GetUsersCount return users count in the room
func (r *Room) GetUsersCount() int {
return len(r.GetUsers())
}
func (r *Room) run() {
for {
select {
case user := <-r.join:
r.users[user.ID] = user
go user.BroadcastEventJoin()
case user := <-r.leave:
if _, ok := r.users[user.ID]; ok {
delete(r.users, user.ID)
close(user.send)
}
go user.BroadcastEventLeave()
case message := <-r.broadcast:
for _, user := range r.users {
// message will be broadcasted to everyone, except this user
if message.user != nil && user.ID == message.user.ID {
continue
}
select {
case user.send <- message.data:
default:
close(user.send)
delete(r.users, user.ID)
}
}
}
}
}
func main() {
rooms := NewRooms()
router := mux.NewRouter()
router.HandleFunc("/api/stats", func(w http.ResponseWriter, r *http.Request) {
bytes, err := json.Marshal(rooms.GetStats())
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
}
w.Write(bytes)
}).Methods("GET")
router.HandleFunc("/api/rooms/{id}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Allow-Origin", "*")
vars := mux.Vars(r)
roomID := vars["id"]
room, err := rooms.Get(roomID)
if err == errNotFound {
http.NotFound(w, r)
return
}
bytes, err := json.Marshal(room.Wrap(nil))
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
}
w.Write(bytes)
}).Methods("GET")
router.HandleFunc("/{id}", func(w http.ResponseWriter, r *http.Request) {
serveWs(rooms, w, r)
})
port := os.Getenv("PORT")
if port == "" {
port = "80"
log.Printf("Defaulting to port %s", port)
}
addr := fmt.Sprintf(":%s", port)
srv := &http.Server{
Handler: router,
Addr: addr,
WriteTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
}
log.Println("server is running on:", port)
log.Fatal(srv.ListenAndServe())
}