Skip to content

Commit e45ae01

Browse files
committed
Added error handling in case of an IOException
1 parent 4aab543 commit e45ae01

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/main/java/org/javawebstack/httpserver/websocket/InternalWebSocketRequestHandler.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public InternalWebSocketRequestHandler(WebSocketHandler handler) {
2020

2121
public Object handle(Exchange exchange) {
2222
IHTTPSocket socket = exchange.socket();
23+
WebSocket webSocket = new WebSocket(exchange);
2324
try {
24-
WebSocket webSocket = new WebSocket(exchange);
2525
handler.onConnect(webSocket);
2626
WebSocketFrame frame;
2727
while (true) {
@@ -48,10 +48,17 @@ public Object handle(Exchange exchange) {
4848
}
4949
if(frame.getOpcode() == WebSocketUtil.OP_TEXT) {
5050
handler.onMessage(webSocket, new String(frame.getPayload(), StandardCharsets.UTF_8));
51-
continue;
5251
}
5352
}
54-
} catch (IOException ignored) {}
53+
} catch (IOException e) {
54+
handler.onClose(webSocket, null, null);
55+
try {
56+
socket.close();
57+
} catch (IOException ex) {
58+
throw new RuntimeException(ex);
59+
}
60+
throw new RuntimeException(e);
61+
}
5562
return null;
5663
}
5764
}

0 commit comments

Comments
 (0)