diff --git a/android/src/main/java/software/eskimo/capacitor/sockets/SocketHandler.java b/android/src/main/java/software/eskimo/capacitor/sockets/SocketHandler.java index 6351607..b9c113d 100644 --- a/android/src/main/java/software/eskimo/capacitor/sockets/SocketHandler.java +++ b/android/src/main/java/software/eskimo/capacitor/sockets/SocketHandler.java @@ -91,29 +91,28 @@ public class SocketHandler { } private void receive() { - new Thread(() -> { - try { - char[] buffer = new char[1024]; - StringBuilder messageBuilder = new StringBuilder(); - int numCharsRead; - while ((numCharsRead = inputStream.read(buffer)) != -1) { - messageBuilder.append(buffer, 0, numCharsRead); - String message = messageBuilder.toString(); - - // Check if the message ends with \r\n (or \n, depending on protocol) - if (message.endsWith("\r\n")) { - Log.d("SocketHandler", "Message received: " + message); - delegate.onMessageReceived(id, message); // Notify with full message including \r\n - messageBuilder.setLength(0); // Clear the buffer for the next message + new Thread(() -> { + try { + char[] buffer = new char[1024]; + StringBuilder messageBuilder = new StringBuilder(); + int numCharsRead; + while ((numCharsRead = inputStream.read(buffer)) != -1) { + messageBuilder.append(buffer, 0, numCharsRead); + String message = messageBuilder.toString(); + + // Check if the message ends with \r\n (or \n, depending on protocol) + if (message.endsWith("\r\n")) { + Log.d("SocketHandler", "Message received: " + message); + delegate.onMessageReceived(id, message); // Notify with full message including \r\n + messageBuilder.setLength(0); // Clear the buffer for the next message + } } + } catch (IOException e) { + Log.e("SocketHandler", "Receive error: " + e.getMessage(), e); + delegate.onStateChanged(id, "disconnected"); } - } catch (IOException e) { - Log.e("SocketHandler", "Receive error: " + e.getMessage(), e); - delegate.onStateChanged(id, "disconnected"); - } - }).start(); -} - + }).start(); + } public interface SocketDelegate { void onStateChanged(String socketId, String state);