-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
174 lines (140 loc) · 4.17 KB
/
app.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
package main
import (
"code.google.com/p/go-uuid/uuid"
"code.google.com/p/go.net/websocket"
"encoding/json"
"io"
"log"
"net/http"
"time"
)
var availableRooms = make(AvailableRooms)
var statsChannel = make(chan bool)
func websocketHandler(ws *websocket.Conn) {
defer func() {
ws.Close()
}()
var gm GenericMessage
var room *Room
var sessionId string = uuid.New()
log.Println("Client connected:", sessionId)
for {
err := websocket.JSON.Receive(ws, &gm)
if err == io.EOF {
room.RemoveClient(sessionId)
log.Println("Client disconencted", sessionId)
if len(room.Clients) == 0 {
log.Println("Removing room", room.RoomId)
availableRooms.Remove(room.RoomId)
}
statsChannel <- true
return
} else if err != nil {
log.Println(err)
} else {
switch gm.Type {
case "new_room":
room = newRoom(gm.Data, ws, sessionId)
log.Println("New room:", room.RoomId)
case "join_room":
room = joinRoom(gm.Data, ws, sessionId)
log.Println("Join room:", room.RoomId)
case "offer_description":
time.Sleep(1 * time.Second)
log.Println("Received OFFER description.")
d := NewRoomMessageData{}
if err := json.Unmarshal(gm.Data, &d); err == nil {
desc := &DescriptionMessage{Type: "caller_description", Description: d.Description}
room.SendToOutClients(desc, sessionId)
}
case "caller_ice_candidate":
ice := &IceCandidate{}
if err := json.Unmarshal(gm.Data, ice); err == nil {
// log.Println("CALLER ICE:", strings.Split(ice.Candidate, " ")[2], strings.Split(ice.Candidate, " ")[4])
ic := &IceCandidateMessage{Type: "caller_ice_candidate", Candidate: *ice}
room.SendToInClients(ic, sessionId)
}
case "offer_ice_candidate":
ice := &IceCandidate{}
if err := json.Unmarshal(gm.Data, ice); err == nil {
// log.Println("OFFER ICE:", strings.Split(ice.Candidate, " ")[2], strings.Split(ice.Candidate, " ")[4])
ic := &IceCandidateMessage{Type: "offer_ice_candidate", Candidate: *ice}
room.SendToOutClients(ic, sessionId)
}
}
}
}
}
func joinRoom(rm json.RawMessage, ws *websocket.Conn, sId string) (r *Room) {
data := NewRoomMessageData{}
if err := json.Unmarshal(rm, &data); err != nil {
log.Println(err)
}
r = availableRooms.Get(data.RoomId)
if r == nil {
websocket.JSON.Send(ws, serverChatMessage("No room available with this ID."))
return nil
}
c := &Client{Websocket: ws, Uuid: sId, State: "out"}
r.AddClient(c)
desc := &DescriptionMessage{Type: "caller_description", Description: data.Description}
r.SendToInClients(desc, sId)
// Non-blocking channel send.
select {
case statsChannel <- true:
default:
}
return r
}
func newRoom(rm json.RawMessage, ws *websocket.Conn, sId string) (r *Room) {
data := NewRoomMessageData{}
if err := json.Unmarshal(rm, &data); err != nil {
log.Println(err)
}
c := &Client{
Websocket: ws,
Uuid: sId,
State: "in",
Username: data.Username,
}
r = &Room{RoomId: data.RoomId}
r.AddClient(c)
availableRooms.Add(r)
websocket.JSON.Send(ws, serverChatMessage("You are connected and waiting a user get in."))
// Non-blocking channel send
select {
case statsChannel <- true:
default:
}
return r
}
func status() []StatusRoom {
sr := make([]StatusRoom, len(availableRooms))
i := 0
for _, v := range availableRooms {
sr[i] = StatusRoom{RoomId: v.RoomId, ConnectedClients: len(v.Clients)}
i++
}
return sr
}
func websocketStatsHandler(ws *websocket.Conn) {
defer func() {
log.Println("Stats closed")
ws.Close()
}()
websocket.JSON.Send(ws, status())
for {
select {
case <-statsChannel:
log.Println("Sending a status update")
websocket.JSON.Send(ws, status())
}
}
}
func main() {
http.Handle("/", http.FileServer(http.Dir("public")))
http.Handle("/ws", websocket.Handler(websocketHandler))
http.Handle("/ws-stats", websocket.Handler(websocketStatsHandler))
log.Println("Server listening at :4000")
http.ListenAndServe(":4000", nil)
}