4.8
#region --- Check Room List ---
/// <summary>
/// Check the existence of every room in room list received from client
/// </summary>
/// <param name="msg">string of room list</param>
private void CheckRoomList(string s_roomList)
{
// TODO : check the existence of chat file of each room
List<string> roomList = (List<string>)JsonConvert.DeserializeObject(s_roomList, typeof(List<string>));
foreach (string room in roomList)
{
string roomFile = room + ".txt";
if (!File.Exists(@roomFile))
{
FileStream fs = new FileStream(roomFile, FileMode.Create);
fs.Close();
}
}
}
#endregion
#region --- Get Room Message ---
/// <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>
private void GetRoomMsg(string clientIP, string roomId)
{
// TODO : get the history of specific chat room
string roomFile = roomId + ".txt";
List<string> msgList = new List<string>();
String sendMsg = "";
if (File.Exists(@roomFile))
{
StreamReader sr = new StreamReader(roomFile, Encoding.Default);
String lineMsg;
while ((lineMsg = sr.ReadLine()) != null)
msgList.Add(lineMsg);
sendMsg = JsonConvert.SerializeObject(msgList);
}
else
{
FileStream fs = new FileStream(roomFile, FileMode.Create);
fs.Close();
}
SendMessage(clientIP, REQUEST_ROOM_MSG, sendMsg);
}
#endregion
#region --- Add Message To File ---
/// <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>
private void AddMsgToFile(string clientIP, string msg)
{
// TODO : put the message into the specific room
MsgHandler msgHandler = (MsgHandler)JsonConvert.DeserializeObject(msg, typeof(MsgHandler));
string roomFile = msgHandler.roomId + ".txt";
FileStream fs = new FileStream(roomFile, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(msgHandler.msg);
sw.Close();
fs.Close();
}
#endregion
#region --- Remove Offline User ---
/// <summary>
/// remove off line user
/// </summary>
/// <param name="clientIP">the client IP</param>
private void RemoveOfflineUser(string clientIP)
{
Console.WriteLine("User IP : " + clientIP + " has went off line.");
if (dictSocket.ContainsKey(clientIP))
{
dictSocket[clientIP].Close();
dictSocket.Remove(clientIP);
}
if (dictThread.ContainsKey(clientIP))
{
Thread tmp = dictThread[clientIP];
dictThread.Remove(clientIP);
tmp.Abort();
}
}
#endregion
#region --- Invalid Message ---
/// <summary>
/// Handle the situation of invalid message
/// </summary>
/// <param name="clientIP">the client ip</param>
private void InvalidMsg(string clientIP)
{
// TODO : send invalid warning to client
SendMessage(clientIP, INVALID_MESSAGE, "");
}
#endregion