View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.api;
20  
21  /**
22   * Default implementation of the {@link WebSocketListener}.
23   * <p>
24   * Convenient abstract class to base standard WebSocket implementations off of.
25   */
26  public class WebSocketAdapter implements WebSocketListener
27  {
28      private volatile Session session;
29  
30      public RemoteEndpoint getRemote()
31      {
32          Session sess = this.session;
33          return sess == null?null:session.getRemote();
34      }
35  
36      public Session getSession()
37      {
38          return session;
39      }
40  
41      public boolean isConnected()
42      {
43          Session sess = this.session;
44          return (sess != null) && (sess.isOpen());
45      }
46  
47      public boolean isNotConnected()
48      {
49          Session sess = this.session;
50          return (sess == null) || (!sess.isOpen());
51      }
52  
53      @Override
54      public void onWebSocketBinary(byte[] payload, int offset, int len)
55      {
56          /* do nothing */
57      }
58  
59      @Override
60      public void onWebSocketClose(int statusCode, String reason)
61      {
62          this.session = null;
63      }
64  
65      @Override
66      public void onWebSocketConnect(Session sess)
67      {
68          this.session = sess;
69      }
70  
71      @Override
72      public void onWebSocketError(Throwable cause)
73      {
74          /* do nothing */
75      }
76  
77      @Override
78      public void onWebSocketText(String message)
79      {
80          /* do nothing */
81      }
82  }