Skip to content

Commit 537df1f

Browse files
feat: improve WebSocketInstance and WebSocketPlugin with enhanced logging and error handling
- Updated `WebSocketInstance.send` and `close` methods to include detailed logging for better debugging. - Modified `WebSocketPlugin` to handle close operation errors and provide appropriate feedback to the callback context. - Ensured proper binding of event handlers in the JavaScript WebSocket interface for consistent context handling.
1 parent 86856b7 commit 537df1f

3 files changed

Lines changed: 47 additions & 17 deletions

File tree

src/plugins/websocket/src/android/WebSocketInstance.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,42 @@ public void setCallback(CallbackContext callbackContext) {
6868
}
6969

7070
public void send(String message) {
71-
if (webSocket != null) {
72-
webSocket.send(message);
71+
if (this.webSocket != null) {
72+
this.webSocket.send(message);
73+
Log.d("WebSocketInstance", "websocket instanceId=" + this.instanceId + " received send() action call, sending message=" + message);
74+
} else {
75+
Log.d("WebSocketInstance", "websocket instanceId=" + this.instanceId + " received send() action call, ignoring... as webSocket is null (not present)");
7376
}
7477
}
7578

76-
public void close(int code, String reason) {
77-
if (webSocket != null) {
78-
readyState = 2; // CLOSING
79-
webSocket.close(code, reason);
80-
Log.d("WebSocketInstance", "websocket instanceId=" + this.instanceId + " received close() action call");
79+
public String close(int code, String reason) {
80+
if (this.webSocket != null) {
81+
this.readyState = 2; // CLOSING
82+
try {
83+
boolean result = this.webSocket.close(code, reason);
84+
Log.d("WebSocketInstance", "websocket instanceId=" + this.instanceId + " received close() action call");
85+
86+
// if a graceful shutdown was already underway...
87+
// or if the web socket is already closed or canceled. do nothing.
88+
if(!result) {
89+
return null;
90+
}
91+
} catch (Exception e) {
92+
return e.getMessage();
93+
}
94+
95+
return null;
8196
} else {
8297
Log.d("WebSocketInstance", "websocket instanceId=" + this.instanceId + " received close() action call, ignoring... as webSocket is null (not present)");
98+
// TODO: finding a better way of telling it wasn't successful.
99+
return "";
83100
}
84101
}
85102

86-
public void close() {
103+
public String close() {
87104
Log.d("WebSocketInstance", "WebSocket instanceId=" + this.instanceId + " close() called with no arguments. Using defaults.");
88105
// Calls the more specific version with default values
89-
close(DEFAULT_CLOSE_CODE, DEFAULT_CLOSE_REASON);
106+
return close(DEFAULT_CLOSE_CODE, DEFAULT_CLOSE_REASON);
90107
}
91108

92109
@Override

src/plugins/websocket/src/android/WebSocketPlugin.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.foxdebug.websocket;
22

3+
import android.util.Log;
4+
35
import org.apache.cordova.*;
46
import org.json.*;
57

@@ -35,6 +37,7 @@ public boolean execute(String action, JSONArray args, final CallbackContext call
3537
String instanceId = args.optString(0);
3638
String message = args.optString(1);
3739
WebSocketInstance inst = instances.get(instanceId);
40+
Log.d("WebSocketPlugin", "send called");
3841
if (inst != null) {
3942
inst.send(message);
4043
callbackContext.success();
@@ -45,12 +48,21 @@ public boolean execute(String action, JSONArray args, final CallbackContext call
4548

4649
case "close":
4750
instanceId = args.optString(0);
48-
int code = args.optInt(1);
49-
String reason = args.optString(2);
51+
// defaults code to 1000 & reason to "Normal closure"
52+
int code = args.optInt(1, 1000);
53+
String reason = args.optString(2, "Normal closure");
5054
inst = instances.get(instanceId);
5155
if (inst != null) {
52-
inst.close(code, reason);
53-
instances.remove(instanceId);
56+
String error = inst.close(code, reason);
57+
58+
if(error == null) {
59+
instances.remove(instanceId);
60+
callbackContext.success();
61+
return true;
62+
} else if(!error.isEmpty()) {
63+
// if error is empty means the websocket is not ready/open.
64+
callbackContext.error(error);
65+
}
5466
callbackContext.success();
5567
} else {
5668
callbackContext.error("Invalid instance ID");
@@ -88,5 +100,6 @@ public void onDestroy() {
88100
}
89101
instances.clear();
90102
okHttpMainClient.dispatcher().executorService().shutdown();
103+
Log.i("WebSocketPlugin", "cleaned up... on destroy");
91104
}
92105
}

src/plugins/websocket/www/websocket.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class WebSocketInstance {
1515
if (event.type === 'open') {
1616
this.readyState = WebSocketInstance.OPEN;
1717
this.extensions = event.extensions || '';
18-
if (this.onopen) this.onopen(event);
18+
if (this.onopen) this.onopen.bind(this)(event);
1919
}
20-
if (event.type === 'message' && this.onmessage) this.onmessage(event);
20+
if (event.type === 'message' && this.onmessage) this.onmessage.bind(this)(event);
2121
if (event.type === 'close') {
2222
this.readyState = WebSocketInstance.CLOSED;
23-
if (this.onclose) this.onclose(event);
23+
if (this.onclose) this.onclose.bind(this)(event);
2424
}
25-
if (event.type === 'error' && this.onerror) this.onerror(event);
25+
if (event.type === 'error' && this.onerror) this.onerror.bind(this)(event);
2626
}, null, "WebSocketPlugin", "registerListener", [this.instanceId]);
2727
}
2828

0 commit comments

Comments
 (0)