i broke it :)
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package software.eskimo.capacitor.sockets;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
interface SocketDelegate {
|
||||
void didChangeState(String socketId, String state);
|
||||
void didReceiveMessage(String socketId, String message);
|
||||
}
|
||||
|
||||
public class SocketConnection {
|
||||
|
||||
public String id;
|
||||
private String host;
|
||||
private int port;
|
||||
private boolean useTLS;
|
||||
private boolean acceptInvalidCertificates;
|
||||
private SocketDelegate delegate;
|
||||
|
||||
private Socket socket;
|
||||
private ExecutorService executor;
|
||||
|
||||
public SocketConnection(String id, String host, int port, boolean useTLS, boolean acceptInvalidCertificates) {
|
||||
this.id = id;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.useTLS = useTLS;
|
||||
this.acceptInvalidCertificates = acceptInvalidCertificates;
|
||||
this.executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
public void setDelegate(SocketDelegate delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
if (useTLS) {
|
||||
SSLSocketFactory factory;
|
||||
if (acceptInvalidCertificates) {
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
TrustManager[] trustManagers = new TrustManager[]{
|
||||
new X509TrustManager() {
|
||||
@Override
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
|
||||
}
|
||||
}
|
||||
};
|
||||
sslContext.init(null, trustManagers, new java.security.SecureRandom());
|
||||
factory = sslContext.getSocketFactory();
|
||||
} else {
|
||||
factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
|
||||
}
|
||||
socket = factory.createSocket();
|
||||
socket.connect(new InetSocketAddress(host, port));
|
||||
delegate.didChangeState(id, "connected");
|
||||
} else {
|
||||
socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(host, port));
|
||||
delegate.didChangeState(id, "connected");
|
||||
}
|
||||
|
||||
listenForMessages();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
delegate.didChangeState(id, "disconnected");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void send(String message) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
if (socket != null && socket.isConnected()) {
|
||||
OutputStream outputStream = socket.getOutputStream();
|
||||
outputStream.write(message.getBytes());
|
||||
outputStream.flush();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
try {
|
||||
if (socket != null && !socket.isClosed()) {
|
||||
socket.close();
|
||||
delegate.didChangeState(id, "disconnected");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void listenForMessages() {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
char[] buffer = new char[1024]; // Adjust buffer size as needed
|
||||
int charsRead;
|
||||
|
||||
while ((charsRead = reader.read(buffer)) != -1) {
|
||||
String message = new String(buffer, 0, charsRead);
|
||||
delegate.didReceiveMessage(id, message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,60 @@
|
||||
package software.eskimo.capacitor.sockets;
|
||||
|
||||
import android.util.Log;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Sockets {
|
||||
public class Sockets implements SocketDelegate {
|
||||
|
||||
public String echo(String value) {
|
||||
Log.i("Echo", value);
|
||||
return value;
|
||||
private List<SocketConnection> sockets = new ArrayList<>();
|
||||
private SocketsPlugin plugin;
|
||||
|
||||
public Sockets(SocketsPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didChangeState(String socketId, String state) {
|
||||
plugin.notifyStateChange(socketId, state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didReceiveMessage(String socketId, String message) {
|
||||
Log.e("Sockets", "Received message: " + message);
|
||||
plugin.notifyMessageReceived(socketId, message);
|
||||
}
|
||||
|
||||
public SocketConnection create(String id, String host, int port, boolean useTLS, boolean acceptInvalidCertificates) {
|
||||
SocketConnection socket = new SocketConnection(id, host, port, useTLS, acceptInvalidCertificates);
|
||||
socket.setDelegate(this);
|
||||
sockets.add(socket);
|
||||
return socket;
|
||||
}
|
||||
|
||||
public void connect(String id) {
|
||||
for (SocketConnection socket : sockets) {
|
||||
if (socket.id.equals(id)) {
|
||||
socket.connect();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void send(String id, String message) {
|
||||
for (SocketConnection socket : sockets) {
|
||||
if (socket.id.equals(id)) {
|
||||
socket.send(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect(String id) {
|
||||
for (SocketConnection socket : sockets) {
|
||||
if (socket.id.equals(id)) {
|
||||
socket.disconnect();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package software.eskimo.capacitor.sockets;
|
||||
|
||||
import android.util.Log;
|
||||
import com.getcapacitor.JSObject;
|
||||
import com.getcapacitor.Plugin;
|
||||
import com.getcapacitor.PluginCall;
|
||||
@@ -9,14 +10,61 @@ import com.getcapacitor.annotation.CapacitorPlugin;
|
||||
@CapacitorPlugin(name = "Sockets")
|
||||
public class SocketsPlugin extends Plugin {
|
||||
|
||||
private Sockets implementation = new Sockets();
|
||||
private Sockets implementation;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
implementation = new Sockets(this);
|
||||
}
|
||||
|
||||
public void notifyStateChange(String socketId, String state) {
|
||||
JSObject data = new JSObject();
|
||||
data.put("id", socketId);
|
||||
data.put("state", state);
|
||||
notifyListeners("state", data);
|
||||
}
|
||||
|
||||
public void notifyMessageReceived(String socketId, String message) {
|
||||
JSObject data = new JSObject();
|
||||
data.put("id", socketId);
|
||||
data.put("message", message);
|
||||
|
||||
Log.e("SocketsPlugin", "Received message: " + message);
|
||||
|
||||
notifyListeners("message", data);
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void echo(PluginCall call) {
|
||||
String value = call.getString("value");
|
||||
public void create(PluginCall call) {
|
||||
String id = call.getString("id", java.util.UUID.randomUUID().toString());
|
||||
String host = call.getString("host");
|
||||
int port = call.getInt("port");
|
||||
boolean useTLS = call.getBoolean("useTLS", false);
|
||||
boolean acceptInvalidCertificates = call.getBoolean("acceptInvalidCertificates", false);
|
||||
|
||||
JSObject ret = new JSObject();
|
||||
ret.put("value", implementation.echo(value));
|
||||
call.resolve(ret);
|
||||
implementation.create(id, host, port, useTLS, acceptInvalidCertificates);
|
||||
call.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void connect(PluginCall call) {
|
||||
String id = call.getString("id");
|
||||
implementation.connect(id);
|
||||
call.resolve();
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void send(PluginCall call) {
|
||||
String id = call.getString("id");
|
||||
String message = call.getString("message");
|
||||
implementation.send(id, message);
|
||||
call.resolve();
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void disconnect(PluginCall call) {
|
||||
String id = call.getString("id");
|
||||
implementation.disconnect(id);
|
||||
call.resolve();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user