title: WebSocket and Endpoint
body: <p>My IDE : NetBeans, GlassFish.
  I  am trying to understand how GlassFish execute and run the Endpoint!
  I understand Glassfish as a webserver creates new Endpoint instance for each connection, and the Set : peers in the code appears as one global variable for every instance of endpoint.
  My question is : how to add another global variable(s) so every instance of endpoint can access it?</p>
  
  <pre><code> enter code here
  
  import java.io.IOException;
  
  import java.util.Collections; 
  import java.util.HashSet; 
  import java.util.Set; 
  import javax.websocket.OnClose;
  import javax.websocket.OnMessage;
  import javax.websocket.OnOpen;
  import javax.websocket.Session;
  import javax.websocket.server.ServerEndpoint;
  
  
  
  @ServerEndpoint("/echo") 
  public class EchoServer {
      /**
       * @OnOpen allows us to intercept the creation of a new session.
       * The session class allows us to send data to the user.
       * In the method onOpen, we'll let the user know that the handshake was 
       * successful.
       */
       private static final Set&lt;Session &gt; peers = Collections.synchronizedSet(new HashSet&lt;Session &gt;());
  
      @OnOpen
      public void onOpen(Session session){
  
  
          System.out.println(session.getId() + " has opened a connection"); 
          try {
              peers.add(session);
  
              session.getBasicRemote().sendText("Connection Established");
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  
  
      @OnMessage
      public void onMessage(String message, Session session) throws IOException{
  
          System.out.println("Message from " + session.getId() + ": " + message);
  
          for (Session  peer : peers) {
               peer.aplayer.getBasicRemote().sendText(" Message from " + session.getId()+" : "+message); 
          }
      }`enter code here`
  
  
      @OnClose
      public void onClose(Session session){
          System.out.println("Session " +session.getId()+" has ended");
          peers.remove(session);
      }
  }
  </code></pre>