From d9662ed189a8176fd625dc6d9d3269b5c4954e54 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 15 Apr 2021 09:15:11 +0200 Subject: [PATCH] Create WebSocket connection --- .../NotificationsService.java | 104 +++++++++++++++++- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/java/org/communiquons/comunic/independentnotifications/NotificationsService.java b/android/app/src/main/java/org/communiquons/comunic/independentnotifications/NotificationsService.java index 92f512e..e9d7d0c 100644 --- a/android/app/src/main/java/org/communiquons/comunic/independentnotifications/NotificationsService.java +++ b/android/app/src/main/java/org/communiquons/comunic/independentnotifications/NotificationsService.java @@ -17,16 +17,33 @@ import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import androidx.core.content.ContextCompat; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketAdapter; +import com.neovisionaries.ws.client.WebSocketException; +import com.neovisionaries.ws.client.WebSocketFactory; +import com.neovisionaries.ws.client.WebSocketFrame; + import org.communiquons.comunic.MainActivity; import org.communiquons.comunic.R; -public class NotificationsService extends Service { +import java.util.List; +import java.util.Map; + +public class NotificationsService extends Service implements Runnable { private static final String TAG = NotificationsService.class.getSimpleName(); public static final String CHANNEL_ID = "IndependentPushServiceChannel"; private static final String INDEPENDENT_PUSH_NOTIFICATIONS_SERVICE = "independent-push-notifications-service"; private static final String WS_URL_PREF_KEY = "ws_url"; + private static final int CONNECT_TIMEOUT = 1000; + private static final int RECONNECT_INTERVAL = 10000; + private static final int PING_INTERVAL = 15000; + + private Thread thread; + private boolean stop = false; + private final Object lock = new Object(); + public static void configure(@NonNull String wsURL, @NonNull Context context) { SharedPreferences prefs = context.getSharedPreferences(INDEPENDENT_PUSH_NOTIFICATIONS_SERVICE, MODE_PRIVATE); @@ -54,10 +71,12 @@ public class NotificationsService extends Service { SharedPreferences prefs = context.getSharedPreferences(INDEPENDENT_PUSH_NOTIFICATIONS_SERVICE, MODE_PRIVATE); if (!prefs.contains(WS_URL_PREF_KEY)) { - System.err.println("Independent push notifications service not configured ! Skipping!"); + Log.v(TAG, "Independent push notifications service not configured ! Skipping!"); return; } + Log.v(TAG, "Start independent push notifications service..."); + Intent intent = new Intent(context, NotificationsService.class); ContextCompat.startForegroundService(context, intent); } @@ -104,13 +123,90 @@ public class NotificationsService extends Service { public void onDestroy() { Log.v(TAG, "Destroying service"); super.onDestroy(); + + if (thread != null) { + thread.interrupt(); + stop = true; + } } private void initService() { + thread = new Thread(this); + thread.start(); + } + + @Override + public void run() { + try { + while (!stop) { + try { + connect(); + } catch (InterruptedException e) { + Log.e(TAG, "Thread interrupted!"); + return; + } catch (Exception e) { + Log.e(TAG, "Failed to connect to push notifications service!"); + e.printStackTrace(); + } + + + // Wait attempting new connection + // noinspection BusyWait + Thread.sleep(RECONNECT_INTERVAL * 1000); + } + } catch (InterruptedException e) { + e.printStackTrace(); + Log.e(TAG, "Stop thread"); + } + } + + private void connect() throws Exception { String url = getSharedPreferences(INDEPENDENT_PUSH_NOTIFICATIONS_SERVICE, MODE_PRIVATE) .getString(WS_URL_PREF_KEY, null); - System.out.println("START HEAVY WORK HERE !!!!"); - System.out.println("Connect to " + url); + Log.v(TAG, "Connect to " + url); + + WebSocket ws = new WebSocketFactory().createSocket(url, CONNECT_TIMEOUT); + ws.setPingInterval(PING_INTERVAL); + ws.addListener(new WebSocketAdapter() { + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + Log.v(TAG, "Connected to independent push notifications service!"); + } + + @Override + public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception { + Log.v(TAG, "Disconnect from independent push notifications websocket!"); + lock.notify(); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + handleTextFrame(frame); + } + + @Override + public void onError(WebSocket websocket, WebSocketException cause) throws Exception { + Log.e(TAG, "An error occured, closing WebSocket!"); + cause.printStackTrace(); + websocket.disconnect(); + } + }); + + ws.connect(); + + + // wait for connection to complete + synchronized (lock) { + lock.wait(); + } + } + + private void handleTextFrame(WebSocketFrame frame) { + if (!frame.isTextFrame()) + return; + + Log.v(TAG, "Got text frame!"); + Log.v(TAG, frame.getPayloadText()); } }