Skip to content

Commit 9f71de1

Browse files
authored
connect web socket in separate thread (#116)
* connect websocket in seprete thread * connect websocket in seprete thread * connect websocket in seprete thread
1 parent 79e8e69 commit 9f71de1

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/websocket/WebSocketConstants.java

+2
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,6 @@ private WebSocketConstants() {
449449
*/
450450
public static final String BROADCAST = "broadcast";
451451

452+
public static final int WEBSOCKET_CONNECTION_TIMEOUT = 10000; //10 sec
453+
452454
}

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/websocket/WebSocketHandler.java

+29-12
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424

2525
import static io.antmedia.webrtcandroidframework.websocket.WebSocketConstants.DEFINITION;
2626
import static io.antmedia.webrtcandroidframework.websocket.WebSocketConstants.NOTIFICATION_COMMAND;
27+
import static io.antmedia.webrtcandroidframework.websocket.WebSocketConstants.WEBSOCKET_CONNECTION_TIMEOUT;
28+
2729

2830
import com.google.gson.Gson;
2931
import com.google.gson.GsonBuilder;
30-
3132
public class WebSocketHandler implements WebSocket.WebSocketConnectionObserver {
3233
private static final String TAG = "WebSocketHandler";
3334
private static final int CLOSE_TIMEOUT = 1000;
@@ -61,20 +62,36 @@ public WebSocketHandler(AntMediaSignallingEvents signallingListener, Handler han
6162
gson = builder.create();
6263
}
6364

65+
public WebSocketConnection creteWebSocket(){
66+
return new WebSocketConnection();
67+
}
6468
public void connect(final String wsUrl) {
6569
checkIfCalledOnValidThread();
70+
if(wsUrl==null || wsUrl.isBlank())
71+
return;
6672
wsServerUrl = wsUrl;
67-
Log.d(TAG, "Connecting WebSocket to: " + wsUrl);
68-
ws = new WebSocketConnection();
69-
try {
70-
ws.connect(new URI(wsServerUrl), this);
71-
} catch (WebSocketException e) {
72-
e.printStackTrace();
73-
disconnect(false);
74-
} catch (URISyntaxException e) {
75-
e.printStackTrace();
76-
disconnect(false);
77-
}
73+
74+
ws = creteWebSocket();
75+
Thread connectorThread = new Thread(() -> {
76+
try {
77+
ws.connect(new URI(wsServerUrl), this);
78+
} catch (WebSocketException e) {
79+
e.printStackTrace();
80+
disconnect(false);
81+
} catch (URISyntaxException e) {
82+
e.printStackTrace();
83+
disconnect(false);
84+
}
85+
});
86+
connectorThread.start();
87+
handler.postDelayed(new Runnable() {
88+
public void run() {
89+
if (connectorThread.isAlive()) {
90+
connectorThread.interrupt();
91+
Log.e(TAG, "exception occurred while waiting for websocket");
92+
}
93+
}
94+
},WEBSOCKET_CONNECTION_TIMEOUT);
7895
}
7996

8097
public void sendTextMessage(String message) {

webrtc-android-framework/src/test/java/io/antmedia/webrtcandroidframework/WebSocketHandlerTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Handler;
44

5+
import org.awaitility.Awaitility;
56
import org.json.JSONArray;
67
import org.json.JSONException;
78
import org.json.JSONObject;
@@ -13,16 +14,19 @@
1314
import org.webrtc.IceCandidate;
1415
import org.webrtc.SessionDescription;
1516

17+
import java.net.URI;
18+
import java.net.URISyntaxException;
19+
import java.util.ArrayList;
1620
import java.util.concurrent.Executors;
1721

1822
import de.tavendo.autobahn.WebSocketConnection;
23+
import de.tavendo.autobahn.WebSocketException;
1924
import io.antmedia.webrtcandroidframework.websocket.AntMediaSignallingEvents;
2025
import io.antmedia.webrtcandroidframework.websocket.Broadcast;
2126
import io.antmedia.webrtcandroidframework.websocket.WebSocketConstants;
2227
import io.antmedia.webrtcandroidframework.websocket.WebSocketHandler;
2328

2429
import static org.junit.Assert.assertEquals;
25-
import static org.junit.Assert.assertNotNull;
2630
import static org.mockito.Mockito.*;
2731

2832
import com.google.gson.Gson;
@@ -452,6 +456,15 @@ public void testOnBroadcastObjectNotification() {
452456

453457
}
454458

459+
@Test
460+
public void testWsConnect() throws InterruptedException, URISyntaxException, WebSocketException {
461+
String url = "wss://test.antmedia.io:5443/LiveApp/websocket";
462+
doReturn(ws).when(webSocketHandler).creteWebSocket();
463+
webSocketHandler.connect(url);
464+
Thread.sleep(3000);
465+
verify(ws,times(1)).connect(new URI(url),webSocketHandler);
466+
}
467+
455468
@Test
456469
public void testWsReconnection(){
457470
Handler wsReconnectionHandlerMock = mock(Handler.class);

0 commit comments

Comments
 (0)