我使用SSE ping我的客户端。 ping请求通过SSE发送,客户端通过POST应答请求。不知何故,我的ping请求经常在客户端延迟到达。延迟似乎与使用的ping超时直接相关。
客户类:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String j = extras.getString("visokiInterval");
}
REST-API类:
public synchronized boolean ping(int timeout){
final OutboundEvent event = EventFactory.newPingEvent();
if (eventSink.isClosed()) {
return false;
} else {
try {
responseReceived.set(false);
requestTimestamp.set(System.currentTimeMillis());
eventSink.write(event);
waitForResponseOrTimeout(timeout);
boolean clientAnswered = responseReceived.get();
return result;
} catch (IOException e) {
return false;
}
}
}
public void onPingResponse() {
responseReceived.set(true);
responseTimestamp.set(System.currentTimeMillis());
synchronized (this) {
notifyAll();
}
}
private void waitForResponseOrTimeout(int timeout) {
boolean timeoutExpired;
do {
try {
synchronized (this) {
wait(timeout);
}
} catch (InterruptedException ex) {
LOGGER.error("Wait was interrupted", ex);
}
timeoutExpired = getElapsedTime() > timeout;
} while (!(timeoutExpired || responseReceived.get()));
}
这种延迟来自何处以及如何避免?