Applied API refactoring
This commit is contained in:
commit
afd5f2d36b
47
README.md
47
README.md
@ -2,3 +2,50 @@ sockets-for-cordova
|
|||||||
===================
|
===================
|
||||||
|
|
||||||
Cordova plugin for socket network communication
|
Cordova plugin for socket network communication
|
||||||
|
|
||||||
|
## API
|
||||||
|
### Event handlers
|
||||||
|
#### `onData: (data: Uint8Array) => void`
|
||||||
|
Called when new batch of data was send from server to client. Data are represented as typed array of bytes (`Uint8Array`).
|
||||||
|
|
||||||
|
#### `onClose: (hasError: boolean) => void`
|
||||||
|
Called after connection was successfully closed. Parameter `hasError` indicates whether connection was closed as a result of some error.
|
||||||
|
|
||||||
|
#### `onError: (message: string) => void`
|
||||||
|
Called when some error occurs on opened connection.
|
||||||
|
|
||||||
|
### Methods
|
||||||
|
#### `open(host, port, onSuccess?, onError?): void`
|
||||||
|
Establishes connection with the remote host.
|
||||||
|
|
||||||
|
| parameter | type | description |
|
||||||
|
| ----------- |-----------------------------|--------------|
|
||||||
|
| `host` | `string` | Remote host/ip address |
|
||||||
|
| `port` | `number` | Tcp port number |
|
||||||
|
| `onSuccess` | `() => void` | Success callback, called after successfull connection to the remote host. (optional)|
|
||||||
|
| `onError` | `(message: string) => void` | Error callback, called when some error occurs during connecting to the remote host. (optional)|
|
||||||
|
|
||||||
|
#### `write(data, onSuccess?, onError?): void`
|
||||||
|
Sends data to the remote host.
|
||||||
|
|
||||||
|
| parameter | type | description |
|
||||||
|
| ----------- |-----------------------------|--------------|
|
||||||
|
| `data` | `Uint8Array` | Typed array of bytes to be written to the output stream |
|
||||||
|
| `onSuccess` | `() => void` | Success callback, called after data are successfully written to the output stream. (optional)|
|
||||||
|
| `onError` | `(message: string) => void` | Error callback, called when some error occurs during writing of data to the output stream. (optional)|
|
||||||
|
|
||||||
|
#### `shutdownWrite(onSuccess?, onError?): void`
|
||||||
|
Finishes sending of data by sending `FIN` to remote host. If you call `write` after invoking `shutdownWrite`, callback `onError` (of `write` method) will be called.
|
||||||
|
|
||||||
|
| parameter | type | description |
|
||||||
|
| ----------- |-----------------------------|--------------|
|
||||||
|
| `onSuccess` | `() => void` | Success callback, called after sending of all data is finished. (optional)|
|
||||||
|
| `onError` | `(message: string) => void` | Error callback, called when some error occurs during this procedure. (optional)|
|
||||||
|
|
||||||
|
#### `close(onSuccess?, onError?): void`
|
||||||
|
Closes the connection. `onClose` event handler is called when closing of socket was succesfull.
|
||||||
|
|
||||||
|
| parameter | type | description |
|
||||||
|
| ----------- |-----------------------------|--------------|
|
||||||
|
| `onSuccess` | `() => void` | Success callback, called after connection was successfully closed. `onClose` event handler is called before that callback. (optional)|
|
||||||
|
| `onError` | `(message: string) => void` | Error callback, called when some error occurs during this procedure. (optional)|
|
||||||
|
66
socket.js
66
socket.js
@ -10,31 +10,30 @@ function Socket() {
|
|||||||
this.socketKey = guid();
|
this.socketKey = guid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket.create = function(callback) {
|
Socket.prototype.open = function (host, port, success, error) {
|
||||||
|
|
||||||
var socket = new Socket();
|
|
||||||
|
|
||||||
|
var _that = this;
|
||||||
function socketEventHandler(event) {
|
function socketEventHandler(event) {
|
||||||
|
|
||||||
var payload = event.payload;
|
var payload = event.payload;
|
||||||
|
|
||||||
if (payload.socketKey !== socket.socketKey) {
|
if (payload.socketKey !== _that.socketKey) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(payload.type) {
|
switch(payload.type) {
|
||||||
case "Close":
|
case "Close":
|
||||||
console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey);
|
//console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey);
|
||||||
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
socket.onClose();
|
_that.onClose();
|
||||||
break;
|
break;
|
||||||
case "DataReceived":
|
case "DataReceived":
|
||||||
console.debug("SocketsForCordova: DataReceived event, socket key: " + payload.socketKey);
|
//console.debug("SocketsForCordova: DataReceived event, socket key: " + payload.socketKey);
|
||||||
socket.onData(new Int8Array(payload.data));
|
_that.onData(new Uint8Array(payload.data));
|
||||||
break;
|
break;
|
||||||
case "Error":
|
case "Error":
|
||||||
console.debug("SocketsForCordova: Error event, socket key: " + payload.socketKey);
|
//console.debug("SocketsForCordova: Error event, socket key: " + payload.socketKey);
|
||||||
socket.onError(payload.errorMessage);
|
_that.onError(payload.errorMessage);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error("SocketsForCordova: Unknown event type " + payload.type + ", socket key: " + payload.socketKey);
|
console.error("SocketsForCordova: Unknown event type " + payload.type + ", socket key: " + payload.socketKey);
|
||||||
@ -42,52 +41,37 @@ Socket.create = function(callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
|
||||||
|
|
||||||
exec(
|
exec(
|
||||||
function() {
|
function() {
|
||||||
console.debug("SocketsForCordova: Socket object successfully constructed.");
|
//console.debug("SocketsForCordova: Socket successfully opened.");
|
||||||
callback(socket);
|
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
},
|
|
||||||
function(error) {
|
|
||||||
console.error("SocketsForCordova: Unexpected error during constructing Socket object. Error: " + error);
|
|
||||||
},
|
|
||||||
CORDOVA_SERVICE_NAME,
|
|
||||||
"create",
|
|
||||||
[ socket.socketKey ]);
|
|
||||||
};
|
|
||||||
|
|
||||||
Socket.prototype.connect = function (host, port, success, error) {
|
|
||||||
exec(
|
|
||||||
function() {
|
|
||||||
console.debug("SocketsForCordova: Socket successfully connected.");
|
|
||||||
if (success)
|
if (success)
|
||||||
success();
|
success();
|
||||||
},
|
},
|
||||||
function(errorMessage) {
|
function(errorMessage) {
|
||||||
console.error("SocketsForCordova: Error during socket connecting. Error: " + errorMessage);
|
//console.error("SocketsForCordova: Error during opening socket. Error: " + errorMessage);
|
||||||
if (error)
|
if (error)
|
||||||
error(errorMessage);
|
error(errorMessage);
|
||||||
},
|
},
|
||||||
CORDOVA_SERVICE_NAME,
|
CORDOVA_SERVICE_NAME,
|
||||||
"connect",
|
"open",
|
||||||
[ this.socketKey, host, port ]);
|
[ this.socketKey, host, port ]);
|
||||||
};
|
};
|
||||||
|
|
||||||
Socket.prototype.write = function (data, success, error) {
|
Socket.prototype.write = function (data, success, error) {
|
||||||
|
|
||||||
var dataToWrite = data instanceof Int8Array
|
var dataToWrite = data instanceof Uint8Array
|
||||||
? Socket._copyToArray(data)
|
? Socket._copyToArray(data)
|
||||||
: data;
|
: data;
|
||||||
|
|
||||||
exec(
|
exec(
|
||||||
function() {
|
function() {
|
||||||
console.debug("SocketsForCordova: Data successfully written to socket. Number of bytes: " + data.length);
|
//console.debug("SocketsForCordova: Data successfully written to socket. Number of bytes: " + data.length);
|
||||||
if (success)
|
if (success)
|
||||||
success();
|
success();
|
||||||
},
|
},
|
||||||
function(errorMessage) {
|
function(errorMessage) {
|
||||||
console.error("SocketsForCordova: Error during writing data to socket. Error: " + errorMessage);
|
//console.error("SocketsForCordova: Error during writing data to socket. Error: " + errorMessage);
|
||||||
if (error)
|
if (error)
|
||||||
error(errorMessage);
|
error(errorMessage);
|
||||||
},
|
},
|
||||||
@ -104,13 +88,17 @@ Socket._copyToArray = function(array) {
|
|||||||
return outputArray;
|
return outputArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
Socket.prototype.shutdownWrite = function () {
|
Socket.prototype.shutdownWrite = function (success, error) {
|
||||||
exec(
|
exec(
|
||||||
function() {
|
function() {
|
||||||
console.debug("SocketsForCordova: Shutdown write successfully called.");
|
//console.debug("SocketsForCordova: Shutdown write successfully called.");
|
||||||
|
if (success)
|
||||||
|
success();
|
||||||
},
|
},
|
||||||
function(errorMessage) {
|
function(errorMessage) {
|
||||||
console.error("SocketsForCordova: Error when call shutdownWrite on socket. Error: " + errorMessage);
|
//console.error("SocketsForCordova: Error when call shutdownWrite on socket. Error: " + errorMessage);
|
||||||
|
if (error)
|
||||||
|
error(errorMessage);
|
||||||
},
|
},
|
||||||
CORDOVA_SERVICE_NAME,
|
CORDOVA_SERVICE_NAME,
|
||||||
"shutdownWrite",
|
"shutdownWrite",
|
||||||
@ -120,10 +108,14 @@ Socket.prototype.shutdownWrite = function () {
|
|||||||
Socket.prototype.close = function () {
|
Socket.prototype.close = function () {
|
||||||
exec(
|
exec(
|
||||||
function() {
|
function() {
|
||||||
console.debug("SocketsForCordova: Close successfully called.");
|
//console.debug("SocketsForCordova: Close successfully called.");
|
||||||
|
if (success)
|
||||||
|
success();
|
||||||
},
|
},
|
||||||
function(errorMessage) {
|
function(errorMessage) {
|
||||||
console.error("SocketsForCordova: Error when call close on socket. Error: " + errorMessage);
|
//console.error("SocketsForCordova: Error when call close on socket. Error: " + errorMessage);
|
||||||
|
if (error)
|
||||||
|
error(errorMessage);
|
||||||
},
|
},
|
||||||
CORDOVA_SERVICE_NAME,
|
CORDOVA_SERVICE_NAME,
|
||||||
"close",
|
"close",
|
||||||
|
@ -5,11 +5,12 @@ import java.net.SocketException;
|
|||||||
|
|
||||||
|
|
||||||
public interface SocketAdapter {
|
public interface SocketAdapter {
|
||||||
public void connect(String host, int port) throws Throwable;
|
public void open(String host, int port) throws Throwable;
|
||||||
public void write(byte[] data) throws IOException;
|
public void write(byte[] data) throws IOException;
|
||||||
|
public void shutdownWrite() throws IOException;
|
||||||
public void close() throws IOException;
|
public void close() throws IOException;
|
||||||
public void setOptions(SocketAdapterOptions options) throws SocketException;
|
public void setOptions(SocketAdapterOptions options) throws SocketException;
|
||||||
public void setDataConsumer(Consumer<byte[]> dataConsumer);
|
public void setDataConsumer(Consumer<byte[]> dataConsumer);
|
||||||
public void setCloseEventHandler(Consumer<Boolean> closeEventHandler);
|
public void setCloseEventHandler(Consumer<Boolean> closeEventHandler);
|
||||||
public void setErrorHandler(Consumer<Throwable> errorHandler);
|
public void setErrorHandler(Consumer<String> errorHandler);
|
||||||
}
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package cz.blocshop.socketsforcordova;
|
package cz.blocshop.socketsforcordova;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InvalidObjectException;
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
@ -19,7 +18,7 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
|
|
||||||
private Consumer<byte[]> dataConsumer;
|
private Consumer<byte[]> dataConsumer;
|
||||||
private Consumer<Boolean> closeEventHandler;
|
private Consumer<Boolean> closeEventHandler;
|
||||||
private Consumer<Throwable> exceptionHandler;
|
private Consumer<String> exceptionHandler;
|
||||||
|
|
||||||
private ExecutorService executor;
|
private ExecutorService executor;
|
||||||
|
|
||||||
@ -29,8 +28,8 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect(String host, int port) throws Throwable {
|
public void open(String host, int port) throws Throwable {
|
||||||
this.connectSocket(host, port);
|
this.openWithBackgroundThread(host, port);
|
||||||
this.submitReadTask();
|
this.submitReadTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,11 +38,15 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
this.socket.getOutputStream().write(data);
|
this.socket.getOutputStream().write(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdownWrite() throws IOException {
|
||||||
|
this.socket.shutdownOutput();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if (!this.socket.isClosed()) {
|
this.invokeCloseEventHandler(false);
|
||||||
this.socket.shutdownOutput();
|
this.socket.close();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,24 +85,25 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setErrorHandler(Consumer<Throwable> exceptionHandler) {
|
public void setErrorHandler(Consumer<String> exceptionHandler) {
|
||||||
this.exceptionHandler = exceptionHandler;
|
this.exceptionHandler = exceptionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectSocket(final String host, final int port) throws Throwable {
|
private void openWithBackgroundThread(final String host, final int port) throws Throwable {
|
||||||
Future future = this.executor.submit(new Runnable() {
|
Future<?> future = this.executor.submit(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
socket.connect(new InetSocketAddress(host, port));
|
socket.connect(new InetSocketAddress(host, port));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e);
|
Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e.getCause());
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Object result = future.get();
|
future.get();
|
||||||
}
|
}
|
||||||
catch (ExecutionException e) {
|
catch (ExecutionException e) {
|
||||||
throw e.getCause();
|
throw e.getCause();
|
||||||
@ -122,7 +126,7 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Logging.Error(SocketAdapterImpl.class.getName(), "Error during reading of socket input stream", e);
|
Logging.Error(SocketAdapterImpl.class.getName(), "Error during reading of socket input stream", e);
|
||||||
hasError = true;
|
hasError = true;
|
||||||
invokeExceptionHandler(e);
|
invokeExceptionHandler(e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
@ -159,9 +163,9 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void invokeExceptionHandler(Throwable exception) {
|
private void invokeExceptionHandler(String errorMessage) {
|
||||||
if (this.exceptionHandler != null) {
|
if (this.exceptionHandler != null) {
|
||||||
this.exceptionHandler.accept(exception);
|
this.exceptionHandler.accept(errorMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
package cz.blocshop.socketsforcordova;
|
package cz.blocshop.socketsforcordova;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.cordova.CallbackContext;
|
import org.apache.cordova.CallbackContext;
|
||||||
import org.apache.cordova.CordovaArgs;
|
import org.apache.cordova.CordovaArgs;
|
||||||
import org.apache.cordova.CordovaPlugin;
|
import org.apache.cordova.CordovaPlugin;
|
||||||
@ -15,6 +13,7 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
|
||||||
public class SocketPlugin extends CordovaPlugin {
|
public class SocketPlugin extends CordovaPlugin {
|
||||||
|
|
||||||
@ -23,12 +22,12 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
|
|
||||||
if(action.equals("create")) {
|
if (action.equals("open")) {
|
||||||
this.create(args, callbackContext);
|
this.open(args, callbackContext);
|
||||||
} else if (action.equals("connect")) {
|
|
||||||
this.connect(args, callbackContext);
|
|
||||||
} else if (action.equals("write")) {
|
} else if (action.equals("write")) {
|
||||||
this.write(args, callbackContext);
|
this.write(args, callbackContext);
|
||||||
|
} else if (action.equals("shutdownWrite")) {
|
||||||
|
this.shutdownWrite(args, callbackContext);
|
||||||
} else if (action.equals("close")) {
|
} else if (action.equals("close")) {
|
||||||
this.close(args, callbackContext);
|
this.close(args, callbackContext);
|
||||||
} else if (action.equals("setOptions")) {
|
} else if (action.equals("setOptions")) {
|
||||||
@ -40,29 +39,19 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void create(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
private void open(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
|
String socketKey = args.getString(0);
|
||||||
final String socketKey = args.getString(0);
|
String host = args.getString(1);
|
||||||
|
int port = args.getInt(2);
|
||||||
|
|
||||||
SocketAdapter socketAdapter = new SocketAdapterImpl();
|
SocketAdapter socketAdapter = new SocketAdapterImpl();
|
||||||
socketAdapter.setCloseEventHandler(new CloseEventHandler(socketKey));
|
socketAdapter.setCloseEventHandler(new CloseEventHandler(socketKey));
|
||||||
socketAdapter.setDataConsumer(new DataConsumer(socketKey));
|
socketAdapter.setDataConsumer(new DataConsumer(socketKey));
|
||||||
socketAdapter.setErrorHandler(new ErrorHandler(socketKey));
|
socketAdapter.setErrorHandler(new ErrorHandler(socketKey));
|
||||||
|
|
||||||
this.socketAdapters.put(socketKey, socketAdapter);
|
|
||||||
|
|
||||||
callbackContext.success(socketKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void connect(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
|
||||||
String socketKey = args.getString(0);
|
|
||||||
String host = args.getString(1);
|
|
||||||
int port = args.getInt(2);
|
|
||||||
|
|
||||||
SocketAdapter socket = this.getSocketAdapter(socketKey);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket.connect(host, port);
|
socketAdapter.open(host, port);
|
||||||
|
this.socketAdapters.put(socketKey, socketAdapter);
|
||||||
callbackContext.success();
|
callbackContext.success();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
callbackContext.error(t.toString());
|
callbackContext.error(t.toString());
|
||||||
@ -88,6 +77,19 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void shutdownWrite(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
|
String socketKey = args.getString(0);
|
||||||
|
|
||||||
|
SocketAdapter socket = this.getSocketAdapter(socketKey);
|
||||||
|
|
||||||
|
try {
|
||||||
|
socket.shutdownWrite();
|
||||||
|
callbackContext.success();
|
||||||
|
} catch (IOException e) {
|
||||||
|
callbackContext.error(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void close(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
private void close(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
String socketKey = args.getString(0);
|
String socketKey = args.getString(0);
|
||||||
|
|
||||||
@ -135,8 +137,7 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
|
|
||||||
private SocketAdapter getSocketAdapter(String socketKey) {
|
private SocketAdapter getSocketAdapter(String socketKey) {
|
||||||
if (!this.socketAdapters.containsKey(socketKey)) {
|
if (!this.socketAdapters.containsKey(socketKey)) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalStateException("Socket isn't connected.");
|
||||||
String.format("Cannot find socketKey: %s. Connection is probably closed.", socketKey));
|
|
||||||
}
|
}
|
||||||
return this.socketAdapters.get(socketKey);
|
return this.socketAdapters.get(socketKey);
|
||||||
}
|
}
|
||||||
@ -197,17 +198,17 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ErrorHandler implements Consumer<Throwable> {
|
private class ErrorHandler implements Consumer<String> {
|
||||||
private String socketKey;
|
private String socketKey;
|
||||||
public ErrorHandler(String socketKey) {
|
public ErrorHandler(String socketKey) {
|
||||||
this.socketKey = socketKey;
|
this.socketKey = socketKey;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void accept(Throwable exception) {
|
public void accept(String errorMessage) {
|
||||||
try {
|
try {
|
||||||
JSONObject event = new JSONObject();
|
JSONObject event = new JSONObject();
|
||||||
event.put("type", "Error");
|
event.put("type", "Error");
|
||||||
event.put("errorMessage", exception.toString());
|
event.put("errorMessage", errorMessage);
|
||||||
event.put("socketKey", socketKey);
|
event.put("socketKey", socketKey);
|
||||||
|
|
||||||
dispatchEvent(event);
|
dispatchEvent(event);
|
||||||
|
@ -37,7 +37,6 @@ BOOL wasOpenned = FALSE;
|
|||||||
[inputStream open];
|
[inputStream open];
|
||||||
|
|
||||||
outputStream = (__bridge NSOutputStream *)writeStream;
|
outputStream = (__bridge NSOutputStream *)writeStream;
|
||||||
//[outputStream setDelegate:self];
|
|
||||||
[outputStream open];
|
[outputStream open];
|
||||||
|
|
||||||
[self performSelectorOnMainThread:@selector(runReadLoop) withObject:nil waitUntilDone:NO];
|
[self performSelectorOnMainThread:@selector(runReadLoop) withObject:nil waitUntilDone:NO];
|
||||||
@ -125,13 +124,11 @@ BOOL wasOpenned = FALSE;
|
|||||||
case NSStreamEventHasBytesAvailable: {
|
case NSStreamEventHasBytesAvailable: {
|
||||||
if(stream == inputStream) {
|
if(stream == inputStream) {
|
||||||
uint8_t buf[65535];
|
uint8_t buf[65535];
|
||||||
unsigned int len = 0;
|
long len = [inputStream read:buf maxLength:65535];
|
||||||
|
|
||||||
len = [inputStream read:buf maxLength:65535];
|
|
||||||
NSLog(@"%d", len);
|
|
||||||
if(len > 0) {
|
if(len > 0) {
|
||||||
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
|
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
|
||||||
for (int i = 0; i < len; i++) {
|
for (long i = 0; i < len; i++) {
|
||||||
|
|
||||||
[dataArray addObject:[NSNumber numberWithUnsignedChar:buf[i]]];
|
[dataArray addObject:[NSNumber numberWithUnsignedChar:buf[i]]];
|
||||||
}
|
}
|
||||||
@ -184,6 +181,7 @@ BOOL wasOpenned = FALSE;
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)close {
|
- (void)close {
|
||||||
|
self.closeEventHandler(FALSE);
|
||||||
[self closeOutputStream];
|
[self closeOutputStream];
|
||||||
[self closeInputStream];
|
[self closeInputStream];
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
NSMutableDictionary *socketAdapters;
|
NSMutableDictionary *socketAdapters;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void) create: (CDVInvokedUrlCommand *) command;
|
|
||||||
-(void) connect: (CDVInvokedUrlCommand *) command;
|
-(void) connect: (CDVInvokedUrlCommand *) command;
|
||||||
-(void) write: (CDVInvokedUrlCommand *) command;
|
-(void) write: (CDVInvokedUrlCommand *) command;
|
||||||
-(void) close: (CDVInvokedUrlCommand *) command;
|
-(void) close: (CDVInvokedUrlCommand *) command;
|
||||||
|
@ -5,15 +5,6 @@
|
|||||||
|
|
||||||
@implementation SocketPlugin : CDVPlugin
|
@implementation SocketPlugin : CDVPlugin
|
||||||
|
|
||||||
- (void) create : (CDVInvokedUrlCommand*) command {
|
|
||||||
//NSString* socketKey = [command.arguments objectAtIndex : 0];
|
|
||||||
|
|
||||||
|
|
||||||
[self.commandDelegate
|
|
||||||
sendPluginResult: [CDVPluginResult resultWithStatus : CDVCommandStatus_OK]
|
|
||||||
callbackId: command.callbackId];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) connect : (CDVInvokedUrlCommand*) command {
|
- (void) connect : (CDVInvokedUrlCommand*) command {
|
||||||
|
|
||||||
NSString *socketKey = [command.arguments objectAtIndex:0];
|
NSString *socketKey = [command.arguments objectAtIndex:0];
|
||||||
|
@ -12,6 +12,7 @@ namespace Blocshop.ScoketsForCordova
|
|||||||
{
|
{
|
||||||
Task Connect(String host, int port);
|
Task Connect(String host, int port);
|
||||||
Task Write(byte[] data);
|
Task Write(byte[] data);
|
||||||
|
void ShutdownWrite();
|
||||||
void Close();
|
void Close();
|
||||||
SocketAdapterOptions Options { set; }
|
SocketAdapterOptions Options { set; }
|
||||||
Action<byte[]> DataConsumer { set; }
|
Action<byte[]> DataConsumer { set; }
|
||||||
@ -55,11 +56,17 @@ namespace Blocshop.ScoketsForCordova
|
|||||||
await this.socket.SendTaskAsync(socketAsyncEventArgs);
|
await this.socket.SendTaskAsync(socketAsyncEventArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Close()
|
public void ShutdownWrite()
|
||||||
{
|
{
|
||||||
this.socket.Shutdown(SocketShutdown.Send);
|
this.socket.Shutdown(SocketShutdown.Send);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
this.CloseEventHandler(false);
|
||||||
|
this.socket.Close();
|
||||||
|
}
|
||||||
|
|
||||||
private void StartReadTask()
|
private void StartReadTask()
|
||||||
{
|
{
|
||||||
Task.Factory.StartNew(() => this.RunRead());
|
Task.Factory.StartNew(() => this.RunRead());
|
||||||
|
@ -21,54 +21,36 @@ namespace Blocshop.ScoketsForCordova
|
|||||||
this.socketStorage = SocketStorage.CreateSocketStorage();
|
this.socketStorage = SocketStorage.CreateSocketStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create(string parameters)
|
|
||||||
{
|
|
||||||
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
|
|
||||||
|
|
||||||
ISocketAdapter socketAdapter = new SocketAdapter();
|
|
||||||
socketAdapter.CloseEventHandler = (hasError) => this.CloseEventHandler(socketKey, hasError);
|
|
||||||
socketAdapter.DataConsumer = (data) => this.DataConsumer(socketKey, data);
|
|
||||||
socketAdapter.ErrorHandler = (ex) => this.ErrorHandler(socketKey, ex);
|
|
||||||
|
|
||||||
this.socketStorage.Add(socketKey, socketAdapter);
|
|
||||||
|
|
||||||
this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerWPEventDispatcher(string parameters)
|
|
||||||
{
|
|
||||||
this.eventDispatcherCallbackId = this.CurrentCommandCallbackId;
|
|
||||||
PluginResult result = new PluginResult(PluginResult.Status.OK);
|
|
||||||
result.KeepCallback = true;
|
|
||||||
DispatchCommandResult(result, this.eventDispatcherCallbackId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connect(string parameters)
|
public void connect(string parameters)
|
||||||
{
|
{
|
||||||
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
|
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
|
||||||
string host = JsonHelper.Deserialize<string[]>(parameters)[1];
|
string host = JsonHelper.Deserialize<string[]>(parameters)[1];
|
||||||
int port = int.Parse(JsonHelper.Deserialize<string[]>(parameters)[2]);
|
int port = int.Parse(JsonHelper.Deserialize<string[]>(parameters)[2]);
|
||||||
|
|
||||||
ISocketAdapter socket = this.socketStorage.Get(socketKey);
|
ISocketAdapter socketAdapter = new SocketAdapter();
|
||||||
|
socketAdapter.CloseEventHandler = (hasError) => this.CloseEventHandler(socketKey, hasError);
|
||||||
|
socketAdapter.DataConsumer = (data) => this.DataConsumer(socketKey, data);
|
||||||
|
socketAdapter.ErrorHandler = (ex) => this.ErrorHandler(socketKey, ex);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
socket.Connect(host, port).Wait();
|
socketAdapter.Connect(host, port).Wait();
|
||||||
|
|
||||||
|
this.socketStorage.Add(socketKey, socketAdapter);
|
||||||
|
|
||||||
this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
|
this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
|
||||||
}
|
}
|
||||||
catch (SocketException ex)
|
catch (SocketException ex)
|
||||||
{
|
{
|
||||||
this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.Message));
|
this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.Message));
|
||||||
socketStorage.Remove(socketKey);
|
|
||||||
}
|
}
|
||||||
catch (AggregateException ex)
|
catch (AggregateException ex)
|
||||||
{
|
{
|
||||||
this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.InnerException.Message));
|
this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.InnerException.Message));
|
||||||
socketStorage.Remove(socketKey);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(string parameters/*, string socketKey, byte[] data*/)
|
public void write(string parameters)
|
||||||
{
|
{
|
||||||
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
|
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
|
||||||
string dataJsonArray = JsonHelper.Deserialize<string[]>(parameters)[1];
|
string dataJsonArray = JsonHelper.Deserialize<string[]>(parameters)[1];
|
||||||
@ -87,6 +69,15 @@ namespace Blocshop.ScoketsForCordova
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void shutdownWrite(string parameters)
|
||||||
|
{
|
||||||
|
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
|
||||||
|
|
||||||
|
ISocketAdapter socket = this.socketStorage.Get(socketKey);
|
||||||
|
|
||||||
|
socket.ShutdownWrite();
|
||||||
|
}
|
||||||
|
|
||||||
public void close(string parameters)
|
public void close(string parameters)
|
||||||
{
|
{
|
||||||
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
|
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
|
||||||
@ -96,6 +87,14 @@ namespace Blocshop.ScoketsForCordova
|
|||||||
socket.Close();
|
socket.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void registerWPEventDispatcher(string parameters)
|
||||||
|
{
|
||||||
|
this.eventDispatcherCallbackId = this.CurrentCommandCallbackId;
|
||||||
|
PluginResult result = new PluginResult(PluginResult.Status.OK);
|
||||||
|
result.KeepCallback = true;
|
||||||
|
DispatchCommandResult(result, this.eventDispatcherCallbackId);
|
||||||
|
}
|
||||||
|
|
||||||
//private void setOptions(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
//private void setOptions(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
|
||||||
|
|
||||||
// String socketKey = args.getString(0);
|
// String socketKey = args.getString(0);
|
||||||
|
Loading…
Reference in New Issue
Block a user