-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
263 lines (235 loc) · 8.45 KB
/
Copy pathMainPage.xaml.cs
File metadata and controls
263 lines (235 loc) · 8.45 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using Kaazing.HTML5;
using Kaazing.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace EchoDemo
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public PasswordAuthentication Credentials;
public System.Threading.AutoResetEvent userInputCompleted = new System.Threading.AutoResetEvent(false);
private WebSocket ws;
private WebSocketFactory factory;
// Constructor
public MainPage()
{
InitializeComponent();
// setup ChallengeHandler
factory = new WebSocketFactory();
BasicChallengeHandler handler = BasicChallengeHandler.Create();
handler.LoginHandler = new LoginHandlerDemo(this);
factory.ChallengeHandler = handler;
}
private void connectButton_Click(object sender, RoutedEventArgs e)
{
if (ws == null || ws.ReadyState != WebSocket.OPEN)
{
connectButton.IsEnabled = false;
this.url.IsEnabled = false;
String url = this.url.Text;
ws = factory.CreateWebSocket();
ws.OpenEvent += ws_OpenEvent;
ws.CloseEvent += ws_CloseEvent;
ws.MessageEvent += ws_MessageEvent;
ws.Connect(url);
log("Connecting to " + url);
}
}
private void disconnectButton_Click(object sender, RoutedEventArgs e)
{
if (ws != null && ws.ReadyState == WebSocket.OPEN)
{
log("CLOSE");
ws.Close();
}
}
private void sendButton_Click(object sender, RoutedEventArgs e)
{
if (ws != null && ws.ReadyState == WebSocket.OPEN)
{
log("SEND: " + this.sendText.Text, Colors.Green);
string text = this.sendText.Text;
if ("long".Equals(text))
{
// send large data
StringBuilder message = new StringBuilder();
message.Append("1");
for (int i = 0; i < 20; i++)
{
String lastMessage = message.ToString();
message.Append(lastMessage);
}
text = message.ToString();
}
if ("utf-8".Equals(text))
{
// send large data
text = "";
for (int i = 0; i < 1024; i++)
{
text += (char)i;
}
}
bool? sendBinary = this.binaryButton.IsChecked;
if (sendBinary.HasValue && sendBinary.Value)
{
ws.Send(ByteBuffer.Wrap(UTF8Encoding.UTF8.GetBytes(text)));
}
else
{
ws.Send(text);
}
}
}
private void clearButton_Click(object sender, RoutedEventArgs e)
{
logList.Items.Clear();
}
void ws_MessageEvent(object sender, MessageEventArgs args)
{
if (args._buf.Remaining() > 1024)
{
switch (args.MessageType)
{
case EventType.BINARY:
log("BINARY MESSAGE: Length = " + args.Data.Count, Colors.Blue);
break;
case EventType.TEXT:
log("TEXT MESSAGE: Length = " + args.Data.Count, Colors.Blue);
break;
case EventType.CLOSE:
log("CLOSE MESSAGE: Length = " + args.Data.Count);
break;
}
}
else
{
switch (args.MessageType)
{
case EventType.BINARY:
log("BINARY MESSAGE: " + BitConverter.ToString(args.Data.Array), Colors.Blue);
break;
case EventType.TEXT:
log("TEXT MESSAGE: " + Encoding.UTF8.GetString(args.Data.Array, args.Data.Offset, args.Data.Count), Colors.Blue);
break;
case EventType.CLOSE:
log("CLOSE MESSAGE: " + args.Data);
break;
}
}
}
void ws_OpenEvent(object sender, OpenEventArgs args)
{
var t = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
log("CONNECTED");
connectButton.IsEnabled = false;
disconnectButton.IsEnabled = true;
sendButton.IsEnabled = true;
});
}
void ws_CloseEvent(object sender, CloseEventArgs args)
{
var t = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
this.url.IsEnabled = true;
log("CLOSED");
connectButton.IsEnabled = true;
disconnectButton.IsEnabled = false;
sendButton.IsEnabled = false;
});
}
private const int MAX_LOG_SIZE = 100;
private void log(string message)
{
var t = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (logList.Items.Count >= MAX_LOG_SIZE)
{
logList.Items.Clear();
}
ListBoxItem item = new ListBoxItem();
item.Content = message;
logList.Items.Insert(0, item);
});
}
private void log(string message, Color foreground)
{
var t = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (logList.Items.Count >= MAX_LOG_SIZE)
{
logList.Items.Clear();
}
ListBoxItem item = new ListBoxItem();
item.Content = message;
item.Foreground = new SolidColorBrush(foreground);
logList.Items.Insert(0, item);
});
}
//ChallengeHandler code
public void AuthenticationHandler()
{
//popup login page
var t = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (loginPopup.Child == null)
{
Border border = new Border();
StackPanel panel1 = new StackPanel();
panel1.Height = 600;
panel1.Width = 370;
panel1.Background = new SolidColorBrush(Colors.Black);
panel1.Opacity = 0.8;
StackPanel panel2 = new StackPanel();
panel2.Background = new SolidColorBrush(Colors.White);
panel2.Opacity = 100;
panel2.Margin = new Thickness(0, 50, 0, 0);
LoginControl login = new LoginControl(this);
panel2.Children.Add(login);
panel1.Children.Add(panel2);
border.Child = panel1;
loginPopup.Child = border;
}
loginPopup.IsOpen = true;
});
}
public void CloseLoginPage()
{
var t = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
loginPopup.IsOpen = false;
});
}
private void OnUrlTextChanged(object sender, TextChangedEventArgs e)
{
if (url.Text.Length == 0)
{
connectButton.IsEnabled = false;
}
else
{
connectButton.IsEnabled = true;
}
}
}
}