/
msgProc.go
79 lines (64 loc) · 1.67 KB
/
msgProc.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
package akWebNet
/*
初级消息处理
*/
import (
"github.com/gorilla/websocket"
"fmt"
"encoding/binary"
)
type receiveMsgProc func(sess *WebSession, msg *wsMessage)
var (
_procMsgs = map[int]receiveMsgProc{
websocket.TextMessage: TextMessageFunc,
websocket.BinaryMessage: BinaryMessageFunc,
websocket.CloseMessage: CloseMessageFunc,
websocket.PingMessage: PingMessageFunc,
websocket.PongMessage: PongMessageFunc,
}
)
func GetMessageHandler(id int)receiveMsgProc{
return _procMsgs[id]
}
func TextMessageFunc(sess *WebSession, msg *wsMessage){
fmt.Println("read TextMessage data: ", string(msg.data))
sess.Write(websocket.TextMessage, []byte("hello,too!"))
}
func BinaryMessageFunc(sess *WebSession, msg *wsMessage){
var pos int
msgid := binary.LittleEndian.Uint32(msg.data[pos:]) //消息id
pos+=4
datalen := binary.LittleEndian.Uint32(msg.data[pos:]) //消息长度
pos+=4
var (
params = []uint32{}
)
for i := uint32(0); i < datalen; i++ {
param := binary.LittleEndian.Uint32(msg.data[pos:])
pos+=4
params = append(params, param)
}
if len(params) <= 0 {
fmt.Println("invalid params: ", params)
return
}
fmt.Println("receive msgid: ", msgid)
if proc := GetGameLogicProcMsg(int(msgid)); proc != nil {
err, _ := proc(sess, params)
if err != nil {
fmt.Println("proc msg err: ", err)
}
}else{
fmt.Println("invalid msg id: ", msgid)
}
}
func CloseMessageFunc(sess *WebSession, msg *wsMessage){
fmt.Println("read CloseMessage.")
sess.offch <-sess
}
func PingMessageFunc(sess *WebSession, msg *wsMessage){
fmt.Println("read PingMessage.")
}
func PongMessageFunc(sess *WebSession, msg *wsMessage){
fmt.Println("read PongMessage.")
}