|
|
@@ -52,7 +52,11 @@ public class SseServiceImpl implements shop.alien.dining.service.SseService {
|
|
|
});
|
|
|
|
|
|
emitter.onError((ex) -> {
|
|
|
- log.error("SSE连接错误, tableId={}, connectionId={}, error={}", tableId, connectionId, ex.getMessage(), ex);
|
|
|
+ if (isClientDisconnect(ex)) {
|
|
|
+ log.info("SSE客户端已断开, tableId={}, connectionId={}", tableId, connectionId);
|
|
|
+ } else {
|
|
|
+ log.error("SSE连接错误, tableId={}, connectionId={}, error={}", tableId, connectionId, ex.getMessage(), ex);
|
|
|
+ }
|
|
|
removeConnection(tableId, connectionId);
|
|
|
});
|
|
|
|
|
|
@@ -90,7 +94,11 @@ public class SseServiceImpl implements shop.alien.dining.service.SseService {
|
|
|
.data(messageJson));
|
|
|
log.info("推送购物车更新成功, tableId={}, connectionId={}", tableId, connectionId);
|
|
|
} catch (IOException e) {
|
|
|
- log.error("推送购物车更新失败, tableId={}, connectionId={}, error={}", tableId, connectionId, e.getMessage(), e);
|
|
|
+ if (isClientDisconnect(e)) {
|
|
|
+ log.debug("推送时客户端已断开, tableId={}, connectionId={}", tableId, connectionId);
|
|
|
+ } else {
|
|
|
+ log.error("推送购物车更新失败, tableId={}, connectionId={}, error={}", tableId, connectionId, e.getMessage(), e);
|
|
|
+ }
|
|
|
removeConnection(tableId, connectionId);
|
|
|
}
|
|
|
});
|
|
|
@@ -113,6 +121,22 @@ public class SseServiceImpl implements shop.alien.dining.service.SseService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 判断是否为客户端主动断开(Broken pipe、Connection reset 等),此类情况属正常,无需打 ERROR。
|
|
|
+ */
|
|
|
+ private boolean isClientDisconnect(Throwable ex) {
|
|
|
+ if (ex == null) return false;
|
|
|
+ String msg = ex.getMessage();
|
|
|
+ if (msg != null) {
|
|
|
+ String lower = msg.toLowerCase();
|
|
|
+ if (lower.contains("broken pipe") || lower.contains("connection reset")
|
|
|
+ || lower.contains("connection closed") || lower.contains("an established connection was aborted")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return isClientDisconnect(ex.getCause());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 移除连接
|
|
|
*/
|
|
|
private void removeConnection(Integer tableId, String connectionId) {
|
|
|
@@ -144,7 +168,11 @@ public class SseServiceImpl implements shop.alien.dining.service.SseService {
|
|
|
.data("ping"));
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- log.error("发送心跳失败, tableId={}, connectionId={}", tableId, connectionId, e);
|
|
|
+ if (isClientDisconnect(e)) {
|
|
|
+ log.debug("心跳时客户端已断开, tableId={}, connectionId={}", tableId, connectionId);
|
|
|
+ } else {
|
|
|
+ log.error("发送心跳失败, tableId={}, connectionId={}", tableId, connectionId, e);
|
|
|
+ }
|
|
|
removeConnection(tableId, connectionId);
|
|
|
}
|
|
|
}, 30, 30, TimeUnit.SECONDS); // 每30秒发送一次心跳
|