title: send multiple messages using web socket server end point in java api
body: <p>I am trying to implement Java WebSocket api.</p>
  
  <p>I am using <a href="https://www.openshift.com/blogs/how-to-build-java-websocket-applications-using-the-jsr-356-api" rel="nofollow">this</a> example for implementation.</p>
  
  <p>My Client End Point Code is as below:</p>
  
  <pre><code>package com.java.webSocket;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.URI;
  import java.net.URISyntaxException;
  import java.util.List;
  import java.util.concurrent.CountDownLatch;
  
  import javax.websocket.ClientEndpoint;
  import javax.websocket.CloseReason;
  import javax.websocket.DeploymentException;
  import javax.websocket.OnClose;
  import javax.websocket.OnMessage;
  import javax.websocket.OnOpen;
  import javax.websocket.Session;
  
  import org.glassfish.tyrus.client.ClientManager;
  
  @ClientEndpoint
  public class WordgameClientEndpoint {
  
       private static CountDownLatch latch;
  
      @OnOpen
      public void onOpen(Session session) {
          System.out.println("Connected ... " + session.getId());
      }
  
      @OnMessage
      public String onMessage(String message, Session session) {
          BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
          try {
              System.out.println("session.getId():-- " + session.getId());
              System.out.println("Received ...." + message);
              String userInput = bufferRead.readLine();
              return userInput;
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
  
      @OnClose
      public void onClose(Session session, CloseReason closeReason) {
          System.out.println(String.format("Session %s close because of %s", session.getId(), closeReason));
          latch.countDown();
      }
  
      public static void main(String[] args) {
          latch = new CountDownLatch(1);
  
          ClientManager client = ClientManager.createClient();
          try {
              client.connectToServer(WordgameClientEndpoint.class, new URI("ws://localhost:8182/WbSocketDemoTest/game"));
  
              latch.await();
  
          } catch (DeploymentException | URISyntaxException | InterruptedException e) {
              throw new RuntimeException(e);
          }
      }
  
  }
  </code></pre>
  
  <p>WebSocketServer.java is as below:</p>
  
  <pre><code>package com.java.webSocket;
  
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  
  import org.glassfish.tyrus.server.Server;
  
  public class WebSocketServer {
  
      public static void main(String[] args) {
          runServer();
      }
  
      public static void runServer() {
          Server server = new Server("localhost", 8182, "/WbSocketDemoTest", WordgameServerEndpoint.class);
  
          try {
              server.start();
              BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
              System.out.print("Please press a key to stop the server.");
              reader.readLine();
          } catch (Exception e) {
              throw new RuntimeException(e);
          } finally {
              server.stop();
          }
      }
  }
  </code></pre>
  
  <p>My Server End points code is as below:</p>
  
  <pre><code>package com.java.webSocket;
  
  import java.io.IOException;
  import java.util.Collections;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Set;
  import java.util.concurrent.CopyOnWriteArraySet;
  
  import javax.websocket.CloseReason;
  import javax.websocket.CloseReason.CloseCodes;
  import javax.websocket.OnClose;
  import javax.websocket.OnMessage;
  import javax.websocket.OnOpen;
  import javax.websocket.Session;
  import javax.websocket.server.ServerEndpoint;
  
  @ServerEndpoint(value = "/game")
  public class WordgameServerEndpoint {
  
      private static final Set&lt;WordgameServerEndpoint&gt; connections =  new CopyOnWriteArraySet&lt;WordgameServerEndpoint&gt;();
      //private static final Set&lt;Session&gt; sessions = Collections.synchronizedSet(new HashSet&lt;Session&gt;());
      private Session session;
  
      @OnOpen
      public void onOpen(Session session) {
          System.out.println("Connected ... " + session.getId());
          this.session = session;
          connections.add(this);
          try {
              session.getBasicRemote().sendText("message form onOpen of server");
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      @OnMessage
      public void onMessage(String message, Session session) throws IOException {
          switch (message) {
          case "quit":
              try {
                  session.close(new CloseReason(CloseCodes.NORMAL_CLOSURE,
                          "Game ended"));
              } catch (IOException e) {
                  throw new RuntimeException(e);
              }
              break;
          }
          broadcast2(session,message);
      }
  
      private void broadcast2(Session currentSession, String message){
          for(WordgameServerEndpoint current : connections){
              try {
                  System.out.println("in broadcast current.session.getId():-- " + current.session.getId());
                  current.session.getBasicRemote().sendText(message);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  
      @OnClose
      public void onClose(Session session, CloseReason closeReason) {
          System.out.println(String.format("Session %s closed because of %s",
                  session.getId(), closeReason));
      }
  
  }
  </code></pre>
  
  <p>The problem is in broadcast2(Session currentSession, String message) method.</p>
  
  <p>It shows all connected clients but sends message to only one client instead of all.</p>
  
  <p>I want to implement this in spring, so any suggestion is highly appreciated.</p>