-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub.go
More file actions
107 lines (85 loc) · 2.18 KB
/
pubsub.go
File metadata and controls
107 lines (85 loc) · 2.18 KB
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
package im
import (
"fmt"
"sync"
"github.com/gorilla/websocket"
)
// Cache stores ws.connections.
type Cache struct {
items sync.Map
}
// An item represents ws.connections.
type item struct {
data interface{}
}
// New creates a new cache
func NewCache() *Cache {
return &Cache{}
}
// Get gets the value for the given key.
// key is client id
func (cache *Cache) Get(key interface{}) (interface{}, bool) {
obj, exists := cache.items.Load(key)
if !exists {
return nil, false
}
item := obj.(item)
return item.data, true
}
// Set sets a value for the given key with an expiration duration.
// If the duration is 0 or less, it will be stored forever.
func (cache *Cache) Set(key interface{}, value interface{}) {
cache.items.Store(key, item{
data: value,
})
}
// Range calls f sequentially for each key and value present in the cache.
// If f returns false, range stops the iteration.
func (cache *Cache) Range(f func(key, value interface{}) bool) {
//now := time.Now().UnixNano()
fn := func(key, value interface{}) bool {
item := value.(item)
return f(key, item.data)
}
cache.items.Range(fn)
}
// Delete deletes the key and its value from the cache.
func (cache *Cache) Delete(key interface{}) {
cache.items.Delete(key)
}
// Close closes the cache and frees up resources.
func (cache *Cache) Close() {
cache.items = sync.Map{}
}
var c = NewCache()
// Subscribes client from a geven topic
func subscribe(topic string, client *websocket.Conn) {
clients, _ := c.Get(topic)
if clients == nil {
clients = make(map[*websocket.Conn]bool)
}
clients.(map[*websocket.Conn]bool)[client] = true
c.Set(topic, clients)
}
// unsubscribe delete client from topic
func unsubscribe(topic string, client *websocket.Conn) {
clients, _ := c.Get(topic)
if clients == nil {
return
}
delete(clients.(map[*websocket.Conn]bool), client)
c.Set(topic, clients)
}
// publish send message to all subsecribed clients
func publish(i int, topic string, data []byte) {
clients, found := c.Get(topic)
if found == false {
fmt.Println("no client to send data")
return
}
for c := range clients.(map[*websocket.Conn]bool) {
if err := c.WriteMessage(i, data); err != nil {
fmt.Println(err)
}
}
}