This commit is contained in:
eskimo
2026-02-12 16:32:16 -05:00
parent 8dee1c7ee5
commit bc547462f3

View File

@@ -129,6 +129,7 @@ public class SocketHandler {
String message = messageBuilder.toString();
if (message.endsWith("\r\n")) {
Log.d("SocketHandler", "Message received: " + message);
handlePingPong(message);
delegate.onMessageReceived(id, message);
messageBuilder.setLength(0);
}
@@ -142,6 +143,54 @@ public class SocketHandler {
}).start();
}
private void handlePingPong(String raw) {
// IRC PING can arrive in several forms:
// PING :param
// PING param
// :server PING param
// @tags :server PING param
String[] lines = raw.split("\r\n");
for (String line : lines) {
String remaining = line;
// Skip IRCv3 tags (@key=value;... )
if (remaining.startsWith("@")) {
int spaceIdx = remaining.indexOf(' ');
if (spaceIdx == -1) continue;
remaining = remaining.substring(spaceIdx + 1).trim();
}
// Skip source prefix (:server )
if (remaining.startsWith(":")) {
int spaceIdx = remaining.indexOf(' ');
if (spaceIdx == -1) continue;
remaining = remaining.substring(spaceIdx + 1).trim();
}
// Now check for PING command
if (remaining.startsWith("PING")) {
String param = "";
if (remaining.length() > 5) {
param = remaining.substring(5);
// Strip leading colon if present (PING :param)
if (param.startsWith(":")) {
param = param.substring(1);
}
}
String pong = "PONG :" + param + "\r\n";
Log.d("SocketHandler", "Auto PONG: " + pong.trim());
try {
if (outputStream != null) {
outputStream.write(pong.getBytes());
outputStream.flush();
}
} catch (IOException e) {
Log.e("SocketHandler", "PONG send error: " + e.getMessage(), e);
}
}
}
}
private void closeQuietly() {
try {
if (socket != null) socket.close();