Skip to content

Commit 337ed3a

Browse files
committed
update data
1 parent 121f713 commit 337ed3a

File tree

5 files changed

+63
-60
lines changed

5 files changed

+63
-60
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (this *NetDataConn) LoginGameSnake(ProtocolData map[string]interface{}) {
5353
// 2 获取玩家的信息 --> player data
5454
// 3 保存到内存中的数据
5555
data := &Proto2.S2S_PlayerLoginS{
56-
Protocol: Proto.GameNet_Proto,
56+
Protocol: Proto.G_Snake_Proto,
5757
Protocol2: Proto2.S2S_PlayerLoginSProto2,
5858
Token: "123456789",
5959
}

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,6 @@ import (
1313
"code.google.com/p/go.net/websocket"
1414
)
1515

16-
// 匹配服务器
17-
// 发消息给服务器
18-
// 否则就一直等待匹配
19-
func initMatch(conn *websocket.Conn) {
20-
21-
// 1 组装 进入的协议
22-
data := &Proto2.C2S_PlayerEntryGame{
23-
Protocol: Proto.G_Snake_Proto, // 游戏主要协议
24-
Protocol2: Proto2.C2S_PlayerEntryGameProto2,
25-
Code: util.UTCTime_LollipopGO(), // 随机生产的数据,时间戳
26-
}
27-
// fmt.Println(data)
28-
// 2 发送数据到服务器
29-
PlayerSendToServer(conn, data)
30-
31-
}
32-
3316
// 公用的send函数
3417
func PlayerSendToServer(conn *websocket.Conn, data interface{}) bool {
3518

@@ -52,3 +35,32 @@ func PlayerSendToServer(conn *websocket.Conn, data interface{}) bool {
5235
func base64Decode(src []byte) ([]byte, error) {
5336
return base64.StdEncoding.DecodeString(string(src))
5437
}
38+
39+
// 匹配服务器
40+
func initMatch(conn *websocket.Conn) {
41+
42+
// 1 组装 进入的协议
43+
data := &Proto2.C2S_PlayerEntryGame{
44+
Protocol: Proto.G_Snake_Proto, // 游戏主要协议
45+
Protocol2: Proto2.C2S_PlayerEntryGameProto2,
46+
Code: util.UTCTime_LollipopGO(), // 随机生产的数据,时间戳
47+
}
48+
// fmt.Println(data)
49+
// 2 发送数据到服务器
50+
PlayerSendToServer(conn, data)
51+
52+
}
53+
54+
// 登录服务器
55+
// 登陆的用户名及密码都是随机暂时
56+
func initLogin(conn *websocket.Conn) {
57+
// 组装数据
58+
data := &Proto2.C2S_PlayerLoginS{
59+
Protocol: Proto.G_Snake_Proto, // 游戏主要协议
60+
Protocol2: Proto2.C2S_PlayerLoginSProto2,
61+
Login_Name: util.UTCTime_LollipopGO(),
62+
Login_PW: util.UTCTime_LollipopGO(),
63+
}
64+
// 发送数据
65+
PlayerSendToServer(conn, data)
66+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"code.google.com/p/go.net/websocket"
8+
)
9+
10+
// 处理数据的返回
11+
func GameServerReceive(ws *websocket.Conn) {
12+
for {
13+
var content string
14+
err := websocket.Message.Receive(ws, &content)
15+
if err != nil {
16+
fmt.Println(err.Error())
17+
continue
18+
}
19+
// decode
20+
fmt.Println(strings.Trim("", "\""))
21+
fmt.Println(content)
22+
content = strings.Replace(content, "\"", "", -1)
23+
contentstr, errr := base64Decode([]byte(content))
24+
if errr != nil {
25+
fmt.Println(errr)
26+
continue
27+
}
28+
// 解析数据 --
29+
fmt.Println("返回数据:", string(contentstr))
30+
}
31+
}

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@ import (
88
"code.google.com/p/go.net/websocket"
99
)
1010

11-
// 用户登录的协议
12-
func Send_PlayerLogin(conn *websocket.Conn, strName string, strPW string) {
13-
if len(strName) == 0 || len(strPW) == 0 {
14-
panic("用户名/密码为空!!!")
15-
}
16-
17-
// 组装数据
18-
data := &Proto2.C2S_PlayerLoginS{
19-
Protocol: Proto.G_Snake_Proto, // 游戏主要协议
20-
Protocol2: Proto2.C2S_PlayerLoginSProto2,
21-
Login_Name: strName,
22-
Login_PW: strPW,
23-
}
24-
// 发送数据
25-
PlayerSendToServer(conn, data)
26-
}
27-
2811
// 发送坐标数据 --==--
2912
// 数据 X Y Z
3013
func Send_XYZ_Data(conn *websocket.Conn, strOpenID string, strRoomID string, OP_ULRDP string) {
@@ -39,26 +22,3 @@ func Send_XYZ_Data(conn *websocket.Conn, strOpenID string, strRoomID string, OP_
3922
// 2 发送数据到服务器
4023
PlayerSendToServer(conn, data)
4124
}
42-
43-
// 处理数据的返回
44-
func GameServerReceive(ws *websocket.Conn) {
45-
for {
46-
var content string
47-
err := websocket.Message.Receive(ws, &content)
48-
if err != nil {
49-
fmt.Println(err.Error())
50-
continue
51-
}
52-
// decode
53-
fmt.Println(strings.Trim("", "\""))
54-
fmt.Println(content)
55-
content = strings.Replace(content, "\"", "", -1)
56-
contentstr, errr := base64Decode([]byte(content))
57-
if errr != nil {
58-
fmt.Println(errr)
59-
continue
60-
}
61-
// 解析数据 --
62-
fmt.Println("返回数据:", string(contentstr))
63-
}
64-
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
_ "glog-master"
4444
"math/rand"
4545
"os"
46-
"strings"
4746
"time"
4847

4948
"code.google.com/p/go.net/websocket"
@@ -85,7 +84,8 @@ func initNet() bool {
8584
// 协程支持 --接受线程操作
8685
go GameServerReceive(connbak)
8786
// 1 登录 协议
88-
// 2 进入游戏
87+
initLogin(connbak)
88+
// 2 进入 游戏
8989
initMatch(connbak)
9090
return true
9191
}

0 commit comments

Comments
 (0)