fix(vitalsharing): UI sync + re-subscribe on reconnect

Two bugs fixed:

1. /mm vitalsharing on|off did not update the checkbox on the Settings
   tab, leaving the UI out of sync with the actual state. Toggling via
   PluginCore.SetVitalSharingEnabled now calls
   Views.VVSTabbedMainView.RefreshSettingsFromConfig so the checkbox
   reflects the new state regardless of whether the toggle came from
   chat, the settings checkbox, or the settings overlay.

2. After a WebSocket reconnect, the plugin was silently dropping out of
   the backend's vital sharing subscriber set. The receive loop's
   disconnect cleanup removes the character from _vital_sharing_subscribers,
   but the plugin only sent share_subscribe once at tracker.Start() — so
   after any reconnect, no future share_* messages would be forwarded to
   that client. Fix: when the WebSocket (re)connects and registers, if
   PluginSettings.VitalSharingEnabled is true, re-send share_subscribe
   immediately after the register envelope.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-11 15:14:30 +02:00
parent 2b6e3cc4fc
commit 49ec534514
3 changed files with 42 additions and 9 deletions

View file

@ -157,17 +157,22 @@ namespace MosswartMassacre
{
PluginSettings.Instance.VitalSharingEnabled = enabled;
var tracker = _instance?._vitalSharingTracker;
if (tracker == null) return;
if (enabled)
if (tracker != null)
{
tracker.Start();
WriteToChat("[VitalShare] ENABLED");
}
else
{
tracker.Stop();
WriteToChat("[VitalShare] DISABLED");
if (enabled)
{
tracker.Start();
WriteToChat("[VitalShare] ENABLED");
}
else
{
tracker.Stop();
WriteToChat("[VitalShare] DISABLED");
}
}
// Sync the checkbox on the Settings tab so chat commands
// and the UI stay in lockstep.
Views.VVSTabbedMainView.RefreshSettingsFromConfig();
}
catch (Exception ex)
{

View file

@ -112,6 +112,34 @@ namespace MosswartMassacre
await SendEncodedAsync(regJson, _cts.Token);
_logger?.Log("[WebSocket] REGISTERED");
// ─── Re-subscribe to vital sharing on every (re)connect ───
// The backend evicts subscribers on disconnect, so after a
// reconnect we need to re-announce or our share_* messages
// won't be forwarded to other clients.
try
{
if (PluginSettings.IsInitialized && PluginSettings.Instance.VitalSharingEnabled)
{
var tag = PluginSettings.Instance.CharTag;
var tags = string.IsNullOrWhiteSpace(tag)
? new string[0]
: new[] { tag };
var subEnvelope = new
{
type = "share_subscribe",
timestamp = DateTime.UtcNow.ToString("o"),
character_name = CoreManager.Current.CharacterFilter.Name,
tags = tags,
};
await SendEncodedAsync(JsonConvert.SerializeObject(subEnvelope), _cts.Token);
_logger?.Log("[WebSocket] Vital sharing re-subscribed");
}
}
catch (Exception ex)
{
_logger?.Log($"[WebSocket] Re-subscribe error: {ex.Message}");
}
var buffer = new byte[4096];
// 2) Fire-and-forget receive loop