-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
53 lines (49 loc) · 1.92 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
package main
import "strconv"
type Hub struct {
// registered clients
clients map[Ids]*SameIdsLinkList
register chan *msg
broadcast chan *Client
unregister chan *Client
}
func newHub() *Hub {
return &Hub{
clients: make(map[Ids]*SameIdsLinkList),
register: make(chan *msg),
broadcast: make(chan *Client),
unregister: make(chan *Client),
}
}
func (h *Hub) run() {
for true {
select {
case msg := <-h.register:
if h.clients[*msg.cltmp.userIds] == nil {
// not exist [uid,tid] key
headList := NewSocketList(msg)
headList.Append(newNode(msg.cltmp, nil))
h.clients[*msg.cltmp.userIds] = headList
go headList.linklistRun() // used for log control
headList.PrintList()
} else {
headlist := h.clients[*msg.cltmp.userIds]
headlist.Append(newNode(msg.cltmp, nil))
go headlist.linklistRun()
headlist.PrintList()
}
case broadcastClient := <-h.broadcast: // only broadcast client msg(small)
if broadcastClient.hub.clients[*broadcastClient.userIds].Head.listCount == 1 {
if !(broadcastClient.hub.clients[*broadcastClient.userIds].Head.next.client.Admin) {
broadcastClient.hub.clients[*broadcastClient.userIds].Head.next.client.send <- []byte(strconv.Itoa(broadcastClient.hub.clients[*broadcastClient.userIds].Head.sm.Type))
}
} else if broadcastClient.hub.clients[*broadcastClient.userIds].Head.listCount == 2 {
if !(broadcastClient.hub.clients[*broadcastClient.userIds].Head.next.client.Admin) {
broadcastClient.hub.clients[*broadcastClient.userIds].Head.next.client.send <- []byte(strconv.Itoa(broadcastClient.hub.clients[*broadcastClient.userIds].Head.sm.Type))
} else if !(broadcastClient.hub.clients[*broadcastClient.userIds].Head.next.next.client.Admin) {
broadcastClient.hub.clients[*broadcastClient.userIds].Head.next.next.client.send <- []byte(strconv.Itoa(broadcastClient.hub.clients[*broadcastClient.userIds].Head.sm.Type))
}
}
}
}
}