public ChatForm(bool isServer = false, UserInfo userInfo=null) { InitializeComponent(); this.isServer = isServer; if (isServer) { socketServer = new SocketServer(8080, this); socketServer.start(); ChatRichTextForm.AppendText("Server start\n"); userChat = new UserChat(new UserInfo("server"), this); } else { userChat = new UserChat(userInfo, this); userChat.sendMessageObject(new TcpMessage(TcpMessage.TypeMsg.Login, "", userInfo.getName())); //userChat.sendMessage(userInfo.getName()); Thread backgroundThread = new Thread(new ThreadStart(userChat.listenNewMessage)); backgroundThread.Start(); } }
private void button1_Click(object sender, EventArgs e) { TcpClient clientSocket = new TcpClient(); try { clientSocket.Connect(address.Text, Int32.Parse(port.Text)); UserInfo userInfo = new UserInfo(loginText.Text, clientSocket); ChatForm chatForm = new ChatForm(false, userInfo); chatForm.Show(); this.Hide(); } catch (ArgumentNullException ane) { MessageBox.Show("ArgumentNullException : {0}", ane.ToString()); } catch (SocketException se) { MessageBox.Show("SocketException : {0}", se.Message); } catch (Exception se) { MessageBox.Show("Unexpected exception : {0}", se.ToString()); } }
private void Initialize() { isConnected = true; UserInfo = new UserInfo("[Unknown]"); IP = client.Client.RemoteEndPoint.ToString(); BeginRead(); }
protected void OnUserDisconnected(UserInfo userInfo) { if (UserDisconnected != null) { UserDisconnected(userInfo); } }
public bool SendTo(UserInfo userInfo, PacketInfo packetInfo) { if (userInfo != null && !string.IsNullOrEmpty(userInfo.Nickname)) { Client client = Clients.FirstOrDefault(x => x.UserInfo.Nickname == userInfo.Nickname); if (client != null) { client.SendPacket(packetInfo); return true; } } return false; }
private void OnUserListReceived(UserInfo[] userList) { if (UserListReceived != null) UserListReceived(userList); }
private void OnUserDisconnected(UserInfo info) { if (UserDisconnected != null) UserDisconnected(info); }
private void server_UserDisconnected(UserInfo userInfo) { InvokeForm(() => { ListViewItem lvi = ServerFindUser(userInfo); if (lvi != null) lvi.Remove(); }); }
private void server_UserConnected(UserInfo userInfo) { InvokeForm(() => { lvServerUsers.Items.Add(userInfo.Nickname); }); }
private ListViewItem ServerFindUser(UserInfo userInfo) { foreach (ListViewItem lvi in lvServerUsers.Items) { if (lvi.Text == userInfo.Nickname) return lvi; } return null; }
private void client_UserListReceived(UserInfo[] userList) { InvokeForm(() => { foreach (UserInfo userInfo in userList) { if (!lvClientUsers.Items.Cast<ListViewItem>().Any(x => x.Text == userInfo.Nickname)) { lvClientUsers.Items.Add(userInfo.Nickname); } } }); }
private void client_UserConnected(UserInfo userInfo) { InvokeForm(() => { if (!lvClientUsers.Items.Cast<ListViewItem>().Any(x => x.Text == userInfo.Nickname)) { lvClientUsers.Items.Add(userInfo.Nickname); } }); }