Skip to content

Commit

Permalink
Fix Websocket Server
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisFeline committed Oct 4, 2024
1 parent b27448a commit 9181ea0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ static Settings() {
/// </summary>
public bool WebSocketEnabled { get; set; } = false;
public bool WebTrackerCompatibility { get; set; } = true;
public int WebSocketPort { get; set; } = 11398;

/// <summary>
/// Import a settings instance from the local json file
Expand Down
39 changes: 27 additions & 12 deletions Utils/API/WebSocketAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace ToNSaveManager.Utils.API {
internal class WebSocketAPI : WebSocketBehavior {
static readonly LoggerSource Logger = new LoggerSource("WS-API");
static readonly LoggerSource Logger = new LoggerSource(nameof(WebSocketAPI));
internal static WebSocketServer? Server;

protected override void OnMessage(MessageEventArgs e) {
Expand All @@ -30,20 +30,21 @@ protected override void OnOpen() {
Args = EventBuffer.ToArray(),
DisplayName = ToNLogContext.Instance?.DisplayName ?? string.Empty,
UserID = ToNLogContext.Instance?.UserID ?? string.Empty
});
SendValue("INSTANCE", ToNLogContext.Instance?.InstanceID);
}, this);

SendEvent(new EventValue<string?>("INSTANCE", ToNLogContext.Instance?.InstanceID), this);

// Send All Stats
foreach (string key in ToNStats.PropertyKeys) {
WebSocketAPI.EventStats.Send(key, ToNStats.Get(key));
SendEvent(new EventStats() { Name = key, Value = ToNStats.Get(key) }, this);
}
}

const int DEFAULT_PORT = 11398;
internal static void Initialize() {
if (Settings.Get.WebSocketEnabled && Server == null) {
const string url = "ws://localhost:11398";
Server = new WebSocketServer(url);
int port = Settings.Get.WebSocketPort > 0 ? Settings.Get.WebSocketPort : DEFAULT_PORT;
Server = new WebSocketServer(port);
Server.AddWebSocketService<WebSocketAPI>("/");
}

Expand All @@ -62,10 +63,24 @@ internal static void Broadcast(string data) {
Logger.Debug("Broadcasting: " + data);
Server?.WebSocketServices?.Broadcast(data);
}
internal static void SendObject(object data) => Broadcast(JsonConvert.SerializeObject(data));
internal static void SendEvent<T>(T value) where T : IEvent
{
if (Settings.Get.WebSocketEnabled) SendObject(value);
internal static void SendEvent<T>(T value, WebSocketAPI? connection) where T : IEvent {
if (!Settings.Get.WebSocketEnabled) return;
string jsonData = JsonConvert.SerializeObject(value);

try {
if (connection != null) {
Logger.Debug("Sending: " + jsonData);
connection.Send(jsonData);
return;
}

Broadcast(jsonData);
} catch (Exception e) {
Logger.Error(e);
#if DEBUG
throw;
#endif
}
}

public interface IEvent {
Expand Down Expand Up @@ -236,7 +251,7 @@ private static void QueueEvent(IEvent ev, bool buffer = true) {

internal static void SendEventUpdate() {
while (EventQueue.Count > 0) {
SendEvent(EventQueue.Dequeue());
SendEvent(EventQueue.Dequeue(), null);
}
}

Expand Down

0 comments on commit 9181ea0

Please sign in to comment.