Commit 2e5b5656 authored by Robert Rudman's avatar Robert Rudman

Add more logging around websockets

parent 6d3c6568
Pipeline #45061285 passed with stages
in 5 minutes and 7 seconds
......@@ -30,8 +30,8 @@
<ng-container *ngIf="tag.userGrandTotals.length">
<h2>User grand totals</h2>
<input type="checkbox" id="userGrandTotalShowAll" [(ngModel)]="userGrandTotalShowAll" [ngModelOptions]="{standalone: true}" >
<label for="userGrandTotalShowAll">Show all</label>
<input type="checkbox" id="{{tag.tag}}-userGrandTotalShowAll" [(ngModel)]="tag.userGrandTotalShowAll" [ngModelOptions]="{standalone: true}" >
<label for="{{tag.tag}}-userGrandTotalShowAll">Show all</label>
<table class="table">
<thead>
<tr>
......@@ -41,7 +41,7 @@
</thead>
<tbody>
<tr *ngFor="let row of tag.userGrandTotals | sortAscending: 'total'; let i=index">
<ng-container *ngIf="userGrandTotalShowAll || i < 10">
<ng-container *ngIf="tag.userGrandTotalShowAll || i < 10">
<td><a href="https://stackoverflow.com/users/{{row.userId}}">{{row.userName}}</a></td>
<td>{{row.total}}</td>
</ng-container>
......@@ -53,8 +53,8 @@
<ng-container *ngIf="tag.userTotals.length">
<h2>User total breakdown</h2>
<input type="checkbox" id="userBreakdownShowAll" [(ngModel)]="userBreakdownShowAll" [ngModelOptions]="{standalone: true}" >
<label for="userBreakdownShowAll">Show all</label>
<input type="checkbox" id="{{tag.tag}}-userBreakdownShowAll" [(ngModel)]="tag.userBreakdownShowAll" [ngModelOptions]="{standalone: true}" >
<label for="{{tag.tag}}-userBreakdownShowAll">Show all</label>
<table class="table">
<thead>
<tr>
......@@ -65,7 +65,7 @@
</thead>
<tbody>
<tr *ngFor="let row of tag.userTotals | sortAscending: 'total'; let i=index">
<ng-container *ngIf="userBreakdownShowAll || i < 10">
<ng-container *ngIf="tag.userBreakdownShowAll || i < 10">
<td><a href="https://stackoverflow.com/users/{{row.userId}}">{{row.userName}}</a></td>
<td>{{row.type}}</td>
<td>{{row.total}}</td>
......
......@@ -74,7 +74,7 @@ namespace Rodgort.Services.HostedServices
var websocket = Observable.Create<int>(async observer =>
{
var webSocket = new PlainWebSocket(wsEndpoint, new Dictionary<string, string> {{"Origin", homePage}});
var webSocket = new PlainWebSocket(wsEndpoint, new Dictionary<string, string> {{"Origin", homePage}}, _serviceProvider.GetRequiredService<ILogger<PlainWebSocket>>());
webSocket.OnTextMessage += async message =>
{
try
......
......@@ -5,6 +5,7 @@ using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
......@@ -19,12 +20,16 @@ namespace StackExchangeChat
public const int MAX_MESSAGE_LENGTH = 500;
private readonly SiteAuthenticator _siteAuthenticator;
private readonly IServiceProvider _serviceProvider;
private readonly HttpClientWithHandler _httpClient;
private readonly ILogger<ChatClient> _logger;
public ChatClient(SiteAuthenticator siteAuthenticator, HttpClientWithHandler httpClient, ILogger<ChatClient> logger)
public ChatClient(SiteAuthenticator siteAuthenticator,
IServiceProvider serviceProvider,
HttpClientWithHandler httpClient, ILogger<ChatClient> logger)
{
_siteAuthenticator = siteAuthenticator;
_serviceProvider = serviceProvider;
_httpClient = httpClient;
_logger = logger;
}
......@@ -205,7 +210,7 @@ namespace StackExchangeChat
var lastEventTime = JsonConvert.DeserializeObject<JObject>(await eventsRequest.Content.ReadAsStringAsync())["time"].Value<string>();
var webSocket = new PlainWebSocket($"{wsAuthUrl}?l={lastEventTime}", new Dictionary<string, string> { { "Origin", $"https://{chatSite.ChatDomain}"} });
var webSocket = new PlainWebSocket($"{wsAuthUrl}?l={lastEventTime}", new Dictionary<string, string> { { "Origin", $"https://{chatSite.ChatDomain}"} }, _serviceProvider.GetRequiredService<ILogger<PlainWebSocket>>());
webSocket.OnTextMessage += (message) =>
{
var dataObject = JsonConvert.DeserializeObject<JObject>(message);
......
......@@ -4,12 +4,14 @@ using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace StackExchangeChat
{
// https://github.com/SOBotics/SharpExchange/blob/master/SharpExchange/Net/WebSocket/DefaultWebSocket.cs
public class PlainWebSocket : IDisposable
{
private readonly ILogger<PlainWebSocket> _logger;
private const int BufferSize = 4 * 1024;
private ClientWebSocket _socket;
private readonly CancellationTokenSource _socketTokenSource;
......@@ -26,8 +28,11 @@ namespace StackExchangeChat
public IReadOnlyDictionary<string, string> Headers { get; }
public bool AutoReconnect { get; set; } = true;
public PlainWebSocket(string endpoint, IReadOnlyDictionary<string, string> headers = null)
public PlainWebSocket(string endpoint,
IReadOnlyDictionary<string, string> headers,
ILogger<PlainWebSocket> logger)
{
_logger = logger;
ThrowIfNullOrEmpty(endpoint, nameof(endpoint));
_socketTokenSource = new CancellationTokenSource();
......@@ -45,6 +50,8 @@ namespace StackExchangeChat
if (_socket?.State == WebSocketState.Open)
_socketTokenSource.Cancel();
_logger.LogWarning("Disposing socket");
_socket?.Dispose();
}
......@@ -119,6 +126,8 @@ namespace StackExchangeChat
when (ex.InnerException?.GetType() == typeof(TaskCanceledException))
{
InvokeAsync(OnClose);
_logger.LogError("Websocket crashed", ex);
return;
}
......@@ -126,6 +135,8 @@ namespace StackExchangeChat
{
OnError?.Invoke(e1);
_logger.LogError("Websocket crashed. AutoReconnect: " + AutoReconnect, e1);
if (!AutoReconnect) return;
try
......@@ -139,6 +150,8 @@ namespace StackExchangeChat
InvokeAsync(OnReconnectFailed);
InvokeAsync(OnError, e2);
InvokeAsync(OnClose);
_logger.LogError("Websocket crashed on reconnect", e2);
}
return;
......@@ -177,6 +190,8 @@ namespace StackExchangeChat
catch (Exception ex)
{
OnError?.Invoke(ex);
_logger.LogError("Failed to process message", ex);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment