/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) |
{ |
/trunk/Winify/Gotify/GotifyConnectionManager.cs |
@@ -1,6 +1,7 @@ |
using System; |
using System.Collections.Concurrent; |
using System.Collections.Specialized; |
using System.ComponentModel; |
using System.Linq; |
using Servers; |
|
@@ -34,11 +35,13 @@ |
_servers = servers; |
|
_servers.Server.CollectionChanged += Server_CollectionChanged; |
_servers.Server.ListChanged += Server_ListChanged; |
} |
|
public void Dispose() |
{ |
_servers.Server.CollectionChanged -= Server_CollectionChanged; |
_servers.Server.ListChanged -= Server_ListChanged; |
} |
|
#endregion |
@@ -45,6 +48,21 @@ |
|
#region Event Handlers |
|
private void Server_ListChanged(object sender, ListChangedEventArgs e) |
{ |
if (e.ListChangedType != ListChangedType.ItemChanged) |
{ |
return; |
} |
|
var server = _servers.Server[e.NewIndex]; |
|
if (RemoveConnection(server)) |
{ |
CreateConnection(server); |
} |
} |
|
private void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
{ |
if (e.NewItems != null) |
@@ -51,11 +69,7 @@ |
{ |
foreach (var server in e.NewItems.OfType<Server>()) |
{ |
var connection = new GotifyConnection(); |
connection.GotifyNotification += Connection_GotifyNotification; |
|
connection.Start(server.Username, server.Password, server.Url); |
_gotifyConnections.TryAdd(server.Name, connection); |
CreateConnection(server); |
} |
} |
|
@@ -63,15 +77,7 @@ |
{ |
foreach (var server in e.OldItems.OfType<Server>()) |
{ |
if (!_gotifyConnections.TryRemove(server.Name, out var gotifyConnection)) |
{ |
continue; |
} |
|
gotifyConnection.GotifyNotification -= Connection_GotifyNotification; |
|
gotifyConnection.Stop(); |
gotifyConnection.Dispose(); |
RemoveConnection(server); |
} |
} |
} |
@@ -82,5 +88,34 @@ |
} |
|
#endregion |
|
#region Private Methods |
|
private bool RemoveConnection(Server server) |
{ |
if (!_gotifyConnections.TryRemove(server.Url, out var gotifyConnection)) |
{ |
return false; |
} |
|
gotifyConnection.GotifyNotification -= Connection_GotifyNotification; |
|
gotifyConnection.Stop(); |
gotifyConnection.Dispose(); |
gotifyConnection = null; |
|
return true; |
} |
|
private void CreateConnection(Server server) |
{ |
var connection = new GotifyConnection(); |
connection.GotifyNotification += Connection_GotifyNotification; |
|
connection.Start(server.Username, server.Password, server.Url); |
_gotifyConnections.TryAdd(server.Url, connection); |
} |
|
#endregion |
} |
} |