/
hub.go
72 lines (59 loc) · 1.58 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chat
import (
"github.com/EngineerKamesh/igb/igweb/bot"
)
type ClientMessage struct {
client *Client
message []byte
}
// hub maintains the set of active clients and broadcasts messages to the
// clients.
type Hub struct {
// A bot that implements the bot.Bot interface
chatbot bot.Bot
// Registered clients.
clients map[*Client]bool
// Inbound messages also containing the client who sent the message
broadcastmsg chan *ClientMessage
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
}
func NewHub(chatbot bot.Bot) *Hub {
return &Hub{
chatbot: chatbot,
broadcastmsg: make(chan *ClientMessage),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[*Client]bool),
}
}
func (h *Hub) SendMessage(client *Client, message []byte) {
client.send <- message
}
func (h *Hub) ChatBot() bot.Bot {
return h.chatbot
}
func (h *Hub) Run() {
for {
select {
case client := <-h.register:
h.clients[client] = true
greeting := h.chatbot.Greeting()
h.SendMessage(client, []byte(greeting))
case client := <-h.unregister:
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
close(client.send)
}
case clientmsg := <-h.broadcastmsg:
client := clientmsg.client
reply := h.chatbot.Reply(string(clientmsg.message))
h.SendMessage(client, []byte(reply))
}
}
}