@@ -63,7 +63,7 @@ public class WebSocketConnection extends AttributeStore implements Comparable<We
6363 private AtomicBoolean connected = new AtomicBoolean (false );
6464
6565 // associated websocket session
66- private WeakReference < WsSession > wsSession ;
66+ private final WsSession wsSession ;
6767
6868 private WeakReference <WebSocketScope > scope ;
6969
@@ -114,9 +114,9 @@ public WebSocketConnection(WebSocketScope scope, Session session) {
114114 log .debug ("path: {}" , path );
115115 }
116116 // cast ws session
117- this .wsSession = new WeakReference <>(( WsSession ) session ) ;
117+ this .wsSession = ( WsSession ) session ;
118118 if (isDebug ) {
119- log .debug ("ws session: {}" , wsSession . get () );
119+ log .debug ("ws session: {}" , wsSession );
120120 }
121121 // the websocket session id will be used for hash code comparison, its the only usable value currently
122122 wsSessionId = session .getId ();
@@ -186,7 +186,7 @@ public void send(String data) throws UnsupportedEncodingException, IOException {
186186 }
187187 // process the incoming string
188188 if (StringUtils .isNotBlank (data )) {
189- final WsSession session = wsSession . get () ;
189+ final WsSession session = wsSession ;
190190 // attempt send only if the session is not closed
191191 if (session != null && !session .isClosed ()) {
192192 try {
@@ -236,7 +236,7 @@ public void send(byte[] buf) throws IOException {
236236 if (isDebug ) {
237237 log .debug ("send binary: {}" , Arrays .toString (buf ));
238238 }
239- WsSession session = wsSession . get () ;
239+ WsSession session = wsSession ;
240240 if (session != null && session .isOpen ()) {
241241 try {
242242 // send the bytes
@@ -281,7 +281,7 @@ public void sendPing(byte[] buf) throws IllegalArgumentException, IOException {
281281 if (isTrace ) {
282282 log .trace ("send ping: {}" , buf );
283283 }
284- WsSession session = wsSession . get () ;
284+ WsSession session = wsSession ;
285285 if (session != null && session .isOpen ()) {
286286 synchronized (wsSessionId ) {
287287 // send the bytes
@@ -305,7 +305,7 @@ public void sendPong(byte[] buf) throws IllegalArgumentException, IOException {
305305 if (isTrace ) {
306306 log .trace ("send pong: {}" , buf );
307307 }
308- WsSession session = wsSession . get () ;
308+ WsSession session = wsSession ;
309309 if (session != null && session .isOpen ()) {
310310 synchronized (wsSessionId ) {
311311 // send the bytes
@@ -324,15 +324,11 @@ public void sendPong(byte[] buf) throws IllegalArgumentException, IOException {
324324 public void close () {
325325 if (connected .compareAndSet (true , false )) {
326326 log .debug ("close: {}" , wsSessionId );
327- WsSession session = wsSession != null ? wsSession .get () : null ;
328- // session has to be open, or user props cannot be retrieved
329- if (session != null && session .isOpen ()) {
330- // trying to close the session nicely
331- try {
332- session .close ();
333- } catch (Exception e ) {
334- log .debug ("Exception closing session" , e );
335- }
327+ // trying to close the session nicely
328+ try {
329+ wsSession .close ();
330+ } catch (Exception e ) {
331+ log .debug ("Exception closing session" , e );
336332 }
337333 // clean up our props
338334 attributes .clear ();
@@ -351,7 +347,6 @@ public void close() {
351347 // disconnect from scope
352348 scope .get ().removeConnection (this );
353349 // clear weak refs
354- wsSession .clear ();
355350 scope .clear ();
356351 }
357352 }
@@ -369,7 +364,7 @@ public void timeoutAsync(long now) {
369364 // XXX(paul) only logging here as we should more than likely rely upon the container checking expiration
370365 log .trace ("timeoutAsync: {} on session id: {} read: {} written: {}" , now , wsSessionId , readBytes , writtenBytes );
371366 /*
372- WsSession session = wsSession.get() ;
367+ WsSession session = wsSession;
373368 Map<String, Object> props = session.getUserProperties();
374369 log.debug("Session properties: {}", props);
375370 long maxIdleTimeout = session.getMaxIdleTimeout();
@@ -453,7 +448,7 @@ public void setOrigin(String origin) {
453448 * @return true if secure and false if unsecure or unconnected
454449 */
455450 public boolean isSecure () {
456- Optional <WsSession > opt = Optional .ofNullable (wsSession . get () );
451+ Optional <WsSession > opt = Optional .ofNullable (wsSession );
457452 if (opt .isPresent ()) {
458453 return (opt .get ().isOpen () ? opt .get ().isSecure () : false );
459454 }
@@ -672,12 +667,12 @@ public Object getUserProperty(String key) {
672667
673668 public void setWsSessionTimeout (long idleTimeout ) {
674669 if (wsSession != null ) {
675- wsSession .get (). setMaxIdleTimeout (idleTimeout );
670+ wsSession .setMaxIdleTimeout (idleTimeout );
676671 }
677672 }
678673
679674 public WsSession getWsSession () {
680- return wsSession != null ? wsSession . get () : null ;
675+ return wsSession != null ? wsSession : null ;
681676 }
682677
683678 public long getReadBytes () {
0 commit comments