3434 */
3535
3636import java .io .IOException ;
37+ import java .io .EOFException ;
3738import java .io .InputStream ;
3839import java .io .OutputStream ;
40+ import java .net .URI ;
3941import java .nio .charset .CharacterCodingException ;
4042import java .util .LinkedList ;
4143import java .util .List ;
@@ -57,11 +59,17 @@ public abstract class WebSocket {
5759
5860 private final List <WebSocketFrame > continuousFrames = new LinkedList <WebSocketFrame >();
5961
60- private State state = State .UNCONNECTED ;
62+ private volatile State state = State .UNCONNECTED ;
6163
6264 private Lock lock = new ReentrantLock ();
6365
66+ private final URI uri ;
67+
6468 protected WebSocket (HttpExchange exchange ) {
69+ this .uri = exchange .getRequestURI ();
70+
71+ logger .info ("connecting websocket " +uri );
72+
6573 this .state = State .CONNECTING ;
6674 this .in = exchange .getRequestBody ();
6775 this .out = exchange .getResponseBody ();
@@ -81,7 +89,9 @@ protected void onOpen() throws WebSocketException {
8189 protected abstract void onPong (WebSocketFrame pong ) throws WebSocketException ;
8290
8391 protected void onException (IOException exception ) {
84- logger .log (Level .FINER , "exception on websocket" , exception );
92+ if (state !=State .CLOSING && state !=State .CLOSED ) {
93+ logger .log (Level .FINER , "exception on websocket" , exception );
94+ }
8595 }
8696
8797 protected void onFrameReceived (WebSocketFrame frame ) {
@@ -100,6 +110,8 @@ protected void onFrameSent(WebSocketFrame frame) {
100110 }
101111
102112 public void close (CloseCode code , String reason , boolean initiatedByRemote ) throws IOException {
113+ logger .info ("closing websocket " +uri );
114+
103115 State oldState = this .state ;
104116 this .state = State .CLOSING ;
105117 if (oldState == State .OPEN ) {
@@ -116,15 +128,13 @@ void doClose(CloseCode code, String reason, boolean initiatedByRemote) {
116128 if (this .in != null ) {
117129 try {
118130 this .in .close ();
119- } catch (IOException e ) {
120- logger .log (Level .FINE , "close failed" , e );
131+ } catch (IOException expected ) {
121132 }
122133 }
123134 if (this .out != null ) {
124135 try {
125136 this .out .close ();
126- } catch (IOException e ) {
127- logger .log (Level .FINE , "close failed" , e );
137+ } catch (IOException expected ) {
128138 }
129139 }
130140 this .state = State .CLOSED ;
@@ -140,6 +150,7 @@ private void handleCloseFrame(WebSocketFrame frame) throws IOException {
140150 code = ((CloseFrame ) frame ).getCloseCode ();
141151 reason = ((CloseFrame ) frame ).getCloseReason ();
142152 }
153+ logger .finest ("handleCloseFrame: " +uri +", code=" +code +", reason=" +reason +", state " +this .state );
143154 if (this .state == State .CLOSING ) {
144155 // Answer for my requested close
145156 doClose (code , reason , false );
@@ -204,21 +215,27 @@ public void ping(byte[] payload) throws IOException {
204215 void readWebsocket () {
205216 try {
206217 state = State .OPEN ;
218+ logger .fine ("websocket open " +uri );
207219 onOpen ();
208220 while (this .state == State .OPEN ) {
209221 handleWebsocketFrame (WebSocketFrame .read (in ));
210222 }
223+ } catch (EOFException e ) {
224+ onException (e );
225+ doClose (CloseCode .AbnormalClosure , e .toString (), false );
211226 } catch (CharacterCodingException e ) {
212227 onException (e );
213228 doClose (CloseCode .InvalidFramePayloadData , e .toString (), false );
214229 } catch (IOException e ) {
215230 onException (e );
216231 if (e instanceof WebSocketException wse ) {
217232 doClose (wse .getCode (), wse .getReason (), false );
233+ } else {
234+ doClose (CloseCode .AbnormalClosure , e .toString (), false );
218235 }
219236 } finally {
220237 doClose (CloseCode .InternalServerError , "Handler terminated without closing the connection." , false );
221- logger .info ("readWebsocket() exiting" );
238+ logger .finest ("readWebsocket() exiting " + uri );
222239 }
223240 }
224241
0 commit comments