This commit is contained in:
eskimo 2024-10-05 13:36:43 -04:00
parent 4d17f4f56f
commit d5d9395655

View File

@ -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();
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
// 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);