-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
184 lines (159 loc) · 4.98 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
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
using System;
using System.Collections.Generic;
using System.Linq;
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;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
namespace signalr;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
HubConnection? connection = null;
public MainWindow()
{
InitializeComponent();
}
public void Log(string message)
{
if (string.IsNullOrEmpty(message))
return;
if (!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(() => Log(message));
return;
}
debuggingText.Text += message + "\r\n";
}
private Task ConnectionOnClosed(Exception exception)
{
Log($"ConnectionOnClosed: {exception?.Message}");
UpdateButtons();
return Task.CompletedTask;
}
private void UpdateButtons()
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(() => UpdateButtons());
return;
}
if (connection == null || connection.State == HubConnectionState.Disconnected)
{
connectButton.IsEnabled = true;
connectButton.Content = "Connect";
//buttonSend.IsEnabled = false;
}
else if (connection.State == HubConnectionState.Connected)
{
connectButton.IsEnabled = true;
connectButton.Content = "Disconnect";
//buttonSend.IsEnabled = true;
}
else if (connection.State == HubConnectionState.Connecting || connection.State == HubConnectionState.Reconnecting)
{
connectButton.IsEnabled = false;
connectButton.Content = "Connecting...";
//buttonSend.IsEnabled = false;
}
}
public async void connectButtonClicked(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(textBoxServerUrl.Text))
{
Log("No Server URL!");
return;
}
try
{
IsEnabled = false;
connection = new HubConnectionBuilder()
.ConfigureLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Information);
logging.AddProvider(new LoggingProvider(this));
})
.WithUrl($"{textBoxServerUrl.Text}/chatHub", options =>
//https://www.piesocket.com/websocket-tester
//.WithUrl("wss://demo.websocket.me/v3/channel_1?api_key=oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm¬ify_self", options =>
{
// We only support websockets - and non sticky sessions
options.Transports = HttpTransportType.WebSockets;
options.SkipNegotiation = true;
options.DefaultTransferFormat = TransferFormat.Text;
//options.AccessTokenProvider = AccessTokenProvider;
//var proxy = new WebProxy("http://localhost:8888", false);
//proxy.UseDefaultCredentials = false;
//options.Proxy = proxy;
})
.Build();
connection.HandshakeTimeout = TimeSpan.FromSeconds(10);
connection.Closed += ConnectionOnClosed;
connection.On<string, string>("ReceiveMessage", (user, message) =>
{
Log($"Rx => {user}: {message}");
});
Log("Attempting to connect...");
await connection.StartAsync();
Log("Connected");
}
catch (Exception ex)
{
Log("Error Connecting: " + ex.Message);
}
finally
{
UpdateButtons();
IsEnabled = true;
}
}
}
public class LoggingProvider : ILoggerProvider
{
private MainWindow form;
public LoggingProvider(MainWindow form)
{
this.form = form;
}
public void Dispose()
{
}
public ILogger CreateLogger(string categoryName)
{
return new TraceLogger(form);
}
}
public class TraceLogger : ILogger
{
private MainWindow form;
public TraceLogger(MainWindow form)
{
this.form = form;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var message = $"{logLevel} {state} {exception?.Message}";
form.Log(message);
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public IDisposable BeginScope<TState>(TState state)
{
return new LoggingProvider(form);
}
}