// 收到客户端消息 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) } }
// 收到客户端消息 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) }
// 新玩家进入 func OnNewUserCome(session *tcpsession.Tcpsession) { uuid := util.Rand() strUuid := uuid.Hex() session.SetKey(NewKey(strUuid)) UuidToSession[strUuid] = session //test key := session.GetKey().(*Key) fmt.Println(session.Conn.RemoteAddr(), "新玩家进入, uuid:", key.uuid) }
// tcp关闭 func OnClientClose(session *tcpsession.Tcpsession) { fmt.Println("玩家关闭", session.Conn.RemoteAddr()) session.Close() }