Exemplo n.º 1
0
func main() {

	session = tcplib.TcpClientCreate("127.0.0.1:8000", OnDataReceive, OnClientClose)
	wpk := packet.NewWpacket(packet.NewByteBuffer(1024), false)
	wpk.PutString("client")
	wpk.PutString("502301")
	session.Send(wpk, OnSendFinish)
	echo := packet.NewWpacket(packet.NewByteBuffer(1024), false)
	echo.PutString("this is echo ~")
	session.Send(echo, OnSendFinish)
	fmt.Println("...")

	for {
		time.Sleep(1 * 1e9)
	}
}
Exemplo n.º 2
0
// 收到客户端消息
func OnDataReceive(session *tcpsession.Tcpsession, rpk *packet.Rpacket) {
	key := session.GetKey().(*Key)
	// 注册
	if key.nFlag == FLAG_NON {
		msg, _ := rpk.String()
		gameid, _ := rpk.String()
		key.gameid = gameid
		// server
		if msg == "server" {
			key.nFlag = FLAG_SVR
			GameidToSession[gameid] = session
			fmt.Println("server RegisterOk   ", gameid)
		} else {
			// client
			key.nFlag = FLAG_CLT
			fmt.Println("client RegisterOk  To ", gameid)
		}
		wpk := packet.NewWpacket(packet.NewByteBuffer(64), false)
		wpk.PutString("registerOk")
		session.Send(wpk, OnSendFinish)
		return
	}

	if key.nFlag == FLAG_CLT {
		SendToServer(key, session, rpk)
		return
	}

	if key.nFlag == FLAG_SVR {
		SendToClient(key, session, rpk)
	}

}
Exemplo n.º 3
0
// 收到客户端消息
func OnDataReceive(session *tcpsession.Tcpsession, rpk *packet.Rpacket) {
	//session.Send(packet.NewWpacket(rpk.Buffer(), rpk.IsRaw()), OnSendFinish)
	uuid, _ := rpk.String()
	msg, _ := rpk.String()
	fmt.Println("收到uuid:", uuid, "转发过来的消息:", msg)
	echo := packet.NewWpacket(packet.NewByteBuffer(1024), false)
	echo.PutString(uuid)
	echo.PutString(msg)
	session.Send(echo, OnSendFinish)
}
Exemplo n.º 4
0
func main() {
	for i := 1; i < 2000; i++ {
		session = tcplib.TcpClientCreate("127.0.0.1:8000", OnDataReceive, OnClientClose)
		wpk := packet.NewWpacket(packet.NewByteBuffer(1024), false)
		wpk.PutString("selloword")
		session.Send(wpk, OnSendFinish)
	}
	fmt.Println("...")
	for {
		time.Sleep(1 * 1e9)
	}
}
Exemplo n.º 5
0
func SendToServer(key *Key, session *tcpsession.Tcpsession, rpk *packet.Rpacket) {
	//
	fmt.Println(session.Conn.RemoteAddr(), "发过来消息,Gate转发,")
	server, ok := GameidToSession[key.gameid]
	if !ok {
		fmt.Println("服务器未注册", key.gameid)
		return
	}
	msg, _ := rpk.String()
	wpk := packet.NewWpacket(packet.NewByteBuffer(64), false)
	wpk.PutString(key.uuid)
	wpk.PutString(msg)
	fmt.Println("转发消息", msg)
	server.Send(wpk, OnSendFinish)
}
Exemplo n.º 6
0
func SendToClient(key *Key, session *tcpsession.Tcpsession, rpk *packet.Rpacket) {
	fmt.Println("收到了服务器要分发的消息")
	wpk := packet.NewWpacket(packet.NewByteBuffer(64), false)
	uuid, err := rpk.String()
	if err != nil {
		return
	}
	msg, _ := rpk.String()
	wpk.PutString(msg)
	client, ok := UuidToSession[uuid]
	if !ok {
		fmt.Println("玩家已下线", key.uuid)
		return
	}
	client.Send(wpk, OnSendFinish)
}