feat: ✨ Native Websocket Plugin (uses okhttp)#1335
feat: ✨ Native Websocket Plugin (uses okhttp)#1335bajrangCoder merged 15 commits intoAcode-Foundation:mainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
Introduces a native WebSocket Cordova plugin for Android using OkHttp, providing a JavaScript WebSocket-like interface.
- Added a JS bridge (
www/websocket.js) implementingconnect()andWebSocketInstance - Implemented Cordova plugin classes (
WebSocketPlugin.java,WebSocketInstance.java) using OkHttp for real WebSocket support - Updated
plugin.xml, pluginpackage.json, rootpackage.json, andREADME.mdfor installation and usage
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/plugins/websocket/www/websocket.js | JS API: connect() returns WebSocketInstance and routes events |
| src/plugins/websocket/src/android/WebSocketPlugin.java | CordovaPlugin routes actions (connect, send, close, listener) |
| src/plugins/websocket/src/android/WebSocketInstance.java | OkHttp WebSocket listener with event marshaling to JS |
| src/plugins/websocket/plugin.xml | Registers JS module and Android sources, declares OkHttp framework |
| src/plugins/websocket/package.json | Plugin metadata, keywords, and license |
| src/plugins/websocket/README.md | Usage guide and API reference |
| package.json | Added plugin reference under cordovaPlugins and project deps |
Comments suppressed due to low confidence (1)
src/plugins/websocket/src/android/WebSocketInstance.java:15
- [nitpick] Core WebSocketInstance behavior (e.g., event callbacks and state transitions) lacks tests. Consider adding unit or integration tests to cover the listener methods.
public class WebSocketInstance extends WebSocketListener {
|
+1, copilot suggestions seem ok-ish. I'll add them by testing out. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
… functionality - Updated `WebSocketPlugin.connect` to accept custom headers. - Enhanced `WebSocketInstance.close` method to allow specifying close code and reason. - Added `WebSocketPlugin.listClients` method to list active WebSocket instances. - Updated JavaScript interface to support new features and improved documentation.
…ging 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.
There was a problem hiding this comment.
Pull Request Overview
Adds a native WebSocket Cordova plugin for Android using OkHttp, exposing a JS API that mimics the browser WebSocket interface and managing connections natively.
- Introduces a JS module (
websocket.js) withconnect,send,close, andlistClientsmethods plus event handlers. - Implements
WebSocketPlugin.javaandWebSocketInstance.javaon Android, leveraging OkHttp for actual socket work. - Updates
plugin.xml,package.json, andREADME.mdfor plugin registration, metadata, and documentation.
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/plugins/websocket/www/websocket.js | JS wrapper providing promise-based connect, send, close, listClients |
| src/plugins/websocket/src/android/WebSocketPlugin.java | CordovaPlugin execute logic, instance map, lifecycle management |
| src/plugins/websocket/src/android/WebSocketInstance.java | OkHttp WebSocketListener handling events and sending to JS |
| src/plugins/websocket/plugin.xml | Plugin manifest, Android feature and source-file declarations |
| src/plugins/websocket/package.json | Plugin npm metadata and cordova integration settings |
| src/plugins/websocket/README.md | Usage instructions and API reference |
| package.json | Adds plugin to host app’s dependencies |
Comments suppressed due to low confidence (2)
src/plugins/websocket/www/websocket.js:68
- [nitpick] The exported 'send' and 'close' helper functions shadow the instance methods with the same names. Consider renaming them (e.g., 'sendById' / 'closeById') to improve clarity.
const send = function(instanceId, message) {
src/plugins/websocket/www/websocket.js:54
- The core API methods (connect, send, close, listClients) lack accompanying unit or integration tests. Consider adding tests to validate success and error flows.
const connect = function(url, protocols = null, headers = null) {
| <plugin id="cordova-plugin-websocket" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0"> | ||
| <name>cordova-plugin-websocket</name> | ||
| <description>Cordova Websocket</description> | ||
| <license>MIT</license> |
There was a problem hiding this comment.
The license in plugin.xml is set to MIT, but package.json specifies Apache-2.0. Please align the license entries to avoid confusion for consumers.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ocket instances management
- 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.
…ved event handling - Added support for binary messages in `WebSocketInstance` with a new `onMessage` method for handling `ByteString`. - Updated `sendEvent` method to include a flag indicating if the message is binary. - Modified JavaScript interface to handle binary data and added logging for better debugging. - Updated `WebSocketPlugin.connect` to accept a `binaryType` parameter for configuration.
TODO
|
Fixes ` error: name clash: class WebSocketInstance has two methods with the same erasure, yet neither overrides the other public void onMessage(@nonnull WebSocket webSocket, @nonnull String text) { ^ first method: onMessage(WebSocket,ByteString) in WebSocketInstance second method: onMessage(WebSocket,ByteString) in WebSocketListener`
…ed API - Updated `WebSocketPlugin.connect` to accept an optional `binaryType` parameter. - Modified `WebSocketInstance.send` method to handle both string and binary messages. - Enhanced JavaScript interface to support binary message sending and receiving. - Improved event handling for binary messages in both Java and JavaScript layers. - Added logging for better debugging of message sending and receiving.
- Added logic to remove the WebSocket instance from the plugin after a close event is received. - Introduced a new static method `removeInstance` in `WebSocketPlugin` to handle instance removal.
- Added a call to `this.webSocket.close(code, reason)` in the `onClosing` method to ensure the WebSocket instance closes with the specified code and reason. Ref: square/okhttp#6510 (comment)
Adds Native Websocket Cordova Plugin (using okhttp)
Important
plugin initialise(using okhttp singleton client) & destroy(closing okhttp builder clients) are to be completed. While the plugin is functional but those methods are important for plugin lifecycles. Also, custom headers support to be added.
Features
onopen,onmessage,onerror,oncloseextensionsandreadyStatepropertieslistClients()to list active connectionsUsage
Usage
Import
Connect to WebSocket
API Reference
Methods
WebSocketPlugin.connect(url, protocols, headers)url: The WebSocket server URL.protocols: (Optional) An array of subprotocol strings.headers(object, optional): Custom headers as key-value pairs.WebSocketInstance.WebSocketPlugin.listClients()Promisethat resolves to an array ofinstanceIdstrings.WebSocketPlugin.send(instanceId, message)WebSocketInstance.send(message)but needsinstanceId.Promisethat resolves.WebSocketPlugin.close(instanceId, code, reason)WebSocketInstance.close(code, reason)but needsinstanceId.Promisethat resolves.WebSocketInstance.send(message)WebSocketInstance.close(code, reason)code: (Optional) If unspecified, a close code for the connection is automatically set: to 1000 for a normal closure, or otherwise to another standard value in the range 1001-1015 that indicates the actual reason the connection was closed.reason: A string providing a custom WebSocket connection close reason (a concise human-readable prose explanation for the closure). The value must be no longer than 123 bytes (encoded in UTF-8).Properties of
WebSocketInstanceonopen: Event listener for connection open.onmessage: Event listener for messages received.onclose: Event listener for connection close.onerror: Event listener for errors.readyState: (number) The state of the connection.CONNECTING): Socket created, not yet open.OPEN): Connection is open and ready.CLOSING): Connection is closing.CLOSED): Connection is closed or couldn't be opened.extensions: (string) Extensions negotiated by the server (usually empty or a list).