|
35 | 35 | import jakarta.websocket.server.ServerEndpoint; |
36 | 36 | import java.io.IOException; |
37 | 37 | import java.io.StringWriter; |
| 38 | +import java.nio.channels.ClosedChannelException; |
38 | 39 | import java.util.Iterator; |
39 | 40 | import java.util.Map; |
40 | 41 | import java.util.concurrent.ConcurrentHashMap; |
@@ -124,6 +125,16 @@ public void closeSession(final Session session, final CloseReason closeReason) { |
124 | 125 | sessions.remove(session); |
125 | 126 | } |
126 | 127 |
|
| 128 | + @OnError |
| 129 | + public void onError(final Session session, final Throwable throwable) { |
| 130 | + sessions.remove(session); |
| 131 | + if (throwable instanceof ClosedChannelException) { |
| 132 | + LOG.debug("WebSocket client disconnected abruptly: session {}", session.getId()); |
| 133 | + } else { |
| 134 | + LOG.warn("WebSocket error on session {}: {}", session.getId(), throwable.getMessage(), throwable); |
| 135 | + } |
| 136 | + } |
| 137 | + |
127 | 138 | @OnMessage |
128 | 139 | public void recv(final String message, final Session session) { |
129 | 140 | try (final JsonParser parser = JSON_FACTORY.createParser(message)) { |
@@ -192,7 +203,8 @@ public static void sendAll(final String toChannel, final String message) { |
192 | 203 | session.getBasicRemote().sendText(message); |
193 | 204 | } |
194 | 205 | } catch (final IOException e) { |
195 | | - LOG.error("Error sending message via websocket: {}", e.getMessage(), e); |
| 206 | + LOG.debug("Removing disconnected WebSocket session: {}", e.getMessage()); |
| 207 | + iterator.remove(); |
196 | 208 | } |
197 | 209 | } |
198 | 210 | } |
|
0 commit comments