-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (63 loc) · 2.34 KB
/
Copy pathProgram.cs
File metadata and controls
67 lines (63 loc) · 2.34 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
using System;
using System.Net.WebSockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace ChannelWSClient
{
public class Program
{
public const int KEYSTROKE_TRANSMIT_INTERVAL_MS = 100;
public const int CLOSE_SOCKET_TIMEOUT_MS = 10000;
// async Main requires C# 7.2 or newer in csproj properties
static async Task Main(string[] args)
{
bool running = true;
while (running)
{
Console.Clear();
await MainThreadUILoop();
Console.WriteLine("\nPress R to re-connect or any other key to exit.");
var key = Console.ReadKey(intercept: true);
running = (key.Key == ConsoleKey.R);
}
}
static async Task MainThreadUILoop()
{
try
{
await WebSocketClient.StartAsync(@"ws://localhost:8080/");
Console.WriteLine("Press ESC to exit. Other keystrokes are sent to the echo server.\n\n");
bool running = true;
while (running && WebSocketClient.State == WebSocketState.Open)
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey(intercept: true);
if (key.Key == ConsoleKey.Escape)
{
running = false;
}
else
{
WebSocketClient.QueueKeystroke(key.KeyChar.ToString());
}
}
}
await WebSocketClient.StopAsync();
}
catch (OperationCanceledException)
{
// normal upon task/token cancellation, disregard
}
catch (Exception ex)
{
ReportException(ex);
}
}
public static void ReportException(Exception ex, [CallerMemberName] string location = "(Caller name not set)")
{
Console.WriteLine($"\n{location}:\n Exception {ex.GetType().Name}: {ex.Message}");
if (ex.InnerException != null) Console.WriteLine($" Inner Exception {ex.InnerException.GetType().Name}: {ex.InnerException.Message}");
}
}
}