From d5d9395655fcb4bf77a990c0b54ce3aee230731c Mon Sep 17 00:00:00 2001 From: eskimo Date: Sat, 5 Oct 2024 13:36:43 -0400 Subject: [PATCH] fix --- .../capacitor/sockets/SocketHandler.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) 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);