private void linkRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (isConnect) { ChatHeaderObject header = new ChatHeaderObject() { Header = Header.Quit, SessionFrom = this.user }; ChatPayloadObject payload = new ChatPayloadObject() { }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; this.chatClient.sendDataObject(chatData); } Login login = new Login(this._frmLogin, this.user); login.Location = this.Location; login.Width = this.Width; login.Height = this.Height; login.Anchor = this.Anchor; panel1.Controls.Clear(); panel1.Controls.Add(login); }
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { if (this.chatClient != null) { try { ChatHeaderObject header = new ChatHeaderObject() { Header = Header.Quit, SessionFrom = this.username }; ChatPayloadObject payload = new ChatPayloadObject() { }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; this.chatClient.sendDataObject(chatData); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
private void sendRegister(string fullname, string username, string password, string email) { ChatHeaderObject header = new ChatHeaderObject() { Header = Header.Register, SessionFrom = this.user }; ChatPayloadObject payload = new ChatPayloadObject() { Fullname = fullname, Username = username, Password = password, Email = email }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; try { this.chatClient.sendDataObject(chatData); } catch (Exception) { lblWarning.Text = "Kết nối tới máy chủ thất bại!"; isConnect = false; } }
private void sendRefresh() { while (this.chatClient != null) { try { ChatHeaderObject header = new ChatHeaderObject() { Header = Header.Refresh, SessionFrom = this.username }; ChatPayloadObject payload = new ChatPayloadObject() { }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; this.chatClient.sendDataObject(chatData); } catch (Exception e) { Console.WriteLine(e.Message); } System.Threading.Thread.Sleep(5000); } }
private void getRoomName() { ChatHeaderObject header = new ChatHeaderObject() { SessionFrom = this.username, Header = Header.GetRoomName }; ChatPayloadObject payload = new ChatPayloadObject() { Username = username, Fullname = this.fullname, Data = this.toUser }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; try { this.chatClient.sendDataObject(chatData); } catch (Exception e) { Console.WriteLine(e); } }
private void sendLoadMessage() { while (this.chatClient != null) { if (box != null && box.roomName != "" && loadBox) { try { ChatHeaderObject header = new ChatHeaderObject() { SessionFrom = this.username, Header = Header.LoadMessage, SessionTo = box.roomName }; ChatPayloadObject payload = new ChatPayloadObject() { }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; this.chatClient.sendDataObject(chatData); } catch (Exception e) { Console.WriteLine(e.Message); } loadBox = false; } System.Threading.Thread.Sleep(500); } }
private void checkLogin(string username, string password) { ChatHeaderObject header = new ChatHeaderObject() { SessionFrom = this.user, Header = Header.Login }; ChatPayloadObject payload = new ChatPayloadObject() { Username = username, Password = password }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; try { this.chatClient.sendDataObject(chatData); } catch (Exception) { lblWarning.Text = "Kết nối tới máy chủ thất bại!"; isConnect = false; } }
public override void eventRecvData(object o) { ChatDataObject chatDataObject = (ChatDataObject)o; String data = DateTime.Now + ":Session from " + chatDataObject.Header.SessionFrom + " with header: " + chatDataObject.Header.Header; invokeTextBox(this.textBox, data + "\r\n"); this.mqRequestQueue.Enqueue(o); }
private void btnLogin_Click(object sender, EventArgs e) { if (!isConnect) { getConfig(); connect(hostAddress, iPort); Thread.Sleep(200); isConnect = true; } string username = txtUsername.Text.Trim(); string password = txtPassword.Text.Trim(); lblWarning.Text = ""; if ("".Equals(username) || "".Equals(password)) { lblWarning.Text = "Chưa điền tài khoản/mật khẩu"; } else { checkLogin(username, password); if (chatClient != null) { try { Object o = this.chatClient.recvDataObject(); if (o != null) { ChatDataObject chatData = (ChatDataObject)o; if (chatData.Header.Header == Header.Login) { if (chatData.Payload.StatusCode == StatusCode.MissUsername) { invokeLabel(_login.lblWarning, "Tài khoản không chính xác"); } else if (chatData.Payload.StatusCode == StatusCode.PasswordWrong) { invokeLabel(_login.lblWarning, "Mật khẩu không chính xác"); } else if (chatData.Payload.StatusCode == StatusCode.CheckTrue) { chatData.Header.Header = Header.Quit; this.chatClient.sendDataObject(chatData); invokehideForm(_frmLogin); frmMain main = new frmMain(chatData.Payload.Username, chatData.Payload.Fullname); main.Show(); } } } } catch (Exception) { lblWarning.Text = "Kết nối tới máy chủ thất bại!"; isConnect = false; } } } }
private void runRecvData() { while (this.chatClient != null) { try { Object o = this.chatClient.recvDataObject(); if (o != null) { ChatDataObject chatData = (ChatDataObject)o; if (chatData.Header.Header == Header.Login) { if (chatData.Payload.StatusCode == StatusCode.MissUsername) { invokeLabel(_login.lblWarning, "Tài khoản không chính xác"); } else if (chatData.Payload.StatusCode == StatusCode.PasswordWrong) { invokeLabel(_login.lblWarning, "Mật khẩu không chính xác"); } else if (chatData.Payload.StatusCode == StatusCode.CheckTrue) { invokehideForm(_frmLogin); frmMain main = new frmMain(chatData.Payload.Username, chatData.Payload.Fullname); main.Show(); } } } } catch (Exception e) { Console.WriteLine(e.Message); } System.Threading.Thread.Sleep(200); } }
private void btnRegister_Click(object sender, EventArgs e) { if (!isConnect) { getConfig(); connect(hostAddress, iPort); System.Threading.Thread.Sleep(200); isConnect = true; } string fullname = txtFullname.Text.Trim(); string username = txtUsername.Text.Trim(); string email = txtEmail.Text.Trim(); string password = txtPassword.Text.Trim(); lblWarning.ForeColor = Color.Red; if ("".Equals(fullname) || "".Equals(username) || "".Equals(password) || "".Equals(email)) { lblWarning.Text = "Vui lòng điền đầy đủ các trường"; } else { sendRegister(fullname, username, password, email); if (chatClient != null) { try { Object o = this.chatClient.recvDataObject(); if (o != null) { ChatDataObject chatData = (ChatDataObject)o; if (chatData.Header.Header == Header.Register) { if (chatData.Payload.StatusCode == StatusCode.ExistEmail) { this.lblWarning.Text = "Email đã tồn tại!"; //invokeLabel(_register.lblWarning, "Email đã tồn tại"); } else if (chatData.Payload.StatusCode == StatusCode.ExistUsername) { this.lblWarning.Text = "Tài khoản đã tồn tại"; //invokeLabel(_register.lblWarning, "Tên tài khoản đã tồn tại"); } else if (chatData.Payload.StatusCode == StatusCode.MissField) { this.lblWarning.Text = "Vui lòng điền đầy đủ các trường!"; //invokeLabel(_register.lblWarning, "Vui lòng điền đầy đủ các trường!"); } else if (chatData.Payload.StatusCode == StatusCode.CheckTrue) { this.txtEmail.Text = ""; this.txtFullname.Text = ""; this.txtPassword.Text = ""; this.txtUsername.Text = ""; this.lblWarning.Text = "Đăng ký thành công"; this.lblWarning.ForeColor = Color.Green; //invokeLabel(_register.lblWarning, "Đăng ký thành công!"); chatData.Header.Header = Header.Quit; this.chatClient.sendDataObject(chatData); isConnect = false; } } } } catch (Exception) { lblWarning.Text = "Kết nối tới máy chủ thất bại!"; //ChatHeaderObject header = new ChatHeaderObject() //{ // Header = Header.Quit, // SessionFrom = this.user //}; //ChatPayloadObject payload = new ChatPayloadObject() //{ //}; //ChatDataObject chatData = new ChatDataObject() //{ // Header = header, // Payload = payload //}; //this.chatClient.sendDataObject(chatData); } } } }
private void runRecvData() { while (this.chatClient != null) { try { Object o = chatClient.recvDataObject(); if (o != null) { ChatDataObject chatData = (ChatDataObject)o; if (chatData.Header.Header == Header.Refresh) { string allUser = chatData.Payload.Data; Dictionary <string, string> dictUsers = getAllUser(allUser); usersDict = dictUsers; invokeButton(_main, dictUsers); } if (chatData.Header.Header == Header.Message) { if (box != null) { if (chatData.Header.ChatType == ChatTypeMess.Message) { string fullname = chatData.Payload.Fullname; string message = chatData.Payload.Data; string time = chatData.Payload.Time.ToShortTimeString(); TempMessage tempMessage = new TempMessage() { Fullname = fullname, Message = message, Username = chatData.Header.SessionFrom, ChatType = chatData.Header.ChatType, Time = time }; invokeOneMessageToChatBox(box, tempMessage); } else if (chatData.Header.ChatType == ChatTypeMess.File) { } } else { AddChatBox(chatData.Payload.Fullname); } } if (chatData.Header.Header == Header.LoadMessage) { string tempMessages = chatData.Payload.Data; List <TempMessage> messages = convertStringToTempMessage(tempMessages); if (box != null) { if (messages.Count > 0) { invokeChatBox(box, messages); } } } if (chatData.Header.Header == Header.GetRoomName) { if (box != null) { box.roomName = chatData.Payload.ChatroomName; } } } } catch (Exception e) { Console.WriteLine(e); } System.Threading.Thread.Sleep(200); } }
private void button2_Click(object sender, EventArgs e) { if (type == ChatObject.ChatTypeMess.Message) { string mess = txtMessage.Text; if (mess != "" && mess != null) { sendMessage(this.fullname, mess, DateTime.Now.ToShortTimeString()); ChatHeaderObject header = new ChatHeaderObject() { Header = Header.Message, SessionFrom = this.username, SessionTo = this.roomName, ChatType = type }; ChatPayloadObject payload = new ChatPayloadObject() { MessageType = this.messageType, Data = mess, Time = DateTime.Now, Fullname = this.fullname }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; this.chatClient.sendDataObject(chatData); txtMessage.Text = ""; } } else if (type == ChatObject.ChatTypeMess.File) { MessageBox.Show("Chức năng đang phát triển!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); //try //{ // byte[] dataFile = File.ReadAllBytes(txtMessage.Text.Trim()); // string dataTranfer = Convert.ToBase64String(dataFile); // string filePath = txtMessage.Text.Trim(); // string[] temp = filePath.Split('\\'); // string filename = temp[temp.Length - 1]; // sendFile(this.fullname, filename, DateTime.Now.ToShortTimeString()); // ChatHeaderObject header = new ChatHeaderObject() // { // Header = Header.Upload, // SessionFrom = this.username, // SessionTo = this.roomName, // ChatType = type // }; // ChatPayloadObject payload = new ChatPayloadObject() // { // MessageType = this.messageType, // Data = dataTranfer, // Time = DateTime.Now, // Filename = filename // }; // ChatDataObject chatData = new ChatDataObject() // { // Header = header, // Payload = payload // }; // this.chatClient.sendDataObject(chatData); // txtMessage.Text = ""; //} //catch (Exception) //{ // MessageBox.Show("Đường dẫn file sai", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); //} } }
private void runRecvData() { List <MixUserInRoom> userInRooms = new List <MixUserInRoom>(); bool messageChat = false; bool status = true; while (this.chatServerControl != null && this.chatServerControl.isConnected()) { try { Object o = this.mqRequestQueue.Dequeue(); string data = ""; string username = ""; if (o != null) { ChatDataObject chatData = (ChatDataObject)o; SocketChannel channel = null; if (chatData.Header.SessionTo == null) { channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; } else { channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionTo]; } if (chatData.Header.Header == Header.Download) { channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; string fileName = chatData.Payload.Filename; string dataTranfer = ""; try { byte[] dataFile = File.ReadAllBytes(appFolder + "\\" + fileName); dataTranfer = Convert.ToBase64String(dataFile); data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " download file " + fileName; } catch (Exception) { data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " download file " + fileName + " - filename wrong!"; } chatData.Payload.Data = dataTranfer; } else if (chatData.Header.Header == Header.Upload) { string fileName = chatData.Payload.Filename; int chatId = int.Parse(chatData.Header.SessionTo); int messageTypeId = int.Parse(chatData.Payload.MessageType); byte[] dataFile = Convert.FromBase64String(chatData.Payload.Data); File.WriteAllBytes(appFolder + "\\" + fileName, dataFile); int id = getUserId(chatData.Header.SessionFrom); if (id > 0) { Model.Message message = new Model.Message() { ChatroomName = chatData.Header.SessionTo, Content = chatData.Payload.Filename, UserId = id, Time = DateTime.Now }; addMessageToDB(message); } messageChat = true; userInRooms = getUserInRooms(chatData.Header.SessionTo, chatData.Header.SessionFrom); data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " upload file " + fileName; } else if (chatData.Header.Header == Header.Register) { username = chatData.Payload.Username; string email = chatData.Payload.Email; string password = chatData.Payload.Password; string fullname = chatData.Payload.Fullname; channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; if (checkExistEmail(email)) { status = false; chatData.Payload.StatusCode = StatusCode.ExistEmail; data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " register failure. Exist email error!"; } else if (checkExistUsername(username)) { status = false; chatData.Payload.StatusCode = StatusCode.ExistUsername; data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " register failure. Exist username error!"; } else { if (checkField(fullname, username, email, password)) { status = false; chatData.Payload.StatusCode = StatusCode.MissField; data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " register failure. Miss fields error!"; } else { chatData.Payload.StatusCode = StatusCode.CheckTrue; addUser(fullname, username, email, password); data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " register successfully!"; } } } else if (chatData.Header.Header == Header.Login) { username = chatData.Payload.Username; string password = chatData.Payload.Password; channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; if (!checkExistUsername(username)) { status = false; chatData.Payload.StatusCode = StatusCode.MissUsername; data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " login failure. Miss username error!"; } else { if (!checkPassword(username, password)) { chatData.Payload.StatusCode = StatusCode.PasswordWrong; data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " login failure. Password wrong error!"; } else { User user = getUser(username); chatData.Payload.Password = null; chatData.Payload.Fullname = user.Fullname; chatData.Payload.StatusCode = StatusCode.CheckTrue; data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " username: "******" login successfully!"; } } } else if (chatData.Header.Header == Header.Message) { messageChat = true; if (chatData.Header.ChatType == ChatTypeMess.Message) { int id = getUserId(chatData.Header.SessionFrom); data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " username: "******": message to " + chatData.Header.SessionTo; if (id > 0) { Model.Message message = new Model.Message() { ChatroomName = chatData.Header.SessionTo, Content = chatData.Payload.Data, UserId = id, Time = DateTime.Now }; addMessageToDB(message); } } userInRooms = getUserInRooms(chatData.Header.SessionTo, chatData.Header.SessionFrom); } else if (chatData.Header.Header == Header.LoadMessage) { channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; List <TempMessage> messages = GetTempMessages(chatData.Header.SessionTo); data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " username: "******" load message!"; chatData.Payload.Data = covertTempMessageToString(messages); } else if (chatData.Header.Header == Header.Quit) { channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; chatData.Payload.StatusCode = StatusCode.CheckTrue; data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " quit!"; isQuit = true; } else if (chatData.Header.Header == Header.Refresh) { channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; string allUser = getAllUser(chatData.Header.SessionFrom); chatData.Payload.Data = allUser; data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " get all user!"; } else if (chatData.Header.Header == Header.GetRoomName) { channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; chatData.Payload.ChatroomName = GetRoomName(chatData.Payload.Fullname, chatData.Payload.Data); if (checkUser(chatData.Payload.Data)) { addUserToChat(chatData.Payload.ChatroomName, chatData.Payload.Fullname, chatData.Payload.Data); } data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " get room name!"; } else if (chatData.Header.Header == Header.CreateRoom) { data = DateTime.Now + ":Session from " + chatData.Header.SessionFrom + " create chat room " + chatData.Payload.ChatroomName; channel = (SocketChannel)this.mhSessionTable[chatData.Header.SessionFrom]; if (createNewRoom(chatData.Payload.ChatroomName, chatData.Header.SessionFrom, chatData.Payload.Data)) { chatData.Payload.StatusCode = StatusCode.CheckTrue; } else { chatData.Payload.StatusCode = StatusCode.CreateFail; } } if (isQuit) { isQuit = false; channel.stopChannel(); this.mhSessionTable.Remove(chatData.Header.SessionFrom); } if (messageChat) { foreach (MixUserInRoom user in userInRooms) { try { chatData.Header.SessionTo = user.Username; chatData.Payload.Fullname = user.Fullname; chatData.Payload.ChatroomName = user.RoomName; channel = (SocketChannel)this.mhSessionTable[user.Username]; if (channel != null) { channel.sendData(chatData); } } catch (Exception) { } } messageChat = false; } else { channel.sendData(chatData); channel.logMonitor(data); } if (status == false) { if (username != "") { channel.sendData(chatData); System.Threading.Thread.Sleep(300); } } } } catch (Exception e) { Console.Write(e); } System.Threading.Thread.Sleep(300); } }
private void btnCreateRoom_Click(object sender, EventArgs e) { string roomName = txtRoomName.Text; if (!"".Equals(roomName)) { List <string> users = new List <string>(); foreach (string user in lbUsers.CheckedItems) { users.Add(user); } if (users.Count > 0) { string usersString = convertUserListToString(users); ChatHeaderObject header = new ChatHeaderObject() { SessionFrom = this.username, Header = Header.CreateRoom }; ChatPayloadObject payload = new ChatPayloadObject() { ChatroomName = roomName, Data = usersString }; ChatDataObject chatData = new ChatDataObject() { Header = header, Payload = payload }; this.chatClient.sendDataObject(chatData); if (chatClient != null) { try { Object o = this.chatClient.recvDataObject(); if (o != null) { ChatDataObject chatDataRecv = (ChatDataObject)o; if (chatDataRecv.Header.Header == Header.CreateRoom) { if (chatDataRecv.Payload.StatusCode == StatusCode.CheckTrue) { lblWarning.Text = "Tạo phòng thành công!"; } else { lblWarning.Text = "Tên phòng trùng/ chưa chọn thành viên!"; } } } } catch (Exception) { } } } else { lblWarning.Text = "Chưa chọn thành viên!"; } } else { lblWarning.Text = "Chưa nhập tên phòng!"; } }