From f0017a2a3917c2ea1134d304280426678db44574 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Thu, 25 Oct 2018 01:22:26 +0200 Subject: [PATCH] Implemented proper resume --- .../nextcloud/talk/controllers/CallController.java | 2 +- .../talk/jobs/WebsocketConnectionsWorker.java | 2 +- .../talk/webrtc/MagicWebSocketInstance.java | 14 +++++++++++--- .../talk/webrtc/WebSocketConnectionHelper.java | 14 ++++---------- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/CallController.java b/app/src/main/java/com/nextcloud/talk/controllers/CallController.java index 78d22c4aa..1c527f864 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/CallController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/CallController.java @@ -1195,7 +1195,7 @@ public class CallController extends BaseController { webSocketConnectionHelper = new WebSocketConnectionHelper(); webSocketClient = webSocketConnectionHelper.getExternalSignalingInstanceForServer( externalSignalingServer.getExternalSignalingServer(), - conversationUser, externalSignalingServer.getExternalSignalingTicket()); + conversationUser, externalSignalingServer.getExternalSignalingTicket(), false); if (webSocketClient.isConnected()) { joinRoomAndCall(); diff --git a/app/src/main/java/com/nextcloud/talk/jobs/WebsocketConnectionsWorker.java b/app/src/main/java/com/nextcloud/talk/jobs/WebsocketConnectionsWorker.java index 5c14acbb5..34210df77 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/WebsocketConnectionsWorker.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/WebsocketConnectionsWorker.java @@ -67,7 +67,7 @@ public class WebsocketConnectionsWorker extends Worker { !TextUtils.isEmpty(externalSignalingServer.getExternalSignalingTicket())) { webSocketConnectionHelper.getExternalSignalingInstanceForServer( externalSignalingServer.getExternalSignalingServer(), - userEntity, externalSignalingServer.getExternalSignalingTicket()); + userEntity, externalSignalingServer.getExternalSignalingTicket(), false); } } catch (IOException e) { Log.e(TAG, "Failed to parse external signaling server"); diff --git a/app/src/main/java/com/nextcloud/talk/webrtc/MagicWebSocketInstance.java b/app/src/main/java/com/nextcloud/talk/webrtc/MagicWebSocketInstance.java index 600352dfc..0c71b3aad 100644 --- a/app/src/main/java/com/nextcloud/talk/webrtc/MagicWebSocketInstance.java +++ b/app/src/main/java/com/nextcloud/talk/webrtc/MagicWebSocketInstance.java @@ -71,19 +71,20 @@ public class MagicWebSocketInstance extends WebSocketListener { private WebSocketConnectionHelper webSocketConnectionHelper; private WebSocket webSocket; private MagicMap magicMap; + private String connectionUrl; private String currentRoomToken; MagicWebSocketInstance(UserEntity conversationUser, String connectionUrl, String webSocketTicket) { NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); - Request request = new Request.Builder().url(connectionUrl).build(); - this.webSocket = okHttpClient.newWebSocket(request, this); + this.connectionUrl = connectionUrl; this.conversationUser = conversationUser; this.webSocketTicket = webSocketTicket; this.webSocketConnectionHelper = new WebSocketConnectionHelper(); - magicMap = new MagicMap(); + + restartWebSocket(); } @Override @@ -99,6 +100,11 @@ public class MagicWebSocketInstance extends WebSocketListener { } } + private void restartWebSocket() { + Request request = new Request.Builder().url(connectionUrl).build(); + this.webSocket = okHttpClient.newWebSocket(request, this); + } + @Override public void onMessage(WebSocket webSocket, String text) { Log.d(TAG, "Receiving : " + text); @@ -121,6 +127,7 @@ public class MagicWebSocketInstance extends WebSocketListener { resumeId = ""; } + restartWebSocket(); break; case "room": JoinedRoomOverallWebSocketMessage joinedRoomOverallWebSocketMessage = LoganSquare.parse(text, JoinedRoomOverallWebSocketMessage.class); @@ -198,6 +205,7 @@ public class MagicWebSocketInstance extends WebSocketListener { public void onFailure(WebSocket webSocket, Throwable t, Response response) { Log.d(TAG, "Error : " + t.getMessage()); connected = false; + restartWebSocket(); } public String getSessionId() { diff --git a/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java b/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java index 4174f9101..7e8de01d9 100644 --- a/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java +++ b/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java @@ -57,7 +57,7 @@ public class WebSocketConnectionHelper { NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); } - private static String getExternalSignalingServerUrlFromSettingsUrl(String url) { + public static synchronized MagicWebSocketInstance getExternalSignalingInstanceForServer(String url, UserEntity userEntity, String webSocketTicket, boolean forceReconnect) { String generatedURL = url.replace("https://", "wss://").replace("http://", "ws://"); if (generatedURL.endsWith("/")) { @@ -66,17 +66,10 @@ public class WebSocketConnectionHelper { generatedURL += "/spreed"; } - return generatedURL; - } - - public static synchronized MagicWebSocketInstance getExternalSignalingInstanceForServer(String url, UserEntity userEntity, String webSocketTicket) { - - String connectionUrl = getExternalSignalingServerUrlFromSettingsUrl(url); - - if (magicWebSocketInstanceMap.containsKey(userEntity.getUserId())) { + if (magicWebSocketInstanceMap.containsKey(userEntity.getUserId()) && !forceReconnect) { return magicWebSocketInstanceMap.get(userEntity.getUserId()); } else { - MagicWebSocketInstance magicWebSocketInstance = new MagicWebSocketInstance(userEntity, connectionUrl, webSocketTicket); + MagicWebSocketInstance magicWebSocketInstance = new MagicWebSocketInstance(userEntity, generatedURL, webSocketTicket); magicWebSocketInstanceMap.put(userEntity.getUserId(), magicWebSocketInstance); return magicWebSocketInstance; } @@ -104,6 +97,7 @@ public class WebSocketConnectionHelper { HelloWebSocketMessage helloWebSocketMessage = new HelloWebSocketMessage(); helloWebSocketMessage.setVersion("1.0"); helloWebSocketMessage.setResumeid(resumeId); + helloOverallWebSocketMessage.setHelloWebSocketMessage(helloWebSocketMessage); return helloOverallWebSocketMessage; }