forked from deven96/gosock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.go
119 lines (107 loc) · 2.67 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import(
"fmt"
"crypto/rand"
"encoding/hex"
"net/http"
"github.com/gorilla/websocket"
"github.com/deven96/gosock/pkg/custlog"
)
func init(){
// append to the main log
defwriters := custlog.DefaultWriters(*LogFile, true)
//TRACE will be Discarded, while the rest will be routed accordingly
custlog.LogInit(defwriters)
}
// generate random color string
func randHex() (string, error) {
bytez := make([]byte, 3)
if _, err := rand.Read(bytez); err != nil {
return "", err
}
ret := "#" + hex.EncodeToString(bytez)
custlog.Info.Printf("Assigning %s color to new client", ret)
return ret, nil
}
type room struct {
//forward is then channel holding the incoming messages
// that should be forwarded to other clients
forward chan *message
// clients that want to join
join chan *client
// clients that want to leave
leave chan *client
// holds all current clients in the room
clients map[*client]bool
}
func (r *room) run(){
for {
select {
case client:= <- r.join:
// joining
r.clients[client] = true
custlog.Info.Printf("Client %s joined the room...", client.color)
msg := &message{
Code: "#ffffff",
Message: fmt.Sprintf("Client %s joined the room", client.color),
}
for client := range r.clients {
client.send <- msg
}
case client:= <- r.leave:
// leaving
// delete client
custlog.Warning.Printf("Removing Client %s from room clients and closing send channel...", client.color)
delete(r.clients, client)
// close send channel of client
close(client.send)
msg := &message{
Code: "#ffffff",
Message: fmt.Sprintf("Client %s left the room", client.color),
}
for client := range r.clients {
client.send <- msg
}
case msg := <- r.forward:
for client := range r.clients {
client.send <- msg
}
}
}
}
// message constants
const (
socketBufferSize = 1024
messageBufferSize = 256
)
// set read and write size
var upgrader = &websocket.Upgrader{ReadBufferSize: socketBufferSize, WriteBufferSize: socketBufferSize}
// ServeHTTP for room struct
func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request){
socket, err := upgrader.Upgrade(w, req, nil)
if err!= nil {
custlog.Trace.Println(err)
return
}
gencolor, _ := randHex()
// pass client address to room
client := &client {
socket : socket,
send: make(chan *message, messageBufferSize),
room: r,
color: gencolor,
}
r.join <- client
defer func() {r.leave <- client} ()
go client.write()
client.read()
}
// creates a new room
func newRoom() *room {
return &room{
forward: make(chan *message),
join: make(chan *client),
leave: make(chan *client),
clients: make(map[*client]bool),
}
}