Skip to content

Commit 48a831b

Browse files
feat: enhance WebSocket close event handling
- Updated `WebSocketInstance.onClosed` to send a structured close event with code and reason. - Modified JavaScript event handling to properly receive and process the close event data.
1 parent d6862a6 commit 48a831b

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.apache.cordova.*;
88
import org.json.JSONArray;
9+
import org.json.JSONException;
910
import org.json.JSONObject;
1011

1112
import java.util.Iterator;
@@ -130,8 +131,15 @@ public void onClosing(@NonNull WebSocket webSocket, int code, @NonNull String re
130131
@Override
131132
public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
132133
this.readyState = 3; // CLOSED
133-
sendEvent("close", reason);
134134
Log.i("WebSocketInstance", "websocket instanceId=" + this.instanceId + " Closed code: " + code + " reason: " + reason);
135+
JSONObject closedEvent = new JSONObject();
136+
try {
137+
closedEvent.put("code", code);
138+
closedEvent.put("reason", reason);
139+
} catch (JSONException e) {
140+
Log.e("WebSocketInstance", "Error creating close event", e);
141+
}
142+
sendEvent("close", closedEvent.toString());
135143
}
136144

137145
@Override

src/plugins/websocket/www/websocket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class WebSocketInstance {
2020
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.bind(this)(event);
23+
if (this.onclose) this.onclose.bind(this)({ code: event?.data?.code, reason: event?.data?.reason, type: event.type });
2424
}
2525
if (event.type === 'error' && this.onerror) this.onerror.bind(this)(event);
2626
}, null, "WebSocketPlugin", "registerListener", [this.instanceId]);

0 commit comments

Comments
 (0)