private void MSG_CLIENT_LOGIN_RESPONSE() { bool success; string msg = ""; bool first; float pos_x; float pos_y; float hp; int ctype; NetBitStream stream = new NetBitStream(); stream.BeginRead2(packet); stream.ReadBool(out success); stream.ReadString(out msg); //读取用户基本状态信息,并更新信息 stream.ReadBool(out first); stream.ReadFloat(out pos_x); stream.ReadFloat(out pos_y); stream.ReadFloat(out hp); stream.ReadInt(out ctype); //更新用户基本信息 player.name = strUsername; player.first = first; player.pos_x = pos_x; player.pos_y = pos_y; player.hp = hp; player.ctype = ctype; Debug.Log("$$$" + strUsername); Debug.Log(first); Debug.Log(pos_x); Debug.Log(pos_y); Debug.Log(hp); Debug.Log(ctype); if (success) { Debug.Log("登录成功!"); iLoginState = 1; } else { Debug.Log("登录失败!"); iLoginState = 2; } strMessage = msg; }
private void MSG_CLIENT_LINK_WORLD() { NetBitStream stream = new NetBitStream(); stream.BeginRead2(packet); // 从流中读取新登陆玩家状态信息, 并写入 string name; bool first; float pos_x; float pos_y; float hp; int ctype; stream.ReadString(out name); stream.ReadBool(out first); stream.ReadFloat(out pos_x); stream.ReadFloat(out pos_y); stream.ReadFloat(out hp); stream.ReadInt(out ctype); Player p = new Player(name, first, pos_x, pos_y, hp, ctype); playerList.Add(p); CGameManager.Instance.AddPlayer(p); }
private void MSG_CLIENT_LINK_WORLD_RESPONSE() { bool success; string msg = ""; NetBitStream stream = new NetBitStream(); stream.BeginRead2(packet); stream.ReadBool(out success); stream.ReadString(out msg); if (success) { Debug.Log("登入游戏世界成功**"); iGameState = 1; // 从流中读取世界状态信息, 并写入 int count; string name; bool first; float pos_x; float pos_y; float hp; int ctype; stream.ReadInt(out count); for (int i = 0; i < count; i++) { stream.ReadString(out name); stream.ReadBool(out first); stream.ReadFloat(out pos_x); stream.ReadFloat(out pos_y); stream.ReadFloat(out hp); stream.ReadInt(out ctype); Player p = new Player(name, first, pos_x, pos_y, hp, ctype); playerList.Add(p); CGameManager.Instance.InitPlayer(p); } } else { Debug.Log("登录游戏世界失**"); iGameState = 2; } strMessage = msg; }
private void MSG_CLIENT_REGISTER_RESPONSE() { bool success; string msg = ""; NetBitStream stream = new NetBitStream(); stream.BeginRead2(packet); stream.ReadBool(out success); stream.ReadString(out msg); if (success) { Debug.Log("注册成功!"); iRegisterState = 1; } else { Debug.Log("注册失败!"); iRegisterState = 2; } strMessage = msg; }
private void MSG_PLAYER_ACTION() { //读取流信息,更新玩家状态 NetBitStream stream = new NetBitStream(); stream.BeginRead2(packet); string name; bool first; float pos_x; float pos_y; float hp; int ctype; stream.ReadString(out name); stream.ReadBool(out first); stream.ReadFloat(out pos_x); stream.ReadFloat(out pos_y); stream.ReadFloat(out hp); stream.ReadInt(out ctype); Player player = null; foreach (Player p in playerList) { if (p.name.Equals(name)) { player = p; } } player.name = name; player.first = first; player.pos_x = pos_x; player.pos_y = pos_y; player.hp = hp; player.ctype = ctype; List <byte> lc = new List <byte>(); int count; stream.ReadInt(out count); for (int i = 0; i < count; i++) { byte b; stream.ReadByte(out b); lc.Add(b); } //传递控制信息 CGameManager.Instance.UpdateControl(name, lc); Debug.Log("recv control ****"); Debug.Log(name); Debug.Log(first); Debug.Log(pos_x); Debug.Log(pos_y); Debug.Log(hp); Debug.Log(ctype); string str = ""; foreach (byte b in lc) { str += b + "-"; } Debug.Log(str); }
static void Main(string[] args) { ushort id = 10; byte n = 0; bool b = false; int vint = 100; uint vint2 = 999; short vshort = 10; ushort vshort2 = 101; float number = 0.8f; string data = "我是中国人"; NetBitStream stream = new NetBitStream(); stream.BeginWrite(id); stream.WriteByte(n); stream.WriteBool(b); stream.WriteInt(vint); stream.WriteUInt(vint2); stream.WriteShort(vshort); stream.WriteUShort(vshort2); stream.WriteFloat(number); stream.WriteString(data); stream.EncodeHeader(); NetPacket packet = new NetPacket(); packet.CopyBytes(stream); NetBitStream stream2 = new NetBitStream(); stream2.BeginRead(packet, out id); stream2.ReadByte(out n); stream2.ReadBool(out b); stream2.ReadInt(out vint); stream2.ReadUInt(out vint2); stream2.ReadShort(out vshort); stream2.ReadUShort(out vshort2); stream2.ReadFloat(out number); stream2.ReadString(out data); System.Console.WriteLine(id); System.Console.WriteLine(n); System.Console.WriteLine(b); System.Console.WriteLine(vint); System.Console.WriteLine(vint2); System.Console.WriteLine(vshort); System.Console.WriteLine(vshort2); System.Console.WriteLine(number); System.Console.WriteLine(data); NetStructManager.TestStruct cif; cif.header = 10; cif.msgid = 5; cif.m = 0.9f; cif.n = 10; cif.str = "hello"; byte[] bs = NetStructManager.getBytes(cif); NetStructManager.EncoderHeader(ref bs); NetStructManager.TestStruct cif2; System.Type type = typeof(NetStructManager.TestStruct); cif2 = (NetStructManager.TestStruct)NetStructManager.fromBytes(bs, type); System.Console.WriteLine(":" + bs.Length); System.Console.WriteLine(":" + cif2.header); System.Console.WriteLine(cif2.msgid); System.Console.WriteLine(cif2.m); System.Console.WriteLine(cif2.n); System.Console.WriteLine(cif2.str); ChatClient client = new ChatClient(); client.Start(); client.Update(); }