/
session.go
117 lines (104 loc) · 2.18 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
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
package main
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/gorilla/websocket"
)
var (
conns map[int]*Session
mutex *sync.Mutex
id int = 0
)
func init() {
conns = make(map[int]*Session)
mutex = new(sync.Mutex)
}
func NewSession(ws *websocket.Conn) *Session {
id++
session := &Session{
Conn: ws,
SendQueue: make(chan ResponseData, 10),
Quit: false,
Index: id,
}
mutex.Lock()
defer mutex.Unlock()
conns[id] = session
return session
}
func DelSession(session *Session) {
mutex.Lock()
mutex.Unlock()
delete(conns, session.Index)
}
type ResponseData struct {
Opt int `json:"opt"`
Success bool `json:"success"`
Msg interface{} `json:"msg"`
}
type Session struct {
Conn *websocket.Conn
SendQueue chan ResponseData
Quit bool
Index int
}
func (session *Session) Close() {
session.Quit = true
session.Conn.Close()
}
func (session *Session) handler() {
var err error
var messageType int
for !session.Quit {
var reply []byte
if messageType, reply, err = session.Conn.ReadMessage(); err != nil {
fmt.Println("Can't receive")
session.Close()
break
}
fmt.Println("Receive Message", string(reply), messageType)
var msg ResponseData
if err = json.Unmarshal(reply, &msg); err != nil {
fmt.Println(err.Error())
session.Close()
break
}
session.SendQueue <- msg
}
}
func (session *Session) Send() {
var err error
for !session.Quit {
select {
case msg := <-session.SendQueue:
fmt.Println("Sending to client: ", msg)
// var result []byte
// if result, err = json.Marshal(&msg); err != nil {
// fmt.Println("json.Marshal error", err.Error())
// session.Close()
// break
// }
if err = session.Conn.WriteJSON(msg); err != nil {
fmt.Println("Can't send")
session.Close()
break
}
}
}
}
func NotifyClient() {
for {
select {
case <-time.After(10 * time.Second):
for k, v := range conns {
v.SendQueue <- ResponseData{
Opt: 1,
Success: true,
Msg: fmt.Sprintf("你是第%d位接收者", k),
}
}
}
}
}