Skip to content

Commit 532e2ea

Browse files
committed
Close previous connection when opening a new inspector
1 parent 3aeaa47 commit 532e2ea

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

test-app/app/src/main/java/com/tns/AndroidJsV8Inspector.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ private class JsV8InspectorServer extends NanoWSD {
218218
super(name);
219219
}
220220

221+
private JsV8InspectorWebSocket webSocket;
222+
221223
@Override
222224
protected Response serveHttp(IHTTPSession session) {
223225
if (DEBUG_LOG_ENABLED) {
@@ -228,7 +230,18 @@ protected Response serveHttp(IHTTPSession session) {
228230

229231
@Override
230232
protected WebSocket openWebSocket(IHTTPSession handshake) {
231-
return new JsV8InspectorWebSocket(handshake);
233+
// close the previous webSocket
234+
if(this.webSocket != null) {
235+
try {
236+
this.webSocket.close(WebSocketFrame.CloseCode.NormalClosure, "New browser connection is open", false);
237+
} catch (IOException ioException) {
238+
if(this.webSocket.getState() != State.CLOSED) {
239+
Log.e("{N}.v8-inspector", "Error closing previous connection", ioException);
240+
}
241+
}
242+
}
243+
this.webSocket = new JsV8InspectorWebSocket(handshake);
244+
return this.webSocket;
232245
}
233246
}
234247

test-app/runtime/src/main/cpp/com_tns_AndroidJsV8Inspector.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_init(JNIEnv* env, jo
1212
}
1313

1414
JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_connect(JNIEnv* env, jobject instance, jobject connection) {
15+
JsV8InspectorClient::GetInstance()->disconnect();
1516
JsV8InspectorClient::GetInstance()->connect(connection);
1617
}
1718

test-app/runtime/src/main/java/fi/iki/elonen/NanoWSD.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public boolean isOpen() {
101101
return state == State.OPEN;
102102
}
103103

104+
public State getState() { return this.state; }
105+
104106
protected abstract void onOpen();
105107

106108
protected abstract void onClose(CloseCode code, String reason, boolean initiatedByRemote);

0 commit comments

Comments
 (0)