protected override async Task OnMessage(MessageEventArgs e) { using (StreamReader sr = new StreamReader(e.Data)) { string json = sr.ReadToEnd(); NetInfoBase bas = JsonConvert.DeserializeObject <NetInfoBase>(json); string data = bas.JsonData; if (this.OnGameData != null) { this.OnGameData(this, bas); } switch ((RoomOrderType)bas.OrderType) { case RoomOrderType.连接到房间: { var obj = JsonConvert.DeserializeAnonymousType(data, new { roomid = "", pid = "" }); string pid = obj.pid; string roomid = obj.roomid; //找到对应房间 GameRoom room = Management.GetRoom(roomid); if (room == null) { return; } //判断是否添加玩家到房间 Player p = Management.GetOnlinePlayer(pid); if (!room.HasPlayer(pid)) { room.AddPlayer(p); p.RoomWhich = room; } //绑定玩家到会话 this.BindPlayer = p; RoomBinding(p.RoomWhich); p.SetRoomWebSok(this); //创建房间实体类 NetRoom nr = new NetRoom(room); //广播房间信息 room.BroadCast(JsonConvert.SerializeObject(nr), RoomOrderType.回送房间信息); Log.Print("玩家" + this.BindPlayer.Name + "成功连接到房间"); break; } case RoomOrderType.玩家准备: { this.BindRoom.SetReady(this.BindPlayer); this.BindPlayer.LastSendRoomData("", RoomOrderType.玩家准备, this.BindPlayer.PlayerID); this.BindPlayer.NextSendRoomData("", RoomOrderType.玩家准备, this.BindPlayer.PlayerID); break; } case RoomOrderType.踢出玩家: { var pid = bas.Tag; this.BindRoom.BroadCast("", RoomOrderType.踢出玩家, pid); //将玩家从房间删除 this.BindRoom.RemovePlayer(pid); break; } } } }
//CallBack->房主切换时触发 private void Room_MasterChange(GameRoom sender, MasterChangeEventArgs e) { sender.BroadCast("", RoomOrderType.房主切换, e.NewMaster.PlayerID); }
//绑定房间到会话 private void RoomBinding(GameRoom rom) { this.BindRoom = rom; rom.MasterChange += Room_MasterChange; rom.PlayerDisconnect += Room_PlayerDisconnect; }
//CallBack->玩家退出房间触发 private void Room_PlayerDisconnect(GameRoom sender, Player ply) { sender.BroadCast("", RoomOrderType.玩家退出, ply.PlayerID); }