// 收到客户端消息 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 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) }