/
session.go
72 lines (58 loc) · 1.37 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package plugins
import (
"bytes"
"fmt"
"math/rand"
"github.com/8treenet/jaguar"
)
func NewSession() *Session {
return new(Session)
}
type Session struct {
userId uint32
userName string
Conn jaguar.TcpConn `inject:"tcp_conn"`
}
func (s *Session) Auth(token string) bool {
//
// mock token验证
//
s.userName = "user_" + token
s.userId = uint32(10000 + rand.Intn(80000))
if GetSession(s.userId) != nil {
// 已登录
return false
}
SetSession(s.userId, s)
return true
}
func (s *Session) User() (uname string, uid uint32) {
uname = s.userName
uid = s.userId
return
}
func (s *Session) CloseEvent() {
fmt.Println("CloseEvent", s.userId)
RemoveSession(s.userId)
return
}
func (s *Session) Recover(err error, stack string) {
fmt.Println("Recover", err, stack)
}
func (s *Session) Reader(protocolId uint16, buffer *bytes.Buffer) *bytes.Buffer {
fmt.Println("Reader", protocolId)
return buffer
}
func (s *Session) Writer(protocolId uint16, buffer *bytes.Buffer) *bytes.Buffer {
fmt.Println("Writer", protocolId)
return buffer
}
func (s *Session) Request(protocolId uint16, handle interface{}) {
fmt.Println("Request", protocolId, handle)
}
func (s *Session) Respone(protocolId uint16, handle interface{}) {
fmt.Println("Respone", protocolId, handle)
}
func (s *Session) Push(protocolId uint16, handle interface{}) {
fmt.Println("Push", protocolId, handle)
}