Fix for Android older than 4.4
This commit is contained in:
parent
6bd83b9fc4
commit
813e44083a
@ -5,11 +5,11 @@ import java.net.SocketException;
|
||||
|
||||
|
||||
public interface SocketAdapter {
|
||||
public void connect(String host, int port) throws IOException;
|
||||
public void connect(String host, int port) throws Throwable;
|
||||
public void write(byte[] data) throws IOException;
|
||||
public void close() throws IOException;
|
||||
public void setOptions(SocketAdapterOptions options) throws SocketException;
|
||||
public void setDataConsumer(Consumer<byte[]> dataConsumer);
|
||||
public void setCloseEventHandler(Consumer<Boolean> closeEventHandler);
|
||||
public void setErrorHandler(Consumer<IOException> errorHandler);
|
||||
public void setErrorHandler(Consumer<Throwable> errorHandler);
|
||||
}
|
@ -1,12 +1,15 @@
|
||||
package cz.blocshop.socketsforcordova;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InvalidObjectException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
|
||||
public class SocketAdapterImpl implements SocketAdapter {
|
||||
@ -16,15 +19,18 @@ public class SocketAdapterImpl implements SocketAdapter {
|
||||
|
||||
private Consumer<byte[]> dataConsumer;
|
||||
private Consumer<Boolean> closeEventHandler;
|
||||
private Consumer<IOException> exceptionHandler;
|
||||
private Consumer<Throwable> exceptionHandler;
|
||||
|
||||
private ExecutorService executor;
|
||||
|
||||
public SocketAdapterImpl() {
|
||||
this.socket = new Socket();
|
||||
this.executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect(String host, int port) throws IOException {
|
||||
this.socket.connect(new InetSocketAddress(host, port));
|
||||
public void connect(String host, int port) throws Throwable {
|
||||
this.connectSocket(host, port);
|
||||
this.submitReadTask();
|
||||
}
|
||||
|
||||
@ -76,13 +82,32 @@ public class SocketAdapterImpl implements SocketAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorHandler(Consumer<IOException> exceptionHandler) {
|
||||
public void setErrorHandler(Consumer<Throwable> exceptionHandler) {
|
||||
this.exceptionHandler = exceptionHandler;
|
||||
}
|
||||
|
||||
private void connectSocket(final String host, final int port) throws Throwable {
|
||||
Future future = this.executor.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
socket.connect(new InetSocketAddress(host, port));
|
||||
} catch (IOException e) {
|
||||
Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
Object result = future.get();
|
||||
}
|
||||
catch (ExecutionException e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
}
|
||||
|
||||
private void submitReadTask() {
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
executor.submit(new Runnable() {
|
||||
this.executor.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
runRead();
|
||||
@ -94,7 +119,7 @@ public class SocketAdapterImpl implements SocketAdapter {
|
||||
boolean hasError = false;
|
||||
try {
|
||||
runReadLoop();
|
||||
} catch (IOException e) {
|
||||
} catch (Throwable e) {
|
||||
Logging.Error(SocketAdapterImpl.class.getName(), "Error during reading of socket input stream", e);
|
||||
hasError = true;
|
||||
invokeExceptionHandler(e);
|
||||
@ -134,7 +159,7 @@ public class SocketAdapterImpl implements SocketAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private void invokeExceptionHandler(IOException exception) {
|
||||
private void invokeExceptionHandler(Throwable exception) {
|
||||
if (this.exceptionHandler != null) {
|
||||
this.exceptionHandler.accept(exception);
|
||||
}
|
||||
|
@ -2,7 +2,11 @@ package cz.blocshop.socketsforcordova;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaArgs;
|
||||
@ -60,8 +64,8 @@ public class SocketPlugin extends CordovaPlugin {
|
||||
try {
|
||||
socket.connect(host, port);
|
||||
callbackContext.success();
|
||||
} catch (IOException e) {
|
||||
callbackContext.error(e.toString());
|
||||
} catch (Throwable t) {
|
||||
callbackContext.error(t.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -174,7 +178,8 @@ public class SocketPlugin extends CordovaPlugin {
|
||||
try {
|
||||
JSONObject event = new JSONObject();
|
||||
event.put("type", "DataReceived");
|
||||
event.put("data", new JSONArray(data));
|
||||
//event.put("data", new JSONArray(data)); NOT SUPPORTED IN API LEVEL LESS THAN 19
|
||||
event.put("data", new JSONArray(this.toByteList(data)));
|
||||
event.put("socketKey", socketKey);
|
||||
|
||||
dispatchEvent(event);
|
||||
@ -182,15 +187,23 @@ public class SocketPlugin extends CordovaPlugin {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private List<Byte> toByteList(byte[] array) {
|
||||
List<Byte> byteList = new ArrayList<Byte>(array.length);
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
byteList.add(array[i]);
|
||||
}
|
||||
return byteList;
|
||||
}
|
||||
}
|
||||
|
||||
private class ErrorHandler implements Consumer<IOException> {
|
||||
private class ErrorHandler implements Consumer<Throwable> {
|
||||
private String socketKey;
|
||||
public ErrorHandler(String socketKey) {
|
||||
this.socketKey = socketKey;
|
||||
}
|
||||
@Override
|
||||
public void accept(IOException exception) {
|
||||
public void accept(Throwable exception) {
|
||||
try {
|
||||
JSONObject event = new JSONObject();
|
||||
event.put("type", "Error");
|
||||
|
Loading…
Reference in New Issue
Block a user