Improve websocket connection management

This commit is contained in:
Mario Danic 2018-10-25 00:56:01 +02:00
parent 91d0d829e0
commit 3ec2480848
4 changed files with 25 additions and 7 deletions

View file

@ -1196,6 +1196,10 @@ public class CallController extends BaseController {
webSocketClient = webSocketConnectionHelper.getExternalSignalingInstanceForServer(
externalSignalingServer.getExternalSignalingServer(),
conversationUser, externalSignalingServer.getExternalSignalingTicket());
if (webSocketClient.isConnected()) {
joinRoomAndCall();
}
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)

View file

@ -183,6 +183,7 @@ public class RestModule {
CookieManager cookieManager, Dispatcher dispatcher) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.retryOnConnectionFailure(true);
httpClient.connectTimeout(45, TimeUnit.SECONDS);
httpClient.readTimeout(45, TimeUnit.SECONDS);
httpClient.writeTimeout(45, TimeUnit.SECONDS);

View file

@ -72,6 +72,8 @@ public class MagicWebSocketInstance extends WebSocketListener {
private WebSocket webSocket;
private MagicMap magicMap;
private String currentRoomToken;
MagicWebSocketInstance(UserEntity conversationUser, String connectionUrl, String webSocketTicket) {
NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
Request request = new Request.Builder().url(connectionUrl).build();
@ -115,12 +117,17 @@ public class MagicWebSocketInstance extends WebSocketListener {
break;
case "error":
ErrorOverallWebSocketMessage errorOverallWebSocketMessage = LoganSquare.parse(text, ErrorOverallWebSocketMessage.class);
if (("no_such_session").equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode().equals("no_such_session"))) {
resumeId = "";
}
break;
case "room":
JoinedRoomOverallWebSocketMessage joinedRoomOverallWebSocketMessage = LoganSquare.parse(text, JoinedRoomOverallWebSocketMessage.class);
if (joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomPropertiesWebSocketMessage() != null) {
HashMap<String, String> joinRoomHashMap = new HashMap<>();
joinRoomHashMap.put("roomToken", joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomId());
currentRoomToken = joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomId();
eventBus.post(new WebSocketCommunicationEvent("roomJoined", joinRoomHashMap));
}
break;
@ -202,10 +209,16 @@ public class MagicWebSocketInstance extends WebSocketListener {
}
public void joinRoomWithRoomTokenAndSession(String roomToken, String normalBackendSession) {
try {
webSocket.send(LoganSquare.serialize(webSocketConnectionHelper.getAssembledJoinOrLeaveRoomModel(roomToken, normalBackendSession)));
} catch (IOException e) {
Log.e(TAG, "Failed to serialize room overall websocket message");
if (!roomToken.equals(currentRoomToken)) {
try {
webSocket.send(LoganSquare.serialize(webSocketConnectionHelper.getAssembledJoinOrLeaveRoomModel(roomToken, normalBackendSession)));
} catch (IOException e) {
Log.e(TAG, "Failed to serialize room overall websocket message");
}
} else {
HashMap<String, String> joinRoomHashMap = new HashMap<>();
joinRoomHashMap.put("roomToken", currentRoomToken);
eventBus.post(new WebSocketCommunicationEvent("roomJoined", joinRoomHashMap));
}
}

View file

@ -47,7 +47,7 @@ import okhttp3.OkHttpClient;
@AutoInjector(NextcloudTalkApplication.class)
public class WebSocketConnectionHelper {
private Map<String, MagicWebSocketInstance> magicWebSocketInstanceMap = new HashMap<>();
private static Map<String, MagicWebSocketInstance> magicWebSocketInstanceMap = new HashMap<>();
@Inject
OkHttpClient okHttpClient;
@ -57,7 +57,7 @@ public class WebSocketConnectionHelper {
NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
}
private String getExternalSignalingServerUrlFromSettingsUrl(String url) {
private static String getExternalSignalingServerUrlFromSettingsUrl(String url) {
String generatedURL = url.replace("https://", "wss://").replace("http://", "ws://");
if (generatedURL.endsWith("/")) {
@ -69,7 +69,7 @@ public class WebSocketConnectionHelper {
return generatedURL;
}
public synchronized MagicWebSocketInstance getExternalSignalingInstanceForServer(String url, UserEntity userEntity, String webSocketTicket) {
public static synchronized MagicWebSocketInstance getExternalSignalingInstanceForServer(String url, UserEntity userEntity, String webSocketTicket) {
String connectionUrl = getExternalSignalingServerUrlFromSettingsUrl(url);