-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
57 lines (50 loc) · 1.19 KB
/
session.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
package main
import (
"github.com/rs/xid"
"gopkg.in/olahol/melody.v1"
"sync"
)
var (
sessionMap = make(map[string]*Session)
sessionLock sync.RWMutex
adminSessionMap = make(map[string]*Session)
adminSessionLock sync.RWMutex
)
type Session struct {
Namespace string `json:"namespace"`
Id string `json:"id"`
Ip string `json:"ip"`
IsController bool `json:"isController"`
Ws *melody.Session `json:"-"`
}
func NewSession(namespace string, ip string, m *melody.Session, isController bool) (string, *Session) {
var id = xid.New().String()
var s = &Session{
Namespace: namespace,
Id: id,
Ip: ip,
Ws: m,
IsController: isController,
}
if isController {
adminSessionLock.Lock()
defer adminSessionLock.Unlock()
adminSessionMap[id] = s
} else {
sessionLock.Lock()
defer sessionLock.Unlock()
sessionMap[id] = s
}
return id, s
}
func UnmapSession(id string, isController bool) {
if isController {
adminSessionLock.Lock()
defer adminSessionLock.Unlock()
delete(adminSessionMap, id)
} else {
sessionLock.Lock()
defer sessionLock.Unlock()
delete(sessionMap, id)
}
}