From 871118d1594e1f1bc9f406a0374d5fa7c3fa9998 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 8 Oct 2024 13:25:40 -0500 Subject: [PATCH] If initial connection fatally fails, notify client --- snikket/Client.hx | 12 ++++++++++++ snikket/streams/XmppJsStream.hx | 25 +++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/snikket/Client.hx b/snikket/Client.hx index 7285c6f..a12265f 100644 --- a/snikket/Client.hx +++ b/snikket/Client.hx @@ -888,6 +888,18 @@ class Client extends EventEmitter { }); } + /** + Event fired when connection fails with a fatal error and will not be retried + + @param handler takes no arguments + **/ + public function addConnectionFailedListener(handler:()->Void):Void { + stream.on("status/error", (data) -> { + handler(); + return EventHandled; + }); + } + #if !cpp // TODO: haxe cpp erases enum into int, so using it as a callback arg is hard // could just use int in C bindings, or need to come up with a good strategy diff --git a/snikket/streams/XmppJsStream.hx b/snikket/streams/XmppJsStream.hx index 102ddca..1a8b13b 100644 --- a/snikket/streams/XmppJsStream.hx +++ b/snikket/streams/XmppJsStream.hx @@ -100,13 +100,14 @@ typedef HostMetaJson = { class XmppJsStream extends GenericStream { private var client:XmppJsClient; private var jid:XmppJsJID; - private var connectionURI:String; + private var connectionURI: Null; private var debug = true; private var state:FSM; private var pending:Array = []; private var pendingOnIq:Array<{type:IqRequestType,tag:String,xmlns:String,handler:(Stanza)->IqResult}> = []; private var initialSM: Null = null; private var resumed = false; + private var everConnected = false; override public function new() { super(); @@ -121,6 +122,9 @@ class XmppJsStream extends GenericStream { "online" => this.onOnline, "offline" => this.onOffline, ], + transition_handlers: [ + "connection-error" => this.onError, + ], }, "offline"); } @@ -129,11 +133,10 @@ class XmppJsStream extends GenericStream { request.onData = function (data:String) { try { var parsed:HostMetaJson = Json.parse(data); - for(entry in parsed.links) { - if(entry.href.substr(0, 6) == "wss://") { - callback(entry.href); - return; - } + final links = parsed.links.filter((entry) -> entry.href.substr(0, 6) == "wss://"); + if (links.length > 0) { + callback(links[0].href); + return; } } catch (e) { } @@ -148,8 +151,7 @@ class XmppJsStream extends GenericStream { private function connectWithURI(uri:String) { trace("Got connection URI: "+uri); if(uri == null) { - // What if first is null and next is fine?? - //this.state.event("connection-error"); + this.state.event("connection-error"); return; } connectionURI = uri; @@ -372,6 +374,7 @@ class XmppJsStream extends GenericStream { /* State handlers */ private function onOnline(event) { + everConnected = true; var item; while ((item = pending.shift()) != null) { client.send(item); @@ -383,4 +386,10 @@ class XmppJsStream extends GenericStream { private function onOffline(event) { trigger("status/offline", {}); } + + private function onError(event) { + if (!everConnected) trigger("status/error", {}); + // If everConnected then we are retrying so not fatal + return true; + } }