Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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:

Expand All @@ -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.
Expand Down
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