/// <summary> /// put the message into the specific room /// </summary> /// <param name="clientIP">the client IP</param> /// <param name="msg">the string include the room id and received message</param> public void AddMsgToFile(string clientIP, string msg) { // TODO : put the message into the specific room MsgHandler msgHandler = (MsgHandler)JsonConvert.DeserializeObject(msg, typeof(MsgHandler)); string roomFile = "DataBase\\" + msgHandler.roomId + ".txt"; if (Server.dictLock.ContainsKey(msgHandler.roomId)) { try { lock (Server.dictLock[msgHandler.roomId]) { FileStream fs = File.OpenWrite(roomFile); fs.Position = fs.Length; foreach (string message in msgHandler.msgList) { byte[] writeMsg = Encoding.UTF8.GetBytes(message + "\r\n"); fs.Write(writeMsg, 0, writeMsg.Length); } fs.Close(); } } catch (Exception e) { Console.WriteLine("[Error]Exception happens during writing message into room " + msgHandler.roomId + ". [ExceptionMsg]" + e.Message); } } else { Server.dictLock.Add(msgHandler.roomId, new Object()); try { lock (Server.dictLock[msgHandler.roomId]) { FileStream fs = new FileStream(roomFile, FileMode.Create); fs.Close(); } } catch (Exception e) { Console.WriteLine("[Error]Exception happens during creating new room " + msgHandler.roomId + " in FUNC[AddMsgToFile]. [ExceptionMsg]" + e.Message); } try { lock (Server.dictLock["room"]) { string romFile = "room.txt"; FileStream f = File.OpenWrite(romFile); f.Position = f.Length; byte[] writeMsg = Encoding.UTF8.GetBytes(msgHandler.roomId + "\r\n"); f.Write(writeMsg, 0, writeMsg.Length); f.Close(); } } catch (Exception e) { Console.WriteLine("[Error]Exception happens during writing message into room " + msgHandler.roomId + " in FUNC[AddMsgToFile]. [ExceptionMsg]" + e.Message); } } GetRoomMsg(clientIP, msgHandler.roomId); }
/// <summary> /// get the history message of the specific chat room /// </summary> /// <param name="clientIP">the client IP</param> /// <param name="msg">the room ID</param> public void GetRoomMsg(string clientIP, string roomId) { // TODO : get the history of specific chat room string roomFile = "DataBase\\" + roomId + ".txt"; List <string> msgList = new List <string>(); MsgHandler msgHandler; String sendMsg = ""; if (Server.dictLock.ContainsKey(roomId)) { try { lock (Server.dictLock[roomId]) { StreamReader sr = new StreamReader(roomFile, Encoding.UTF8); String lineMsg; while ((lineMsg = sr.ReadLine()) != null) { lineMsg += ("\r\n" + sr.ReadLine()); msgList.Add(lineMsg); } sr.Close(); } msgHandler = new MsgHandler(roomId, msgList); sendMsg = JsonConvert.SerializeObject(msgHandler); } catch (Exception e) { Console.WriteLine("[Error]Exception happens during getting message from room " + roomId + ". [ExceptionMsg]" + e.Message); } } else { Server.dictLock.Add(roomId, new Object()); lock (Server.dictLock[roomId]) { try { FileStream fs = new FileStream(roomFile, FileMode.Create); fs.Close(); } catch (Exception e) { Console.WriteLine("[Error]Exception happens during creating new room " + roomId + " in FUNC[GetRoomMsg]. [ExceptionMsg]" + e.Message); } try { lock (Server.dictLock["room"]) { string romFile = "room.txt"; FileStream f = File.OpenWrite(romFile); f.Position = f.Length; byte[] writeMsg = Encoding.UTF8.GetBytes(roomId + "\r\n"); f.Write(writeMsg, 0, writeMsg.Length); f.Close(); msgHandler = new MsgHandler(roomId); sendMsg = JsonConvert.SerializeObject(msgHandler); } } catch (Exception e) { Console.WriteLine("[Error]Exception happens during getting message from room " + roomId + ". P.S:It shouldn't be here. [ExceptionMsg]" + e.Message); } } } SendMessage(clientIP, SEND_MSG, sendMsg); }