/trunk/Winify/Gotify/GotifyConnection.cs |
@@ -32,13 +32,39 @@ |
|
private Task _runTask; |
|
private HttpClient _httpClient; |
|
private readonly string _auth; |
|
private readonly Uri _webSocketsUri; |
|
private readonly Uri _httpUri; |
|
#endregion |
|
#region Constructors, Destructors and Finalizers |
|
public GotifyConnection(Server server) |
public GotifyConnection() |
{ |
_httpClient = new HttpClient(); |
} |
|
public GotifyConnection(Server server) : this() |
{ |
_server = server; |
|
_auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{_server.Username}:{_server.Password}")); |
_httpClient.DefaultRequestHeaders.Authorization = |
new AuthenticationHeaderValue("Basic", _auth); |
|
if (Uri.TryCreate(_server.Url, UriKind.Absolute, out _httpUri)) |
{ |
// Build the web sockets URI. |
var webSocketsUriBuilder = new UriBuilder(_httpUri); |
webSocketsUriBuilder.Scheme = "ws"; |
webSocketsUriBuilder.Path = Path.Combine(webSocketsUriBuilder.Path, "stream"); |
_webSocketsUri = webSocketsUriBuilder.Uri; |
} |
} |
|
public void Dispose() |
@@ -48,6 +74,9 @@ |
_cancellationTokenSource.Dispose(); |
_cancellationTokenSource = null; |
} |
|
_httpClient.Dispose(); |
_httpClient = null; |
} |
|
#endregion |
@@ -56,23 +85,18 @@ |
|
public void Start() |
{ |
if (!Uri.TryCreate(_server.Url, UriKind.Absolute, out var httpUri)) return; |
|
// Build the web sockets URI. |
var webSocketsUriBuilder = new UriBuilder(httpUri); |
webSocketsUriBuilder.Scheme = "ws"; |
webSocketsUriBuilder.Path = Path.Combine($"{webSocketsUriBuilder.Path}", "stream"); |
var webSocketsUri = webSocketsUriBuilder.Uri; |
|
_cancellationTokenSource = new CancellationTokenSource(); |
_cancellationToken = _cancellationTokenSource.Token; |
|
_runTask = Run(webSocketsUri, httpUri, _server.Username, _server.Password, _cancellationToken); |
_runTask = Run(_cancellationToken); |
} |
|
public void Stop() |
{ |
if (_cancellationTokenSource != null) _cancellationTokenSource.Cancel(); |
if (_cancellationTokenSource != null) |
{ |
_cancellationTokenSource.Cancel(); |
} |
} |
|
#endregion |
@@ -79,8 +103,7 @@ |
|
#region Private Methods |
|
private async Task Run(Uri webSocketsUri, Uri httpUri, string username, string password, |
CancellationToken cancellationToken) |
private async Task Run(CancellationToken cancellationToken) |
{ |
try |
{ |
@@ -90,12 +113,10 @@ |
{ |
using (var webSocketClient = new ClientWebSocket()) |
{ |
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]); |
@@ -102,9 +123,15 @@ |
|
var result = await webSocketClient.ReceiveAsync(payload, cancellationToken); |
|
if (result.Count == 0) continue; |
if (result.Count == 0) |
{ |
continue; |
} |
|
if (payload.Array == null || payload.Count == 0) continue; |
if (payload.Array == null || payload.Count == 0) |
{ |
continue; |
} |
|
var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count); |
|
@@ -119,12 +146,14 @@ |
|
gotifyNotification.Server = _server; |
|
if (!Uri.TryCreate(Path.Combine($"{httpUri}", "application"), UriKind.Absolute, |
if (!Uri.TryCreate(Path.Combine($"{_httpUri}", "application"), UriKind.Absolute, |
out var applicationUri)) |
{ |
continue; |
} |
|
var image = await RetrieveGotifyApplicationImage(gotifyNotification.AppId, httpUri, |
applicationUri, auth, cancellationToken); |
var image = await RetrieveGotifyApplicationImage(gotifyNotification.AppId, |
applicationUri, cancellationToken); |
|
GotifyNotification?.Invoke(this, |
new GotifyNotificationEventArgs(gotifyNotification, image)); |
@@ -151,40 +180,41 @@ |
} |
} |
|
private static async Task<Image> RetrieveGotifyApplicationImage(int appId, Uri httpUri, Uri applicationUri, |
string auth, |
private async Task<Image> RetrieveGotifyApplicationImage(int appId, Uri applicationUri, |
CancellationToken cancellationToken) |
{ |
using (var httpClient = new HttpClient()) |
{ |
httpClient.DefaultRequestHeaders.Authorization = |
new AuthenticationHeaderValue("Basic", auth); |
var applicationResponse = await _httpClient.GetAsync(applicationUri, cancellationToken); |
|
var applicationResponse = await httpClient.GetAsync(applicationUri, cancellationToken); |
var applications = await applicationResponse.Content.ReadAsStringAsync(); |
|
var applications = await applicationResponse.Content.ReadAsStringAsync(); |
var gotifyApplications = |
JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
|
var gotifyApplications = |
JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
if (gotifyApplications == null) |
{ |
return null; |
} |
|
if (gotifyApplications == null) return null; |
foreach (var application in gotifyApplications) |
{ |
if (application.Id != appId) |
{ |
continue; |
} |
|
foreach (var application in gotifyApplications) |
if (!Uri.TryCreate(Path.Combine($"{_httpUri}", $"{application.Image}"), UriKind.Absolute, |
out var applicationImageUri)) |
{ |
if (application.Id != appId) continue; |
continue; |
} |
|
if (!Uri.TryCreate(Path.Combine($"{httpUri}", $"{application.Image}"), UriKind.Absolute, |
out var applicationImageUri)) |
continue; |
var imageResponse = await _httpClient.GetAsync(applicationImageUri, cancellationToken); |
|
var imageResponse = await httpClient.GetAsync(applicationImageUri, cancellationToken); |
using (var memoryStream = new MemoryStream()) |
{ |
await imageResponse.Content.CopyToAsync(memoryStream); |
|
using (var memoryStream = new MemoryStream()) |
{ |
await imageResponse.Content.CopyToAsync(memoryStream); |
|
return Image.FromStream(memoryStream); |
} |
return Image.FromStream(memoryStream); |
} |
} |
|