Skip to content

Commit

Permalink
If initial connection fatally fails, notify client
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Oct 8, 2024
1 parent 22db517 commit 871118d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
12 changes: 12 additions & 0 deletions snikket/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions snikket/streams/XmppJsStream.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -121,6 +122,9 @@ class XmppJsStream extends GenericStream {
"online" => this.onOnline,
"offline" => this.onOffline,
],
transition_handlers: [
"connection-error" => this.onError,
],
}, "offline");
}

Expand All @@ -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) {
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}

0 comments on commit 871118d

Please sign in to comment.