title: Tomcat clientendpoint websocket keeps closing
body: <p>I have the following <code>clientendpoint</code> class for a <code>websocket</code> in <code>tomcat 7.0.53</code>. It is based off of this example on a website <a href="https://blog.openshift.com/how-to-build-java-websocket-applications-using-the-jsr-356-api/" rel="nofollow">https://blog.openshift.com/how-to-build-java-websocket-applications-using-the-jsr-356-api/</a></p>
  
  <pre><code>import java.io.IOException;
  import java.net.URI;
  import java.util.ArrayList;
  
  import javax.websocket.ClientEndpoint;
  import javax.websocket.CloseReason;
  import javax.websocket.ContainerProvider;
  import javax.websocket.DeploymentException;
  import javax.websocket.OnClose;
  import javax.websocket.OnError;
  import javax.websocket.OnMessage;
  import javax.websocket.OnOpen;
  import javax.websocket.Session;
  import javax.websocket.WebSocketContainer;
  @ClientEndpoint
  public class connect {
      private static ArrayList&lt;Session&gt; sessionList = new ArrayList&lt;Session&gt;();
  
      public connect(URI endpointURI) throws DeploymentException, IOException
      {
          WebSocketContainer container = ContainerProvider.getWebSocketContainer();
          container.connectToServer(this, endpointURI);
      }
  
      @OnOpen
      public void onOpen(Session session) throws IOException
      {
          sessionList.add(session);
          System.out.println(session.getId());
          session.getBasicRemote().sendText("hello");
      }
  
      public void sendMessage(String message) throws IOException
      {
          for(Session session : sessionList){
              //asynchronous communication
              session.getBasicRemote().sendText(message);
          }
      }
  
      @OnClose
      public void onClose(Session session){
          sessionList.remove(session);
          System.out.println("here");
      }
  
      @OnError
      public void onError(Throwable t, Session session){
          System.out.println("tedt");  
      }
  }
  </code></pre>
  
  <p>I then have the following code to start the client endpoint</p>
  
  <pre><code>import java.io.IOException;
  import java.net.URI;
  import java.net.URISyntaxException;
  
  import javax.websocket.DeploymentException;
  
  
  public class test {
  
      public static void main(String[] args) throws DeploymentException, IOException, URISyntaxException {
          // TODO Auto-generated method stub
          connect connect = new connect(new URI("ws://localhost:8080/example/talk"));
          connect.sendMessage("now");
      }
  
  }
  </code></pre>
  
  <p>The client does successfully connect to the websocket server, however then it gets disconnected right away when I try to send a message or do anything, I know this since the <code>onError</code> function is being called when I try to send a message from the <code>onOpen</code> function. Why is the <code>websocket</code> getting closed immediately after it is connected to the server?</p>