Skip to content

Commit b2d721d

Browse files
committed
update data
1 parent 337ed3a commit b2d721d

File tree

2 files changed

+171
-0
lines changed
  • 第一季 从零开始写游戏服务器 第二期/最新课节--课程代码

2 files changed

+171
-0
lines changed

第一季 从零开始写游戏服务器 第二期/最新课节--课程代码/class3/src/snake/NetRec.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package main
22

33
import (
4+
"Proto"
5+
"Proto/Proto2"
6+
"encoding/json"
47
"fmt"
58
"strings"
69

10+
"LollipopGo/LollipopGo/log"
11+
712
"code.google.com/p/go.net/websocket"
813
)
914

@@ -27,5 +32,91 @@ func GameServerReceive(ws *websocket.Conn) {
2732
}
2833
// 解析数据 --
2934
fmt.Println("返回数据:", string(contentstr))
35+
go SyncMeassgeFun(string(contentstr))
36+
}
37+
}
38+
39+
// 结构体数据类型
40+
type Requestbody struct {
41+
req string
42+
}
43+
44+
// json转化为map:数据的处理
45+
func (r *Requestbody) Json2map() (s map[string]interface{}, err error) {
46+
var result map[string]interface{}
47+
if err := json.Unmarshal([]byte(r.req), &result); err != nil {
48+
log.Debug("Json2map:", err.Error())
49+
return nil, err
50+
}
51+
return result, nil
52+
}
53+
54+
func SyncMeassgeFun(content string) {
55+
var r Requestbody
56+
r.req = content
57+
58+
if ProtocolData, err := r.Json2map(); err == nil {
59+
// 处理我们的函数
60+
HandleCltProtocol(ProtocolData["Protocol"], ProtocolData["Protocol2"], ProtocolData)
61+
} else {
62+
log.Debug("解析失败:", err.Error())
63+
}
64+
}
65+
66+
// //json str 转map
67+
// var dat map[string]interface{}
68+
// if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
69+
// fmt.Println("==============json str 转map=======================")
70+
// fmt.Println(dat)
71+
// fmt.Println(dat["host"])
72+
// }
73+
74+
// 字符串 解析成 json
75+
76+
func HandleCltProtocol(protocol interface{}, protocol2 interface{}, ProtocolData map[string]interface{}) {
77+
defer func() { // 必须要先声明defer,否则不能捕获到panic异常
78+
if err := recover(); err != nil {
79+
strerr := fmt.Sprintf("%s", err)
80+
//发消息给客户端
81+
ErrorST := Proto2.G_Error_All{
82+
Protocol: Proto.G_Error_Proto, // 主协议
83+
Protocol2: Proto2.G_Error_All_Proto, // 子协议
84+
ErrCode: "80006",
85+
ErrMsg: "亲,您发的数据的格式不对!" + strerr,
86+
}
87+
// 发送给玩家数据
88+
fmt.Println("贪吃蛇的主协议!!!", ErrorST)
89+
}
90+
}()
91+
92+
// 协议处理
93+
switch protocol {
94+
case float64(Proto.G_Snake_Proto):
95+
{ // 贪吃蛇的主协议
96+
fmt.Println("贪吃蛇的主协议!!!")
97+
HandleCltProtocol2Snake(protocol2, ProtocolData)
98+
99+
}
100+
default:
101+
panic("主协议:不存在!!!")
30102
}
103+
return
104+
}
105+
106+
// 子协议的处理
107+
func HandleCltProtocol2Snake(protocol2 interface{}, ProtocolData map[string]interface{}) {
108+
109+
switch protocol2 {
110+
case float64(Proto2.S2S_PlayerLoginSProto2):
111+
{
112+
fmt.Println("贪吃蛇:玩家进入游戏的协议!!!")
113+
// 玩家进入游戏的协议
114+
// this.EntryGameSnake(ProtocolData)
115+
}
116+
117+
default:
118+
panic("子协议:不存在!!!")
119+
}
120+
121+
return
31122
}

第一季 从零开始写游戏服务器 第二期/最新课节--课程代码/vender/src/LollipopGo/LollipopGo/util/util.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,83 @@ func MD5_LollipopGO(data string) string {
7070
}
7171

7272
//------------------------------------------------------------------------------
73+
74+
// package main
75+
76+
// import (
77+
// "encoding/json"
78+
// "fmt"
79+
// "os"
80+
// )
81+
82+
// type ConfigStruct struct {
83+
// Host string `json:"host"`
84+
// Port int `json:"port"`
85+
// AnalyticsFile string `json:"analytics_file"`
86+
// StaticFileVersion int `json:"static_file_version"`
87+
// StaticDir string `json:"static_dir"`
88+
// TemplatesDir string `json:"templates_dir"`
89+
// SerTcpSocketHost string `json:"serTcpSocketHost"`
90+
// SerTcpSocketPort int `json:"serTcpSocketPort"`
91+
// Fruits []string `json:"fruits"`
92+
// }
93+
94+
// type Other struct {
95+
// SerTcpSocketHost string `json:"serTcpSocketHost"`
96+
// SerTcpSocketPort int `json:"serTcpSocketPort"`
97+
// Fruits []string `json:"fruits"`
98+
// }
99+
100+
// func main() {
101+
// jsonStr := `{"host": "http://localhost:9090","port": 9090,"analytics_file": "","static_file_version": 1,"static_dir": "E:/Project/goTest/src/","templates_dir": "E:/Project/goTest/src/templates/","serTcpSocketHost": ":12340","serTcpSocketPort": 12340,"fruits": ["apple", "peach"]}`
102+
103+
// //json str 转map
104+
// var dat map[string]interface{}
105+
// if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
106+
// fmt.Println("==============json str 转map=======================")
107+
// fmt.Println(dat)
108+
// fmt.Println(dat["host"])
109+
// }
110+
111+
// //json str 转struct
112+
// var config ConfigStruct
113+
// if err := json.Unmarshal([]byte(jsonStr), &config); err == nil {
114+
// fmt.Println("================json str 转struct==")
115+
// fmt.Println(config)
116+
// fmt.Println(config.Host)
117+
// }
118+
119+
// //json str 转struct(部份字段)
120+
// var part Other
121+
// if err := json.Unmarshal([]byte(jsonStr), &part); err == nil {
122+
// fmt.Println("================json str 转struct==")
123+
// fmt.Println(part)
124+
// fmt.Println(part.SerTcpSocketPort)
125+
// }
126+
127+
// //struct 到json str
128+
// if b, err := json.Marshal(config); err == nil {
129+
// fmt.Println("================struct 到json str==")
130+
// fmt.Println(string(b))
131+
// }
132+
133+
// //map 到json str
134+
// fmt.Println("================map 到json str=====================")
135+
// enc := json.NewEncoder(os.Stdout)
136+
// enc.Encode(dat)
137+
138+
// //array 到 json str
139+
// arr := []string{"hello", "apple", "python", "golang", "base", "peach", "pear"}
140+
// lang, err := json.Marshal(arr)
141+
// if err == nil {
142+
// fmt.Println("================array 到 json str==")
143+
// fmt.Println(string(lang))
144+
// }
145+
146+
// //json 到 []string
147+
// var wo []string
148+
// if err := json.Unmarshal(lang, &wo); err == nil {
149+
// fmt.Println("================json 到 []string==")
150+
// fmt.Println(wo)
151+
// }
152+
// }

0 commit comments

Comments
 (0)