-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
134 lines (112 loc) · 4.3 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ChattingClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
/// This is the main class...
public partial class MainWindow : Window
{
public static IChattingService Server;
//Duplex channel allows more than one user to connect at one time
private static DuplexChannelFactory<IChattingService> _channelFactory;
public MainWindow()
{
InitializeComponent();
_channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(),
"ChattingServicesEndPoint");
Server = _channelFactory.CreateChannel();
//I have slpit the classes into sections so it is easier to read and understand.
}
public void TakeMessage(string message, string userName)
{
//Show username + message in the text box.
//End user will also see message and username in this order.
TextDisplayTextBox.Text += userName + ": " + message + "\n";
TextDisplayTextBox.ScrollToEnd();
}
//New command...
private void SendButton_Click(object sender, RoutedEventArgs e)
{
//if message in message text box has no characters - dont allow send.
if(MessageTextBox.Text.Length == 0)
{
return;
}
//This line of command tells the system to show username and message sent.
Server.SendMessageToAll(MessageTextBox.Text, UserNameTextBox.Text);
TakeMessage(MessageTextBox.Text, "You");
MessageTextBox.Text = "";
}
//New command...
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
//username must be entered in order for action LOGIN to be successful
int returnValue = Server.Login(UserNameTextBox.Text);
if (returnValue == 1)
{
//if I am logged in, do not let someone else login again...
MessageBox.Show("You are already logged in. Log out first and try again");
}
//Otherwise connect the user successfully...
else if (returnValue == 0)
{
//once username is entered, do not allow another client to login with same credentials.
UserNameTextBox.IsEnabled = false;
//disable login button.
LoginButton.IsEnabled = false;
//Once logged in, message view box will be set as non edit mode ***
TextDisplayTextBox.IsEnabled = false;
//load our users...
LoadUserList(Server.GetCurrentUsers());
}
}
//This line of code makes the text box public so that the messages can appear
//The line of code below fixed the issue
//This is now classed as FIX.
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Server.Logout();
}
public void AddUserToList(string userName)
{
if (UsersListBox.Items.Contains(userName))
{
return;
}
UsersListBox.Items.Add(userName);
}
public void RemoveUserFromList(string userName)
{
if (UsersListBox.Items.Contains(userName))
{
UsersListBox.Items.Remove(userName);
}
}
private void LoadUserList(List<string> users)
{
foreach(var user in users)
{
AddUserToList(user);
}
}
}
}