Winify

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 11  →  ?path2? @ 12
/trunk/Winify/Gotify/GotifyConnection.cs
@@ -2,7 +2,6 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.WebSockets;
@@ -63,34 +62,27 @@
 
#region Public Methods
 
public void Start(string username, string password, string host, string port)
public void Start(string username, string password, string url)
{
if (!Uri.TryCreate($"ws://{host}:{port}/stream", UriKind.RelativeOrAbsolute, out var webSocketsUri))
if (!Uri.TryCreate(url, UriKind.Absolute, out var httpUri))
{
return;
}
 
if (!Uri.TryCreate($"http://{host}:{port}/", UriKind.RelativeOrAbsolute, out var httpUri))
{
return;
}
// Build the web sockets URI.
var webSocketsUriBuilder = new UriBuilder(httpUri);
webSocketsUriBuilder.Scheme = "ws";
webSocketsUriBuilder.Path = $"{webSocketsUriBuilder.Path}/stream";
var webSocketsUri = webSocketsUriBuilder.Uri;
 
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
 
var httpClientHandler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
 
_httpClient = new HttpClient(httpClientHandler)
{
BaseAddress = httpUri
};
_httpClient = new HttpClient();
var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);
 
_runTask = Run(webSocketsUri, username, password, _cancellationToken);
_runTask = Run(webSocketsUri, httpUri, username, password, _cancellationToken);
}
 
public void Stop()
@@ -105,7 +97,8 @@
 
#region Private Methods
 
private async Task Run(Uri uri, string username, string password, CancellationToken cancellationToken)
private async Task Run(Uri webSocketsUri, Uri httpUri, string username, string password,
CancellationToken cancellationToken)
{
try
{
@@ -117,7 +110,7 @@
var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
_webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}");
 
await _webSocketClient.ConnectAsync(uri, cancellationToken);
await _webSocketClient.ConnectAsync(webSocketsUri, cancellationToken);
 
do
{
@@ -138,8 +131,13 @@
continue;
}
 
var applications = await _httpClient.GetStringAsync("application");
if (!Uri.TryCreate($"{httpUri}/application", UriKind.Absolute, out var applicationUri))
{
continue;
}
 
var applications = await _httpClient.GetStringAsync(applicationUri);
 
var gotifyApplications = JsonConvert.DeserializeObject<GotifyApplication[]>(applications);
if (gotifyApplications == null)
{
@@ -153,8 +151,14 @@
continue;
}
 
var imageBytes = await _httpClient.GetByteArrayAsync(application.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;
@@ -164,11 +168,8 @@
{
var image = Image.FromStream(memoryStream);
 
if (GotifyNotification != null)
{
GotifyNotification.Invoke(this,
new GotifyNotificationEventArgs(gotifyNotification, image));
}
GotifyNotification?.Invoke(this,
new GotifyNotificationEventArgs(gotifyNotification, image));
}
 
 
@@ -181,8 +182,10 @@
await _webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty,
CancellationToken.None);
}
catch (WebSocketException)
catch (Exception ex) when (ex is WebSocketException || ex is HttpRequestException)
{
Debug.WriteLine($"Unable to connect to gotify server: {ex.Message}");
 
// Reconnect
if (_webSocketClient != null)
{