Skip to content

Commit

Permalink
Issue Tlantic#54 - added support for sending binary data.
Browse files Browse the repository at this point in the history
(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 ]
  • Loading branch information
shblythe committed Jun 30, 2014
1 parent 3f3dd45 commit 53d8582
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/android/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,7 @@ public class Connection extends Thread {

private Socket callbackSocket;
private PrintWriter writer;
private OutputStream outputStream;
private BufferedReader reader;

private Boolean mustClose;
Expand Down Expand Up @@ -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()
*/
Expand All @@ -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
Expand Down
63 changes: 62 additions & 1 deletion src/android/SocketPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.annotation.SuppressLint;

import java.io.IOException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<jsData.length(); i++)
data[i]=(byte)jsData.getInt(i);

// getting socket
socket = this.pool.get(key);

// checking if socket was not found and his connectivity
if (socket == null) {
callbackContext.error("No connection found with host " + key);

} else if (!socket.isConnected()) {
callbackContext.error("Invalid connection with host " + key);

} else if (data.length == 0) {
callbackContext.error("Cannot send empty data to " + key);

} else {

try {
// write on output stream
socket.writeBinary(data);

// ending send process
callbackContext.success();

} catch (IOException e) {
callbackContext.error("I/O error sending to " + key);
}
}

} catch (JSONException e) {
callbackContext.error("Unexpected error sending information: " + e.getMessage());
}
}
}

/**
* Closes an existing connection
*
Expand Down Expand Up @@ -283,4 +344,4 @@ public void run() {
});
}

}
}
6 changes: 6 additions & 0 deletions www/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Socket.prototype.send = function (successCallback, errorCallback, connectionId,
exec(successCallback, errorCallback, this.pluginRef, 'send', [connectionId, typeof data == 'string' ? data : JSON.stringify(data)]);
};

///
Socket.prototype.sendBinary = function (successCallback, errorCallback, connectionId, data) {
'use strict';
exec(successCallback, errorCallback, this.pluginRef, 'sendBinary', [connectionId, data]);
};

//
Socket.prototype.receive = function (host, port, connectionId, chunk) {
'use strict';
Expand Down

0 comments on commit 53d8582

Please sign in to comment.