git » sdk » commit 871118d

If initial connection fatally fails, notify client

author Stephen Paul Weber
2024-10-08 18:25:40 UTC
committer Stephen Paul Weber
2024-10-08 18:25:40 UTC
parent 22db5177630a35a2afe52e930878ce7d2035edd3

If initial connection fatally fails, notify client

snikket/Client.hx +12 -0
snikket/streams/XmppJsStream.hx +17 -8

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<String>;
 	private var debug = true;
 	private var state:FSM;
 	private var pending:Array<XmppJsXml> = [];
 	private var pendingOnIq:Array<{type:IqRequestType,tag:String,xmlns:String,handler:(Stanza)->IqResult}> = [];
 	private var initialSM: Null<BytesData> = 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;
+	}
 }