/trunk/Winify/Gotify/GotifyConnection.cs |
@@ -108,83 +108,92 @@ |
{ |
try |
{ |
_webSocketClient = new ClientWebSocket(); |
using (_webSocketClient = new ClientWebSocket()) |
{ |
var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); |
|
var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); |
_webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}"); |
|
_webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}"); |
await _webSocketClient.ConnectAsync(webSocketsUri, cancellationToken); |
|
await _webSocketClient.ConnectAsync(webSocketsUri, cancellationToken); |
|
do |
{ |
var payload = new ArraySegment<byte>(new byte[1024]); |
|
await _webSocketClient.ReceiveAsync(payload, cancellationToken); |
|
if (payload.Array == null || payload.Count == 0) |
do |
{ |
continue; |
} |
var payload = new ArraySegment<byte>(new byte[1024]); |
|
var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count); |
var result = await _webSocketClient.ReceiveAsync(payload, cancellationToken); |
|
var gotifyNotification = JsonConvert.DeserializeObject<GotifyNotification>(message); |
if (gotifyNotification == null) |
{ |
continue; |
} |
if (result.Count == 0) |
{ |
continue; |
} |
|
if (!Uri.TryCreate($"{httpUri}/application", UriKind.Absolute, out var applicationUri)) |
{ |
continue; |
} |
if (payload.Array == null || payload.Count == 0) |
{ |
continue; |
} |
|
var applications = await _httpClient.GetStringAsync(applicationUri); |
var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count); |
|
var gotifyApplications = JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
if (gotifyApplications == null) |
{ |
continue; |
} |
|
foreach (var application in gotifyApplications) |
{ |
if (application.Id != gotifyNotification.AppId) |
var gotifyNotification = JsonConvert.DeserializeObject<GotifyNotification>(message); |
if (gotifyNotification == null) |
{ |
continue; |
} |
|
if (!Uri.TryCreate($"{httpUri}/{application.Image}", UriKind.Absolute, |
out var applicationImageUri)) |
if (!Uri.TryCreate($"{httpUri}/application", UriKind.Absolute, out var applicationUri)) |
{ |
continue; |
} |
|
var imageBytes = await _httpClient.GetByteArrayAsync(applicationImageUri); |
var applications = await _httpClient.GetStringAsync(applicationUri); |
|
if (imageBytes == null || imageBytes.Length == 0) |
var gotifyApplications = |
JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
if (gotifyApplications == null) |
{ |
continue; |
} |
|
using (var memoryStream = new MemoryStream(imageBytes)) |
foreach (var application in gotifyApplications) |
{ |
var image = Image.FromStream(memoryStream); |
if (application.Id != gotifyNotification.AppId) |
{ |
continue; |
} |
|
GotifyNotification?.Invoke(this, |
new GotifyNotificationEventArgs(gotifyNotification, image)); |
if (!Uri.TryCreate($"{httpUri}/{application.Image}", UriKind.Absolute, |
out var applicationImageUri)) |
{ |
continue; |
} |
|
var imageBytes = await _httpClient.GetByteArrayAsync(applicationImageUri); |
|
if (imageBytes == null || imageBytes.Length == 0) |
{ |
continue; |
} |
|
using (var memoryStream = new MemoryStream(imageBytes)) |
{ |
var image = Image.FromStream(memoryStream); |
|
GotifyNotification?.Invoke(this, |
new GotifyNotificationEventArgs(gotifyNotification, image)); |
} |
|
|
break; |
} |
|
Log.Debug($"Notification message received: {gotifyNotification.Message}"); |
} while (!cancellationToken.IsCancellationRequested); |
|
break; |
} |
await _webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, |
CancellationToken.None); |
} |
|
Log.Debug($"Notification message received: {gotifyNotification.Message}"); |
} while (!cancellationToken.IsCancellationRequested); |
|
await _webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, |
CancellationToken.None); |
_webSocketClient = null; |
} |
catch (Exception ex) when (ex is WebSocketException || ex is HttpRequestException) |
{ |