Reconnect push notifications (close #34)

This commit is contained in:
bleakgrey 2018-06-02 10:43:54 +03:00
parent a9202d29c4
commit 7917f11f1d
2 changed files with 31 additions and 7 deletions

View File

@ -35,7 +35,7 @@ public class Tootle.NetManager : GLib.Object {
GLib.Object(); GLib.Object();
} }
public async WebsocketConnection stream (Soup.Message msg) { public async WebsocketConnection stream (Soup.Message msg) throws GLib.Error {
return yield session.websocket_connect_async (msg, null, null, null); return yield session.websocket_connect_async (msg, null, null, null);
} }

View File

@ -3,8 +3,10 @@ using Soup;
public class Tootle.Notificator : GLib.Object { public class Tootle.Notificator : GLib.Object {
WebsocketConnection? connection; private WebsocketConnection? connection;
Soup.Message msg; private Soup.Message msg;
private bool closing = false;
private int timeout = 2;
public abstract signal void notification (ref Notification notification); public abstract signal void notification (ref Notification notification);
public abstract signal void status_added (ref Status status); public abstract signal void status_added (ref Status status);
@ -17,17 +19,39 @@ public class Tootle.Notificator : GLib.Object {
} }
public async void start () { public async void start () {
debug ("Starting notificator"); try {
connection = yield Tootle.network.stream (msg); debug ("Starting notificator");
connection.error.connect (on_error); connection = yield Tootle.network.stream (msg);
connection.message.connect (on_message); connection.error.connect (on_error);
connection.message.connect (on_message);
connection.closed.connect (on_closed);
}
catch (GLib.Error e) {
warning (e.message);
on_closed ();
}
} }
public void close () { public void close () {
debug ("Stopping notificator"); debug ("Stopping notificator");
closing = true;
connection.close (0, null); connection.close (0, null);
} }
private bool reconnect () {
start ();
return false;
}
private void on_closed () {
if (closing)
return;
debug ("Notificator aborted. Reconnecting in %i seconds.", timeout);
GLib.Timeout.add_seconds (timeout, reconnect);
timeout = int.min (timeout*2, 60);
}
private void on_error (Error e) { private void on_error (Error e) {
error (e.message); error (e.message);
} }