// 收到客户端消息 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 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) }
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) }
// 收到客户端消息 func OnDataReceive(session *tcpsession.Tcpsession, rpk *packet.Rpacket) { //session.Send(packet.NewWpacket(rpk.Buffer(), rpk.IsRaw()), OnSendFinish) msg, _ := rpk.String() fmt.Println("收到的消息:", msg) }