Skip to content

Commit

Permalink
fix: No messages after closed connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Marconi Gomes committed Sep 10, 2023
1 parent 0ccffbb commit 06e851d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
36 changes: 29 additions & 7 deletions AMRPC WatchDog Desktop/Messenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,41 @@ namespace AMRPC_WatchDog_Desktop
internal class Messenger
{
private readonly Payload _payload;
private WebSocketServer _server;
private Sender _sender;
public Messenger(Payload payload)
{
var sender = new Sender { Payload = payload };
sender.Payload.PropertyChanged += sender.OnPayloadChanged;
_payload = payload;
_payload.PropertyChanged += sender.OnPayloadChanged;
Reconfigure();
}

public void Reconfigure()
{
if (_server != null)
{
_server.RemoveWebSocketService("/watchdog");
_server.Stop();
_server = null;
_sender = null;
}

_sender = new Sender { Payload = _payload };
_payload.PropertyChanged += _sender.OnPayloadChanged;
_sender.Messenger = this;

_server = new WebSocketServer(9632);
_server.AddWebSocketService<Sender>("/watchdog", () => _sender);
_server.Start();

var server = new WebSocketServer(9632);
server.AddWebSocketService<Sender>("/watchdog", () => sender);
server.Start();
}
}

internal class Sender : WebSocketBehavior
{
public Payload Payload;
public Messenger Messenger;

protected override void OnMessage (MessageEventArgs e)
protected override void OnMessage(MessageEventArgs e)
{
Send(JsonConvert.SerializeObject(Payload));
}
Expand All @@ -34,5 +51,10 @@ public void OnPayloadChanged(object sender, System.ComponentModel.PropertyChange
{
Send(JsonConvert.SerializeObject(Payload));
}

protected override void OnClose(CloseEventArgs e)
{
Messenger.Reconfigure();
}
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Go to the [releases page](https://github.com/zephraOSS/AMRPC-WatchDog/releases/t
# TO-DO
- [x] Build the websocket service to export Windows playing info on a websocket
- [x] Build the desktop version
- [ ] Fix the fail to reconnect websocket when watchdog keeps running but connection is reset
- [ ] Fix duplicated websocket messages on song info changed
- [x] Fix the fail to reconnect websocket when watchdog keeps running but connection is closed
- [ ] Fix triplicated websocket messages on song info changed
- [ ] Add "Autostart with system" option
- [ ] Add "Start minimized" option

0 comments on commit 06e851d

Please sign in to comment.