Winify – Diff between revs 3 and 12

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 3 Rev 12
Line 1... Line 1...
1 using System; 1 using System;
2 using System.Diagnostics; 2 using System.Diagnostics;
3 using System.Drawing; 3 using System.Drawing;
4 using System.IO; 4 using System.IO;
5 using System.Net; -  
6 using System.Net.Http; 5 using System.Net.Http;
7 using System.Net.Http.Headers; 6 using System.Net.Http.Headers;
8 using System.Net.WebSockets; 7 using System.Net.WebSockets;
9 using System.Text; 8 using System.Text;
10 using System.Threading; 9 using System.Threading;
Line 61... Line 60...
61   60  
Line 62... Line 61...
62 #endregion 61 #endregion
Line 63... Line 62...
63   62  
64 #region Public Methods 63 #region Public Methods
65   64  
66 public void Start(string username, string password, string host, string port) 65 public void Start(string username, string password, string url)
67 { 66 {
68 if (!Uri.TryCreate($"ws://{host}:{port}/stream", UriKind.RelativeOrAbsolute, out var webSocketsUri)) 67 if (!Uri.TryCreate(url, UriKind.Absolute, out var httpUri))
Line -... Line 68...
-   68 {
69 { 69 return;
70 return; 70 }
71 } 71  
72   72 // Build the web sockets URI.
Line 73... Line 73...
73 if (!Uri.TryCreate($"http://{host}:{port}/", UriKind.RelativeOrAbsolute, out var httpUri)) 73 var webSocketsUriBuilder = new UriBuilder(httpUri);
74 { 74 webSocketsUriBuilder.Scheme = "ws";
Line 75... Line -...
75 return; -  
76 } -  
77   -  
78 _cancellationTokenSource = new CancellationTokenSource(); -  
79 _cancellationToken = _cancellationTokenSource.Token; -  
80   75 webSocketsUriBuilder.Path = $"{webSocketsUriBuilder.Path}/stream";
81 var httpClientHandler = new HttpClientHandler -  
82 { -  
83 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate -  
84 }; 76 var webSocketsUri = webSocketsUriBuilder.Uri;
85   77  
Line 86... Line 78...
86 _httpClient = new HttpClient(httpClientHandler) 78 _cancellationTokenSource = new CancellationTokenSource();
87 { 79 _cancellationToken = _cancellationTokenSource.Token;
Line 88... Line 80...
88 BaseAddress = httpUri 80  
89 }; 81 _httpClient = new HttpClient();
90 var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); 82 var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
Line 103... Line 95...
103   95  
Line 104... Line 96...
104 #endregion 96 #endregion
Line 105... Line 97...
105   97  
-   98 #region Private Methods
106 #region Private Methods 99  
107   100 private async Task Run(Uri webSocketsUri, Uri httpUri, string username, string password,
108 private async Task Run(Uri uri, string username, string password, CancellationToken cancellationToken) 101 CancellationToken cancellationToken)
109 { 102 {
110 try 103 try
Line 115... Line 108...
115 { 108 {
116 _webSocketClient = new ClientWebSocket(); 109 _webSocketClient = new ClientWebSocket();
117 var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); 110 var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
118 _webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}"); 111 _webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}");
Line 119... Line 112...
119   112  
Line 120... Line 113...
120 await _webSocketClient.ConnectAsync(uri, cancellationToken); 113 await _webSocketClient.ConnectAsync(webSocketsUri, cancellationToken);
121   114  
122 do 115 do
Line 136... Line 129...
136 if (gotifyNotification == null) 129 if (gotifyNotification == null)
137 { 130 {
138 continue; 131 continue;
139 } 132 }
Line -... Line 133...
-   133  
-   134 if (!Uri.TryCreate($"{httpUri}/application", UriKind.Absolute, out var applicationUri))
-   135 {
-   136 continue;
-   137 }
140   138  
Line 141... Line 139...
141 var applications = await _httpClient.GetStringAsync("application"); 139 var applications = await _httpClient.GetStringAsync(applicationUri);
142   140  
143 var gotifyApplications = JsonConvert.DeserializeObject<GotifyApplication[]>(applications); 141 var gotifyApplications = JsonConvert.DeserializeObject<GotifyApplication[]>(applications);
144 if (gotifyApplications == null) 142 if (gotifyApplications == null)
Line 151... Line 149...
151 if (application.Id != gotifyNotification.AppId) 149 if (application.Id != gotifyNotification.AppId)
152 { 150 {
153 continue; 151 continue;
154 } 152 }
Line -... Line 153...
-   153  
-   154 if (!Uri.TryCreate($"{httpUri}/{application.Image}", UriKind.Absolute,
-   155 out var applicationImageUri))
-   156 {
-   157 continue;
-   158 }
155   159  
Line 156... Line 160...
156 var imageBytes = await _httpClient.GetByteArrayAsync(application.Image); 160 var imageBytes = await _httpClient.GetByteArrayAsync(applicationImageUri);
157   161  
158 if (imageBytes == null || imageBytes.Length == 0) 162 if (imageBytes == null || imageBytes.Length == 0)
159 { 163 {
Line 160... Line 164...
160 continue; 164 continue;
161 } 165 }
162   166  
Line 163... Line -...
163 using (var memoryStream = new MemoryStream(imageBytes)) -  
164 { -  
165 var image = Image.FromStream(memoryStream); 167 using (var memoryStream = new MemoryStream(imageBytes))
166   168 {
167 if (GotifyNotification != null) -  
168 { 169 var image = Image.FromStream(memoryStream);
Line 169... Line 170...
169 GotifyNotification.Invoke(this, 170  
170 new GotifyNotificationEventArgs(gotifyNotification, image)); 171 GotifyNotification?.Invoke(this,
Line 179... Line 180...
179 } while (!cancellationToken.IsCancellationRequested); 180 } while (!cancellationToken.IsCancellationRequested);
Line 180... Line 181...
180   181  
181 await _webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, 182 await _webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty,
182 CancellationToken.None); 183 CancellationToken.None);
183 } 184 }
184 catch (WebSocketException) 185 catch (Exception ex) when (ex is WebSocketException || ex is HttpRequestException)
-   186 {
-   187 Debug.WriteLine($"Unable to connect to gotify server: {ex.Message}");
185 { 188  
186 // Reconnect 189 // Reconnect
187 if (_webSocketClient != null) 190 if (_webSocketClient != null)
188 { 191 {
189 _webSocketClient.Abort(); 192 _webSocketClient.Abort();