/
sessionmgr.go
126 lines (108 loc) · 2.8 KB
/
sessionmgr.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
120
121
122
123
124
125
126
package mediaserver
import (
"github.com/go-redis/redis"
"github.com/gorilla/websocket"
"github.com/speps/go-hashids"
"log"
"time"
)
const(
SESSION_STARTED = "STARTED"
SESSION_INPROGRESS = "INPROGRESS"
SESSION_ENDED = "ENDED"
)
var redidClient *redis.Client
/*func init() {
redidClient = redis.NewClient(&redis.Options{
Addr: PROPERTIES.MustGet("openavstream.redis.connection"),
Password: "", // no password set
DB: 0, // use default DB
})
}
*/
var SessionStore = map[string]*Session {
}
// Session is started when first user connects to it the server
// a unique session Id is given to it.
type Session struct {
SessionId string
AuthToken string
ConnGroup ConnectionGroup
State string
}
// Holds the socket connection and a unique id for it.
type Connection struct {
Id string
ClientAddr string
Conn *websocket.Conn
}
// Holds a group of connection. Group is defined by one user connection
// and multiple Admin connection.
type ConnectionGroup struct {
UserConnection Connection
AdminConnection []Connection
}
const UserConnType = 0
const AdminConnType = 1
func makeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Second)
}
func IsSessionExist(sessionId string) bool {
if _, ok:= SessionStore[sessionId]; ok {
return true
}
return false
}
// Creates a new session
func CreateNewSession(conn *websocket.Conn) *Session {
id := newHashId()
userConnect := Connection{id, conn.RemoteAddr().String(), conn}
conngroup := ConnectionGroup{userConnect, nil}
session := Session { id, "",conngroup , "STARTED"}
SessionStore[id] = &session
return &session
}
// write the binary data to the socket
func (s *Session) WriteBinary(data []byte, connType int) {
log.Println("Start Writing to the connection")
if connType == 0 {
s.ConnGroup.UserConnection.Conn.WriteMessage(2, data)
log.Println("finished writing to the connection")
} else {
for _, adminConn := range s.ConnGroup.AdminConnection {
adminConn.Conn.WriteMessage(2, data)
}
}
}
// Write the text data to the socket
func (s *Session) WriteText(data []byte, connType int) {
log.Println("Writing text data: "+ string(data))
if connType == 0 {
s.ConnGroup.UserConnection.Conn.WriteMessage(1, data)
log.Println("finished writing to the connection")
} else {
for _, adminConn := range s.ConnGroup.AdminConnection {
adminConn.Conn.WriteMessage(1, data)
}
}
}
func newHashId() string {
var hd = hashids.NewData()
hd.Salt = "OpenAVStream Salt"
h, err := hashids.NewWithData(hd)
handleError(err)
now := time.Now()
year := now.Year()
month := int(now.Month())
day := now.Day()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
id, _ := h.Encode([]int{year, month, day, hour, minute, second})
return id
}
func handleError(err error) {
if err != nil {
log.Println("handling error::::", err)
}
}