From 53d85827d60587db59c0a68c3e94771e7d5ad190 Mon Sep 17 00:00:00 2001 From: Stephen Blythe Date: Mon, 30 Jun 2014 21:35:37 +0100 Subject: [PATCH 1/2] Issue #54 - added support for sending binary data. (Implemented for Android only) Interface is: sendBinary(successCallback, errorCallback, connectionId, data) data is a JSONArray, one element per byte, e.g. [ 0x00, 0x01, 0x00, 0xFD ] --- src/android/Connection.java | 14 ++++++++ src/android/SocketPlugin.java | 63 ++++++++++++++++++++++++++++++++++- www/socket.js | 6 ++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/android/Connection.java b/src/android/Connection.java index f834dd7..e7457e9 100644 --- a/src/android/Connection.java +++ b/src/android/Connection.java @@ -3,6 +3,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; @@ -19,6 +20,7 @@ public class Connection extends Thread { private Socket callbackSocket; private PrintWriter writer; + private OutputStream outputStream; private BufferedReader reader; private Boolean mustClose; @@ -101,6 +103,17 @@ public void write(String data) { + /** + * Outputs to socket output stream to send binary data to target host. + * + * @param data information to be sent + */ + public void writeBinary(byte[] data) throws IOException { + this.outputStream.write(data); + } + + + /* (non-Javadoc) * @see java.lang.Thread#run() */ @@ -111,6 +124,7 @@ public void run() { try { this.callbackSocket = new Socket(this.host, this.port); this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true); + this.outputStream = this.callbackSocket.getOutputStream(); this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream())); // receiving data chunk diff --git a/src/android/SocketPlugin.java b/src/android/SocketPlugin.java index 91f5e37..e9b9caa 100644 --- a/src/android/SocketPlugin.java +++ b/src/android/SocketPlugin.java @@ -2,6 +2,8 @@ import android.annotation.SuppressLint; +import java.io.IOException; + import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -43,6 +45,10 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo this.send(args, callbackContext); return true; + }else if(action.equals("sendBinary")) { + this.sendBinary(args, callbackContext); + return true; + } else if (action.equals("disconnect")) { this.disconnect(args, callbackContext); return true; @@ -192,6 +198,61 @@ private void send(JSONArray args, CallbackContext callbackContext) { } } + + /** + * Send binary information to target host + * + * @param args + * @param callbackContext + */ + private void sendBinary(JSONArray args, CallbackContext callbackContext) { + Connection socket; + + // validating parameters + if (args.length() < 2) { + callbackContext.error("Missing arguments when calling 'sendBinary' action."); + } else { + try { + // retrieving parameters + String key = args.getString(0); + JSONArray jsData = args.getJSONArray(1); + byte[] data = new byte[jsData.length()]; + for (int i=0; i Date: Mon, 30 Jun 2014 21:42:29 +0100 Subject: [PATCH 2/2] Added sendBinary doc to README.md --- README.md | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 39d1a30..de7a354 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ The plugin creates a "Socket" object exposed on window.tlantic.plugins.socket. T * connect: opens a socket connection; * disconnect: closes a socket connection; * disconnectAll: closes ALL opened connections; -* send: send data using a given connection; +* send: send text data using a given connection; +* sendBinary: send binary data using a given connection; * isConnected: returns a boolean falg representing socket connectivity status; * receive: callback used by plugin's native code. Can be override by a custom implementation. @@ -90,7 +91,7 @@ window.tlantic.plugins.socket.connect( ### send (successCallback, errorCallback, connectionId, data) -Sends information and calls success callback if information was send and does not wait for any response. To check how to receive data, please see the item below. +Sends text information and calls success callback if information was sent and does not wait for any response. To check how to receive data, please see the item below. Example: @@ -108,7 +109,29 @@ window.tlantic.plugins.socket.send( ); ``` -### isConnected (connectionId, successCallback, errorCallback) +### sendBinary (successCallback, errorCallback, connectionId, data) + +Sends binary data and calls success callback if information was sent and does not wait for any response. To check how to receive data, please see the item below. + +Binary data to be sent is passed in `data` which is a JSONArray, one integer element per byte. + +Example: + +``` +window.tlantic.plugins.socket.sendBinary( + function () { + console.log('worked!'); + }, + + function () { + console.log('failed!'); + }, + '192.168.2.5:18002', + [ 0x00, 0x01, 0x00, 0xFD ] +); +``` + +#### isConnected (connectionId, successCallback, errorCallback) Returns a boolean value representing the connection status. True, if socket streams are opened and false case else. Both values are supposed to be returned through successCallback. The error callback is called only when facing errors due the check process.