-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.go
99 lines (90 loc) · 2.2 KB
/
room.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
package main
import (
"log"
"net/http"
mgo "gopkg.in/mgo.v2"
"github.com/dkundathagard/chat/trace"
"github.com/gorilla/websocket"
"github.com/stretchr/objx"
)
const (
socketBufferSize = 1024
messageBufferSize = 256
)
type room struct {
name string
// forward is a channel that holds incoming messages
// that should be forwarded to other clients
forward chan *message
// join is a channel for clients wishing to join a room
join chan *client
// leave is a channel for clients wishing to leave the room
leave chan *client
// clients holds all current clients in this room.
clients map[*client]bool
// tracer will receive trace information of activity in the room.
coll *mgo.Collection
//
tracer trace.Tracer
}
// newRoom makes a new room.
func newRoom(name string, c *mgo.Collection) *room {
return &room{
name: name,
forward: make(chan *message),
join: make(chan *client),
leave: make(chan *client),
clients: make(map[*client]bool),
coll: c,
tracer: trace.Off(),
}
}
func (r *room) run() {
for {
select {
case client := <-r.join:
r.clients[client] = true
r.tracer.Trace("New client joined")
case client := <-r.leave:
delete(r.clients, client)
close(client.send)
r.tracer.Trace("Client left")
case msg := <-r.forward:
r.tracer.Trace("Message received: ", msg.Message)
if err := r.coll.Insert(msg); err != nil {
r.tracer.Trace("Error persisting message to db: ", msg.Message)
continue
}
for client := range r.clients {
client.send <- msg
r.tracer.Trace(" -- sent to client")
}
}
}
}
var upgrader = &websocket.Upgrader{
ReadBufferSize: socketBufferSize,
WriteBufferSize: socketBufferSize,
}
func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request) {
socket, err := upgrader.Upgrade(w, req, nil)
if err != nil {
log.Fatal("ServeHTTP:", err)
return
}
authCookie, err := req.Cookie("auth")
if err != nil {
log.Fatal("Failed to get auth cookie:", err)
return
}
client := &client{
socket: socket,
send: make(chan *message, messageBufferSize),
room: r,
userData: objx.MustFromBase64(authCookie.Value),
}
r.join <- client
defer func() { r.leave <- client }()
go client.write()
client.read()
}