diff --git a/CollabVMSharp/CollabVMClient.cs b/CollabVMSharp/CollabVMClient.cs index ca3848a..92a1182 100644 --- a/CollabVMSharp/CollabVMClient.cs +++ b/CollabVMSharp/CollabVMClient.cs @@ -39,7 +39,7 @@ public class CollabVMClient { private TurnUpdateEventArgs _currentturn; private VoteUpdateEventArgs _currentvote; private WebProxy? _proxy; - private Dictionary>> commands; + private Dictionary> commands; // Tasks and related private TaskCompletionSource GotNodeList; private TaskCompletionSource GotConnectionToNode; @@ -63,6 +63,7 @@ public class CollabVMClient { public event EventHandler ChatHistory; public event EventHandler ConnectedToNode; public event EventHandler NodeConnectFailed; + public event EventHandler ConnectionFailed; public event EventHandler Rect; public event EventHandler Renamed; public event EventHandler UserRenamed; @@ -129,6 +130,7 @@ public class CollabVMClient { ChatHistory += delegate { }; ConnectedToNode += delegate { }; NodeConnectFailed += delegate { }; + ConnectionFailed += delegate { }; Rect += delegate { }; Renamed += delegate { }; UserRenamed += delegate { }; @@ -143,8 +145,15 @@ public class CollabVMClient { /// /// Connect to the CollabVM Server /// - public async Task Connect() { - await this.socket.ConnectAsync(this.url, CancellationToken.None); + public async Task Connect() { + try { + await this.socket.ConnectAsync(this.url, CancellationToken.None); + } + catch (WebSocketException e) { + this.ConnectionFailed.Invoke(this, e.Message); + this.Cleanup(false); + return false; + } this._connected = true; if (this.username != null) this.SendMsg(Guacutils.Encode("rename", this.username)); @@ -154,6 +163,7 @@ public class CollabVMClient { this.SendMsg(Guacutils.Encode("connect", this.node)); this.NOPRecieve.Start(); this.WebSocketLoop(); + return true; } private async void WebSocketLoop() { @@ -803,7 +813,7 @@ public class CollabVMClient { /// /// The command which triggers the callback. For example, "!ban" would match "!ban guest12345" /// Function to be called when a user executes the command. The first parameter is a username and the last is an array of arguments - public void RegisterCommand(string cmd, Action> callback) { + public void RegisterCommand(string cmd, Action callback) { this.commands.Add(cmd, callback); } @@ -818,7 +828,7 @@ public class CollabVMClient { return; } if (commands.ContainsKey(args[0])) - commands[args[0]](username, args.Skip(1)); + commands[args[0]](username, args.Skip(1).ToArray()); } public Image GetFramebuffer() => framebuffer.CloneAs();