public void AddToDBTimeNow <T>(ChatPacket chatPack) { string sql; string Time = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); if (typeof(T) == typeof(User)) { User user = BlindChatUtil.ChatPacketToStruct <User>(chatPack); sql = $"insert into User (ID, Name, Time, isOnline) values ({user.ID}, {user.Name}, \'{Time}\', {user.Online});"; } else if (typeof(T) == typeof(ChatRoom)) { ChatRoom room = new ChatRoom(); sql = $"insert into ChatRoom (ID, Name, Time) values ({room.ID},\'{room.Name}\',\'{Time}\');"; } else if (typeof(T) == typeof(ChatRoomJoined)) { ChatRoomJoined roomjoined = new ChatRoomJoined(); sql = $"insert into ChatRoomJoined (ID, UserID, RoomID, Time) values ({roomjoined.ID},{roomjoined.UserID},{roomjoined.RoomID},\'{Time}\');"; } else if (typeof(T) == typeof(ChatMessage)) { ChatMessage msg = BlindChatUtil.ChatPacketToStruct <ChatMessage>(chatPack); sql = $"insert into ChatMessage (UserID, RoomID, Message, Time) values ({msg.UserID},{msg.RoomID},\'{msg.Message}\',\'{msg.Time}\');"; } else { return; } ExecuteQuery(sql); }
public void ExecuteExit(ChatPacket chatPack) { string timeNow = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); ChatRoomJoined roomJoined = BlindChatUtil.ChatPacketToStruct <ChatRoomJoined>(chatPack); SendChatPacketToParticipants(chatPack, roomJoined.RoomID); string sql = $"delete from ChatRoomJoined where UserID = {roomJoined.UserID} and RoomID = {roomJoined.RoomID};"; ExecuteQuery(sql); ChatMessage message = new ChatMessage(); message.RoomID = roomJoined.RoomID; message.UserID = 0; message.Time = timeNow; sql = $"select * from User where ID = {roomJoined.UserID}"; ChatMessage _message = new ChatMessage(); message.RoomID = roomJoined.RoomID; message.UserID = 0; message.Time = timeNow; sql = $"select * from User where ID = {roomJoined.UserID}"; MySqlDataAdapter adpt = new MySqlDataAdapter(sql, hDB); DataSet ds = new DataSet(); adpt.Fill(ds); DataRow r = ds.Tables[0].Rows[0]; User userInfo = (User)GetStructFromDB <User>(r); message.Message = $"{userInfo.Name}님이 나갔습니다."; byte[] _data = BlindNetUtil.StructToByte(message); ChatPacket _packet = BlindChatUtil.ByteToChatPacket(_data, ChatType.Message); SendChatPacketToParticipants(_packet, message.RoomID); //시간을 수정한 메시지를 DB에 등록 AddToDBTimeNow <ChatMessage>(_packet); }
public void ClientUpdateChatRoomJoined(string Time) { string sql = $"select * from ChatRoomJoined " + $"where RoomID in " + $"(select roomID from ChatRoomJoined where UserID={UserID}) " + $"and Time > \'{Time}\' order by Time asc"; MySqlDataReader rdr = ExecuteSelect(sql); while (rdr.Read()) { ChatRoomJoined chatRoomJoined = (ChatRoomJoined)GetStructFromDB <ChatRoomJoined>(rdr); byte[] data = BlindNetUtil.StructToByte(chatRoomJoined); ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, ChatType.RoomJoined); ChatPacketSend(chatPacket); } rdr.Close(); }
public void ExecuteInvitation(ChatPacket chatPack) { ChatRoomJoined roomJoined = BlindChatUtil.ChatPacketToStruct <ChatRoomJoined>(chatPack); //사용자 등록 string timeNow = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); string sql = $"insert into ChatRoomJoined (UserID, RoomID, Time) values ({roomJoined.UserID},{roomJoined.RoomID},\'{timeNow}\');"; ExecuteQuery(sql); //새로 추가한 사용자 검색 sql = $"select * from ChatRoomJoined where Time = \'{timeNow}\' and UserID={ roomJoined.UserID }"; MySqlDataReader rdr = ExecuteSelect(sql); byte[] data; if (rdr.Read()) { roomJoined = (ChatRoomJoined)GetStructFromDB <ChatRoomJoined>(rdr); rdr.Close(); { sql = $"select * from ChatRoom where ID = {roomJoined.RoomID}"; rdr = ExecuteSelect(sql); if (rdr.Read()) { ChatRoom room = (ChatRoom)GetStructFromDB <ChatRoom>(rdr); rdr.Close(); data = BlindNetUtil.StructToByte(room); ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, ChatType.Room); SendChatPacketToUser(chatPacket, roomJoined.UserID); } } //방 인원 전송 { sql = $"select * from ChatRoomJoined " + $"where RoomID = {roomJoined.RoomID} order by Time asc"; MySqlDataAdapter adptr = new MySqlDataAdapter(sql, hDB); DataSet dst = new DataSet(); adptr.Fill(dst); foreach (DataRow dr in dst.Tables[0].Rows) { ChatRoomJoined chatRoomJoined = (ChatRoomJoined)GetStructFromDB <ChatRoomJoined>(dr); if (chatRoomJoined.UserID != roomJoined.UserID) { data = BlindNetUtil.StructToByte(chatRoomJoined); ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, ChatType.RoomJoined); SendChatPacketToUser(chatPacket, roomJoined.UserID); } } rdr.Close(); } data = BlindNetUtil.StructToByte(roomJoined); //방 사용자들에게 전송 ChatPacket packet = BlindChatUtil.ByteToChatPacket(data, ChatType.RoomJoined); SendChatPacketToParticipants(packet, roomJoined.RoomID); ChatMessage message = new ChatMessage(); message.RoomID = roomJoined.RoomID; message.UserID = 0; message.Time = timeNow; sql = $"select * from User where ID = {roomJoined.UserID}"; MySqlDataAdapter adpt = new MySqlDataAdapter(sql, hDB); DataSet ds = new DataSet(); adpt.Fill(ds); DataRow r = ds.Tables[0].Rows[0]; User userInfo = (User)GetStructFromDB <User>(r); message.Message = $"{userInfo.Name}님이 입장하셨습니다."; byte[] _data = BlindNetUtil.StructToByte(message); ChatPacket _packet = BlindChatUtil.ByteToChatPacket(_data, ChatType.Message); SendChatPacketToParticipants(_packet, message.RoomID); //시간을 수정한 메시지를 DB에 등록 AddToDBTimeNow <ChatMessage>(_packet); Console.WriteLine("invite successed"); } else { Console.WriteLine("cant find the user"); } }
public object GetStructFromDB <T>(MySqlDataReader rdr) { object st; try { if (typeof(T) == typeof(User)) { User user = new User(); user.ID = uint.Parse(rdr["ID"].ToString()); user.Online = int.Parse(rdr["isOnline"].ToString()); user.Name = rdr["Name"].ToString(); user.Time = rdr["Time"].ToString(); user.Position = rdr["Position"].ToString(); user.Department = rdr["Department"].ToString(); user.Phone = rdr["Phone"].ToString(); user.Email = rdr["Email"].ToString(); user.Birth = rdr["Birth"].ToString(); user.Online = 0; st = user; } else if (typeof(T) == typeof(ChatRoom)) { ChatRoom room = new ChatRoom(); room.ID = int.Parse(rdr["ID"].ToString()); room.Name = rdr["Name"].ToString(); room.Time = rdr["Time"].ToString(); room.LastMessageTime = rdr["LastMessageTime"].ToString(); st = room; } else if (typeof(T) == typeof(ChatRoomJoined)) { ChatRoomJoined roomjoined = new ChatRoomJoined(); roomjoined.ID = int.Parse(rdr["ID"].ToString()); roomjoined.UserID = int.Parse(rdr["UserID"].ToString()); roomjoined.RoomID = int.Parse(rdr["RoomID"].ToString()); roomjoined.Time = rdr["Time"].ToString(); st = roomjoined; } else if (typeof(T) == typeof(ChatMessage)) { ChatMessage message = new ChatMessage(); message.UserID = uint.Parse(rdr["UserID"].ToString()); message.RoomID = int.Parse(rdr["RoomID"].ToString()); message.Time = rdr["Time"].ToString(); message.Message = rdr["Message"].ToString(); message.ID = int.Parse(rdr["ID"].ToString()); st = message; } else { return(null); } return(st); } catch (Exception e) { Console.WriteLine(e.Message); return(null); } }