/// <summary> ///由用户断开连接的时候 /// </summary> /// <param name="session"></param> /// <param name="value"></param> void ws_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value) { showLog("客户端" + session.RemoteEndPoint.ToString() + ":断开成功"); try { var secretKey = dic[session]; DbConnectionMySQL.ExecuteNonQuery(DbConnectionMySQL.Conn, CommandType.Text, "update game set Online='否' where secretKey='" + secretKey + "'", null); SendToAllPosition(secretKey, 0.0f, 0.0f, 0, "remove"); } catch (System.Collections.Generic.KeyNotFoundException) { dic.Remove(session); showData(); } }
/// <summary> /// 处理从客户端收到的消息 /// </summary> /// <param name="type">收到的消息类型</param> /// <param name="json">收到的json文件</param> /// <param name="client">对应的客户端连接</param> private void manipulationData(string type, JObject json, WebSocketSession client) { if (type == "register") { var account = json["account"].ToString(); var password = json["password"].ToString(); //首先判断账号是否已经存在 if (DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where account='" + account + "'", null).Tables[0].Rows.Count == 0) { DbConnectionMySQL.ExecuteNonQuery(DbConnectionMySQL.Conn, CommandType.Text, "insert into game(account,password) values('" + account + "','" + password + "')", null); //不存的情况 JObject js = new JObject(); js.Add("type", "registerResult"); js.Add("reason", "true"); client.Send(js.ToString()); } else { //账号已经存在的情况 JObject js = new JObject(); js.Add("type", "registerResult"); js.Add("reason", "false"); client.Send(js.ToString()); } } else if (type == "login") { var account = json["account"].ToString(); var password = json["password"].ToString(); var pas = selectMysqlPassword(account); if (pas == "") { //账号不存在 showLog("客户端" + client.RemoteEndPoint + ":账号不存在"); JObject js = new JObject(); js.Add("type", "loginResult"); js.Add("ifloginSuccess", "false"); js.Add("reason", "NoAccount"); client.Send(js.ToString()); } else if (pas == password) { //登陆成功 showLog("客户端" + client.RemoteEndPoint + ":登陆成功"); //将密码和时间组合起来,通过秘钥进行加密 pas = LoginEncryption.encryptionPassword(pas + DateTime.Now.ToLongTimeString().ToString(), "thisisapassword"); var playername = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where account=" + account, null).Tables[0].Rows[0]["playerName"].ToString(); var playerType = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where account=" + account, null).Tables[0].Rows[0]["PlayerType"].ToString(); JObject js = new JObject(); js.Add("type", "loginResult"); js.Add("ifloginSuccess", "true"); js.Add("privatePassword", pas); js.Add("playerName", playername); if (playername != "") { js.Add("playerType", playerType); } client.Send(js.ToString()); dic.Add(client, pas); DbConnectionMySQL.ExecuteNonQuery(DbConnectionMySQL.Conn, CommandType.Text, "update game set secretkey='" + pas + "' where account=" + account, null); showData(); } else { //密码错误 showLog("客户端" + client.RemoteEndPoint + ":密码错误"); JObject js = new JObject(); js.Add("type", "loginResult"); js.Add("ifloginSuccess", "false"); js.Add("reason", "PasswordError"); client.Send(js.ToString()); } } else if (type == "createPlayer") { var secretKey = json["secretKey"].ToString(); var name = json["playerName"].ToString(); var playerType = json["playerType"].ToString(); //先判断是和数据库中否有重名的情况 if (DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where playerName='" + name + "'", null).Tables[0].Rows.Count == 0) { //创建成功 showLog("客户端" + client.RemoteEndPoint + ":创建角色成功,角色名:" + name); DbConnectionMySQL.ExecuteNonQuery(DbConnectionMySQL.Conn, CommandType.Text, "update game set playerName='" + name + "',playerType='" + playerType + "',XPoint=300.0,YPoint=300.0,grade='1',blood='100',direction=1 where secretKey='" + secretKey + "'", null); JObject js = new JObject(); js.Add("type", "craetePlayerResult"); js.Add("ifSuccess", "true"); js.Add("playerType", playerType); client.Send(js.ToString()); showData(); } else { //创建失败,有重名的情况 showLog("客户端" + client.RemoteEndPoint + ":创建角色失败"); JObject js = new JObject(); js.Add("type", "craetePlayerResult"); js.Add("ifSuccess", "false"); client.Send(js.ToString()); } } else if (type == "loginGameScene") { var secretKey = json["secretKey"].ToString();//获得通信秘钥 var playername = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["playerName"].ToString(); var playerType = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["PlayerType"].ToString(); var XPoint = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["XPoint"].ToString(); var YPoint = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["YPoint"].ToString(); var grade = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["grade"].ToString(); var blood = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["blood"].ToString(); var direction = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["direction"].ToString(); JObject js = new JObject(); js.Add("type", "playerInformation"); js.Add("playerName", playername); js.Add("playerType", playerType); js.Add("XPoint", float.Parse(XPoint)); js.Add("YPoint", float.Parse(YPoint)); js.Add("grade", int.Parse(grade)); js.Add("blood", float.Parse(blood)); js.Add("direction", int.Parse(direction)); client.Send(js.ToString()); DbConnectionMySQL.ExecuteNonQuery(DbConnectionMySQL.Conn, CommandType.Text, "update game set Online='是' where secretKey='" + secretKey + "'", null); showData(); showLog("角色" + playername + ":登录游戏"); } else if (type == "nearPlayers") { var secretKey = json["secretKey"].ToString();//获得通信秘钥 var playername = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["playerName"].ToString(); var playerType = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["PlayerType"].ToString(); var XPoint = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["XPoint"].ToString(); var YPoint = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["YPoint"].ToString(); var grade = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["grade"].ToString(); var blood = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["blood"].ToString(); var direction = DbConnectionMySQL.GetDataSet(DbConnectionMySQL.Conn, CommandType.Text, "select * from game where secretKey='" + secretKey + "'", null).Tables[0].Rows[0]["direction"].ToString(); JObject js = new JObject(); js.Add("type", "nearPlayer"); js.Add("playerName", playername); js.Add("playerType", playerType); js.Add("XPoint", float.Parse(XPoint)); js.Add("YPoint", float.Parse(YPoint)); js.Add("grade", int.Parse(grade)); js.Add("blood", float.Parse(blood)); js.Add("direction", int.Parse(direction)); js.Add("state", "free"); GetAllLayer(secretKey, client, js); } else if (type == "position") { var secretKey = json["secretKey"].ToString(); //获得通信秘钥 var state = json["state"].ToString(); //人物的当前状态 if (state == "walk") { var XPoint = float.Parse(json["XPoint"].ToString()); //人物的X坐标 var YPoint = float.Parse(json["YPoint"].ToString()); //人物的Y坐标 var direction = int.Parse(json["direction"].ToString()); //人物的朝向 var playerState = json["state"].ToString(); //更新数据库中人物的信息 DbConnectionMySQL.ExecuteNonQuery(DbConnectionMySQL.Conn, CommandType.Text, "update game set XPoint=" + XPoint + " where secretKey='" + secretKey + "'", null); DbConnectionMySQL.ExecuteNonQuery(DbConnectionMySQL.Conn, CommandType.Text, "update game set YPoint=" + YPoint + " where secretKey='" + secretKey + "'", null); DbConnectionMySQL.ExecuteNonQuery(DbConnectionMySQL.Conn, CommandType.Text, "update game set direction=" + direction + " where secretKey='" + secretKey + "'", null); //广播其他玩家 SendToAllPosition(secretKey, XPoint, YPoint, direction, playerState); } else if (state == "free") { SendToAllPosition(secretKey, 0.0f, 0.0f, 0, "free"); } // showData(); } else if (type == "message") { var secretKey = json["secretKey"].ToString(); //获得通信秘钥 var message = json["message"].ToString(); //获得发送的内容 var name = json["playerName"].ToString(); //获得发送消息的玩家 SendToMessage(secretKey, name, message); showLog("角色" + name + ":发送了一条消息,消息内容为:" + message); } }