Skip to content

Commit dcc3414

Browse files
authored
Merge pull request #1 from Golangltd/master
fork
2 parents 10cbc01 + 2a22860 commit dcc3414

File tree

10,044 files changed

+3477484
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

10,044 files changed

+3477484
-20
lines changed
Loading
Loading
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http://www.golang.ltd/forum.php?mod=viewthread&tid=7174&extra=
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"Proto"
5+
"Proto/Proto2"
6+
"time"
7+
8+
"code.google.com/p/go.net/websocket"
9+
)
10+
11+
// 定时发心跳
12+
func Timer(conn *websocket.Conn) {
13+
itimer := time.NewTicker(50)
14+
for {
15+
select {
16+
case <-itimer.C:
17+
18+
strOpenID := "A123456789A123456789A123456789A123456789A123456789A123456789A1234"
19+
// 执行的代码
20+
// data := &Proto2.Net_HeartBeat{
21+
// Protocol: Proto.GameNet_Proto,
22+
// Protocol2: Proto2.Net_HeartBeatProto2,
23+
// OpenID: strOpenID,
24+
// }
25+
//发送
26+
//PlayerSendToServer(conn, data)
27+
// -----------------------------------------------------------------
28+
// run的消息
29+
datapro := &Proto2.C2S_PlayerRun{
30+
Protocol: Proto.GameData_Proto,
31+
Protocol2: Proto2.C2S_PlayerRunProto2,
32+
OpenID: strOpenID,
33+
StrRunX: "22",
34+
StrRunY: "22",
35+
StrRunZ: "22",
36+
}
37+
PlayerSendToServer(conn, datapro)
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"strconv"
7+
8+
"Proto"
9+
"Proto/Proto2"
10+
"encoding/base64"
11+
"encoding/json"
12+
"flag"
13+
"os"
14+
"strings"
15+
"sync"
16+
"time"
17+
18+
"code.google.com/p/go.net/websocket"
19+
)
20+
21+
var GW sync.WaitGroup
22+
var Scount int
23+
24+
func main1() {
25+
26+
fmt.Println(os.Args[1:])
27+
fmt.Println(flag.Arg(1))
28+
fmt.Println("Entry server count:", os.Args[1]) // 人数
29+
30+
t1 := time.Now()
31+
count := os.Args[1]
32+
loops, _ := strconv.Atoi(os.Args[2]) // 并发次数
33+
int1, _ := strconv.Atoi(count)
34+
GW.Add(int1)
35+
Scount = int1 * loops
36+
for i := 0; i < int1; i++ {
37+
go GoroutineFunc(loops)
38+
}
39+
GW.Wait()
40+
elapsed := time.Since(t1)
41+
fmt.Println("Total count:", int1*loops)
42+
fmt.Println("Success count:", Scount)
43+
fmt.Println("Cysle TPS:", float64(int1*loops)/elapsed.Seconds())
44+
fmt.Println("Taken Time(s) :", elapsed)
45+
fmt.Println("Average Latency time(ms):", elapsed.Seconds()*1000/(float64(int1*loops)))
46+
//-------------------------------------------------------------------
47+
48+
}
49+
func GoroutineFunc(loops int) {
50+
fmt.Println("Robot 客户端模拟!")
51+
url := "ws://" + *addr + "/GolangLtd"
52+
ws, err := websocket.Dial(url, "", "test://golang/")
53+
if err != nil {
54+
fmt.Println("err:", err.Error())
55+
return
56+
}
57+
// 数据的发送
58+
59+
for i := 0; i < loops; i++ {
60+
Send(ws, "HeartBeat")
61+
// 发送心跳的协议
62+
// 数据处理
63+
var content string
64+
err := websocket.Message.Receive(ws, &content)
65+
if err != nil {
66+
fmt.Println(err.Error())
67+
return
68+
}
69+
// decode
70+
//fmt.Println(strings.Trim("", "\""))
71+
//fmt.Println(content)
72+
content = strings.Replace(content, "\"", "", -1)
73+
contentstr, errr := base64Decode([]byte(content))
74+
if errr != nil {
75+
fmt.Println(errr)
76+
}
77+
// 解析数据
78+
//fmt.Println(string(contentstr))
79+
_ = contentstr
80+
}
81+
GW.Done()
82+
83+
}
84+
85+
/*
86+
robot
87+
1 模拟玩家的正常的“操作”,例如 行走 跳跃 开枪等等
88+
2 做服务器的性能的测试,例如 并发量 内存 CPU 等等
89+
3 压力测试
90+
91+
注意点:
92+
1 模拟 ---> 多线程模拟 goroutine --- server !!!
93+
94+
首先:
95+
1 net 网络使用websocket 进行连接
96+
2 send 如何发送 ??
97+
*/
98+
var addr = flag.String("addr", "127.0.0.1:8888", "http service address")
99+
var connbak *websocket.Conn
100+
101+
// 1 robot客户端 我们是可以一起链接的 ---> websocket.Dial 每次都返回一个
102+
// 2 多个 websocket.Dial ---> 多个客户端的链接
103+
104+
func main() {
105+
fmt.Println("Robot 客户端模拟!")
106+
url := "ws://" + *addr + "/GolangLtd"
107+
ws, err := websocket.Dial(url, "", "test://golang/")
108+
if err != nil {
109+
fmt.Println("err:", err.Error())
110+
return
111+
}
112+
// 数据的发送
113+
for i := 0; i < 100; i++ {
114+
// go Send(ws, "Login")
115+
}
116+
117+
go Send(ws, "HeartBeat")
118+
// 发送心跳的协议
119+
connbak = new(websocket.Conn)
120+
connbak = ws
121+
go Timer(ws)
122+
123+
// 数据处理
124+
for {
125+
var content string
126+
err := websocket.Message.Receive(ws, &content)
127+
if err != nil {
128+
fmt.Println(err.Error())
129+
return
130+
}
131+
// decode
132+
fmt.Println(strings.Trim("", "\""))
133+
fmt.Println(content)
134+
content = strings.Replace(content, "\"", "", -1)
135+
contentstr, errr := base64Decode([]byte(content))
136+
if errr != nil {
137+
fmt.Println(errr)
138+
}
139+
// 解析数据
140+
fmt.Println(string(contentstr))
141+
}
142+
143+
}
144+
145+
// 解码
146+
func base64Decode(src []byte) ([]byte, error) {
147+
return base64.StdEncoding.DecodeString(string(src))
148+
}
149+
150+
// 消息的流程?
151+
// 1 针对我们的消息结构进行数据的组装
152+
// 2 针对我们组装的数据经行一个数据格式的转换 --> json
153+
// 3 json 数据直接发送到我们server
154+
155+
// 发送函数
156+
func Send(conn *websocket.Conn, Itype string) {
157+
158+
if Itype == "Login" {
159+
160+
// 1 组装
161+
data := &Proto2.C2S_PlayerLogin{
162+
Protocol: Proto.GameData_Proto,
163+
Protocol2: Proto2.C2S_PlayerLoginProto2,
164+
Itype: 1,
165+
StrLoginName: "Golangltd" + Rand_LoginName(),
166+
StrLoginPW: "1234556",
167+
StrLoginEmail: "[email protected]",
168+
}
169+
// 3 发送数据到服务器
170+
PlayerSendToServer(conn, data)
171+
} else if Itype == "HeartBeat" { // 心跳
172+
// 1 组装
173+
data := &Proto2.Net_HeartBeat{
174+
Protocol: Proto.GameNet_Proto,
175+
Protocol2: Proto2.Net_HeartBeatProto2,
176+
OpenID: "22323",
177+
}
178+
// 3 发送数据到服务器
179+
PlayerSendToServer(conn, data)
180+
}
181+
182+
return
183+
}
184+
185+
// 公用的send函数
186+
func PlayerSendToServer(conn *websocket.Conn, data interface{}) {
187+
188+
// 2 结构体转换成json数据
189+
jsons, err := json.Marshal(data)
190+
if err != nil {
191+
fmt.Println("err:", err.Error())
192+
return
193+
}
194+
///fmt.Println("jsons:", string(jsons))
195+
errq := websocket.Message.Send(conn, jsons)
196+
if errq != nil {
197+
fmt.Println(errq)
198+
}
199+
return
200+
}
201+
202+
// 随机函数
203+
func Rand_LoginName() string {
204+
idata := rand.Intn(100000)
205+
// 去重?
206+
strdata := strconv.Itoa(idata)
207+
return strdata
208+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
/*
9+
登录服务器的函数
10+
*/
11+
12+
func IndexHandler(w http.ResponseWriter, r *http.Request) {
13+
fmt.Fprintln(w, "hello world")
14+
// 需要处理 get请求等
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
/*
9+
登录服务器的函数
10+
*/
11+
12+
func IndexHandlerGM(w http.ResponseWriter, r *http.Request) {
13+
fmt.Fprintln(w, "hello world")
14+
// 需要处理 get请求等
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
����λ�ã�
2+
https://blog.csdn.net/wangshubo1989/article/details/75050024/
3+
4+
//-----------------------------------------------------------------
5+
redis ��������
6+
redis-server.exe redis.windows.conf
7+
//-----------------------------------------------------------------
8+
�������
9+
redis-cli.exe -h 127.0.0.1 -p 6379
10+
//-----------------------------------------------------------------

0 commit comments

Comments
 (0)