Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
v0.3.0 nicer layout plus signalr events push
Browse files Browse the repository at this point in the history
  • Loading branch information
fearlessfrog committed Jun 4, 2024
1 parent abd43d4 commit 45667f6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
4 changes: 4 additions & 0 deletions AtcLogWatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<DebugType>none</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
<None Update="filters.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
10 changes: 7 additions & 3 deletions FileWatcherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Timers;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.SignalR;

public class FileWatcherService
{
private readonly IHubContext<MyHub> _hubContext;
private string _filePath;
private string _lastLineFilePath;
private List<string> _filters;
Expand All @@ -15,8 +17,9 @@ public class FileWatcherService
private string _lastWrittenLine = null;
private bool _debug = false; // Add a debug flag

public FileWatcherService(string filePath, string lastLineFilePath, bool debug = false)
public FileWatcherService(IHubContext<MyHub> hubContext, string filePath, string lastLineFilePath, bool debug = false)
{
_hubContext = hubContext;
_filePath = filePath;
_lastLineFilePath = lastLineFilePath;
_debug = debug;
Expand Down Expand Up @@ -51,9 +54,9 @@ private async void OnTimedEvent(Object source, ElapsedEventArgs e)
var fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
reader = new StreamReader(fileStream);
}
catch (IOException)
catch (IOException ex)
{
LogDebug("File is being used by another process. Retrying...");
LogDebug("File is being used by another process. Retrying..." + ex.Message);
await Task.Delay(1000); // Wait and retry
}
}
Expand All @@ -77,6 +80,7 @@ private async void OnTimedEvent(Object source, ElapsedEventArgs e)
if (_previousLine != _lastWrittenLine)
{
await File.WriteAllTextAsync(_lastLineFilePath, _previousLine);
await _hubContext.Clients.All.SendAsync("ReceiveUpdate", _previousLine);
LogDebug($"Last line output: {_previousLine}");
_lastWrittenLine = _previousLine;
}
Expand Down
10 changes: 10 additions & 0 deletions MyHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;

public class MyHub : Hub
{
public async Task SendUpdate(string update)
{
await Clients.All.SendAsync("ReceiveUpdate", update);
}
}
32 changes: 29 additions & 3 deletions Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
color: #fff;
margin: 0;
padding: 20px;
overflow-x: hidden;
}
h1 {
Expand All @@ -29,13 +30,13 @@
margin-top: 20px;
list-style-type: none;
padding: 0;
height: 900px;
height: 85vh;
overflow-y: scroll;
background-color: #000033;
}
#messagesList li {
background-color: #800080;
background-color: #420042;
color: #fff;
padding: 10px 20px;
margin: 10px 0;
Expand All @@ -47,6 +48,7 @@
background-color: #505050 !important;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/3.1.7/signalr.min.js"></script>
</head>

<body>
Expand All @@ -58,8 +60,32 @@
</div>

<script type="text/javascript">
const connection = new signalR.HubConnectionBuilder()
.withUrl("/hub")
.build();
let lastMessage = '';
connection.on("ReceiveUpdate", function (update) {
const message = update;
if (message && message !== lastMessage) {
const li = document.createElement("li");
if (message.startsWith('> ')) {
li.classList.add('dark-background');
}
li.textContent = message;
document.getElementById("messagesList").appendChild(li);
// Scroll to the bottom
messagesList.scrollTop = messagesList.scrollHeight;
// Keep this so we don't repeat messages
lastMessage = message;
}
});
connection.start().catch(function (err) {
return console.error(err.toString());
});
document.getElementById("clearButton").addEventListener("click", function () {
document.getElementById("messagesList").innerHTML = '';
});
Expand Down Expand Up @@ -119,7 +145,7 @@
window.onload = async function () {
await fetchInitialMessage();
setInterval(fetchLatestMessage, 1000); // Poll every second
//setInterval(fetchLatestMessage, 1000); // Deprecated: Poll every second
};
</script>
</body>
Expand Down
4 changes: 2 additions & 2 deletions Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
</div>
</nav>
</header>
<div class="container">
<div class="container" style='max-width:100%;'>
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>

<footer class="border-top footer text-muted">
<div class="container">
fearlessfrog's ATC Log Viewer v0.2.0
fearlessfrog's ATC Log Viewer v0.3.0
</div>
</footer>

Expand Down
8 changes: 6 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.IO;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.FileProviders;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddSignalR();
builder.Services.AddSingleton<FileWatcherService>(provider =>
{
var userProfilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var defaultFilePath = Path.Combine(userProfilePath, "AppData", "LocalLow", "Skirmish Mode Games, Inc", "BeyondATC", "Player.log");
var logFileLocationPath = Path.Combine(userProfilePath, "logfile_location.txt");
var filePath = File.Exists(logFileLocationPath) ? File.ReadAllText(logFileLocationPath) : defaultFilePath;
var lastLineFilePath = Path.Combine(Path.GetTempPath(), "lastline.txt");
return new FileWatcherService(filePath, lastLineFilePath, true);
var hubContext = provider.GetRequiredService<IHubContext<MyHub>>();
return new FileWatcherService(hubContext, filePath, lastLineFilePath, true);
});

var fileProvider = new ManifestEmbeddedFileProvider(Assembly.GetAssembly(type: typeof(Program))!, "wwwroot");
Expand All @@ -39,6 +42,7 @@
app.UseRouting();

app.MapRazorPages();
app.MapHub<MyHub>("/hub");

app.MapGet("/lastline", async context =>
{
Expand All @@ -58,7 +62,7 @@
var fileWatcherService = app.Services.GetRequiredService<FileWatcherService>();

Console.WriteLine("------------------------------------------------------------------");
Console.WriteLine("Welcome to fearlessfrog's AtcLogWatcher!");
Console.WriteLine("Welcome to fearlessfrog's AtcLogWatcher! v0.3.0");
Console.WriteLine("Listening on: http://localhost:41716");
Console.WriteLine("See filters.txt for the list of filters to exclude.");
Console.WriteLine("Press Ctrl+C to exit.");
Expand Down

0 comments on commit 45667f6

Please sign in to comment.