/// <summary> /// Action performed on chatroom change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void chatrooms_SelectedIndexChanged(object sender, EventArgs e) { // Do nothing if we select the dummy chatroom "Select a chatroom" // Meaning: leave the current chatroom, clear the interface if (chatrooms.Text == "Select a chatroom") { chatrooms.SelectedIndex = -1; ChatMessage quitCr = new ChatMessage(ChatMessage.Header.QUIT_CR); client.sendMessage(quitCr); if (messagesBindingList != null && usersBindingList != null) { messagesBindingList.Clear(); usersBindingList.Clear(); } } // Join the chatroom wanted otherwise if (chatrooms.Text != "" && chatrooms.Text != "Select a chatroom" && chatrooms.SelectedItem != null) { client.User.Chatroom = new Chatroom(chatrooms.Text); ChatMessage joinCr = new ChatMessage(ChatMessage.Header.JOIN_CR); joinCr.addData(chatrooms.Text); client.sendMessage(joinCr); } }
/// <summary> /// Periodically check users (from a thread) connected to the current chatroom /// </summary> private void getUsers() { while (!client.Quit) { try { // Now, check the users if (client.User.Chatroom != null && client.User.Chatroom.Name != "") { // We need to invoke chatrooms (UI thread) to see the selected index chatrooms.BeginInvoke( (Action)(() => { // If we are indeed connected to a chatroom if (chatrooms.Text != "") { ChatMessage messageUsers = new ChatMessage(ChatMessage.Header.LIST_USERS); messageUsers.addData(chatrooms.Text); client.sendMessage(messageUsers); } }) ); Thread.Sleep(2000); } } catch (Exception e) { Console.WriteLine(e.Message); } } }
/// <summary> /// Action performed on "Send" button click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void sendButton_Click(object sender, EventArgs e) { ChatMessage messageToSend = new ChatMessage(ChatMessage.Header.POST); messageToSend.addData(messageTextBox.Text); client.sendMessage(messageToSend); messageTextBox.Clear(); }