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 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 write(byte[] data) 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<IOException> errorHandler);
|
public void setErrorHandler(Consumer<Throwable> errorHandler);
|
||||||
}
|
}
|
@ -1,12 +1,15 @@
|
|||||||
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;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
|
||||||
public class SocketAdapterImpl implements SocketAdapter {
|
public class SocketAdapterImpl implements SocketAdapter {
|
||||||
@ -16,15 +19,18 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
|
|
||||||
private Consumer<byte[]> dataConsumer;
|
private Consumer<byte[]> dataConsumer;
|
||||||
private Consumer<Boolean> closeEventHandler;
|
private Consumer<Boolean> closeEventHandler;
|
||||||
private Consumer<IOException> exceptionHandler;
|
private Consumer<Throwable> exceptionHandler;
|
||||||
|
|
||||||
|
private ExecutorService executor;
|
||||||
|
|
||||||
public SocketAdapterImpl() {
|
public SocketAdapterImpl() {
|
||||||
this.socket = new Socket();
|
this.socket = new Socket();
|
||||||
|
this.executor = Executors.newSingleThreadExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect(String host, int port) throws IOException {
|
public void connect(String host, int port) throws Throwable {
|
||||||
this.socket.connect(new InetSocketAddress(host, port));
|
this.connectSocket(host, port);
|
||||||
this.submitReadTask();
|
this.submitReadTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,13 +82,32 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setErrorHandler(Consumer<IOException> exceptionHandler) {
|
public void setErrorHandler(Consumer<Throwable> exceptionHandler) {
|
||||||
this.exceptionHandler = 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() {
|
private void submitReadTask() {
|
||||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
this.executor.submit(new Runnable() {
|
||||||
executor.submit(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
runRead();
|
runRead();
|
||||||
@ -94,7 +119,7 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
boolean hasError = false;
|
boolean hasError = false;
|
||||||
try {
|
try {
|
||||||
runReadLoop();
|
runReadLoop();
|
||||||
} catch (IOException 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);
|
||||||
@ -134,7 +159,7 @@ public class SocketAdapterImpl implements SocketAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void invokeExceptionHandler(IOException exception) {
|
private void invokeExceptionHandler(Throwable exception) {
|
||||||
if (this.exceptionHandler != null) {
|
if (this.exceptionHandler != null) {
|
||||||
this.exceptionHandler.accept(exception);
|
this.exceptionHandler.accept(exception);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,11 @@ package cz.blocshop.socketsforcordova;
|
|||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
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.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;
|
||||||
@ -60,8 +64,8 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
try {
|
try {
|
||||||
socket.connect(host, port);
|
socket.connect(host, port);
|
||||||
callbackContext.success();
|
callbackContext.success();
|
||||||
} catch (IOException e) {
|
} catch (Throwable t) {
|
||||||
callbackContext.error(e.toString());
|
callbackContext.error(t.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +178,8 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
try {
|
try {
|
||||||
JSONObject event = new JSONObject();
|
JSONObject event = new JSONObject();
|
||||||
event.put("type", "DataReceived");
|
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);
|
event.put("socketKey", socketKey);
|
||||||
|
|
||||||
dispatchEvent(event);
|
dispatchEvent(event);
|
||||||
@ -182,15 +187,23 @@ public class SocketPlugin extends CordovaPlugin {
|
|||||||
e.printStackTrace();
|
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;
|
private String socketKey;
|
||||||
public ErrorHandler(String socketKey) {
|
public ErrorHandler(String socketKey) {
|
||||||
this.socketKey = socketKey;
|
this.socketKey = socketKey;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void accept(IOException exception) {
|
public void accept(Throwable exception) {
|
||||||
try {
|
try {
|
||||||
JSONObject event = new JSONObject();
|
JSONObject event = new JSONObject();
|
||||||
event.put("type", "Error");
|
event.put("type", "Error");
|
||||||
|
Loading…
Reference in New Issue
Block a user