Wolan1995

SAM | EchoChamber (09.06.2017)

Jun 9th, 2017
93
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.util.Map;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5. import javax.inject.Inject;
  6. import javax.websocket.EncodeException;
  7.  
  8. import javax.websocket.OnClose;
  9. import javax.websocket.OnError;
  10. import javax.websocket.OnMessage;
  11. import javax.websocket.OnOpen;
  12. import javax.websocket.Session;
  13. import javax.websocket.server.PathParam;
  14. import javax.websocket.server.ServerEndpoint;
  15.  
  16.  
  17. /**
  18.  * @ServerEndpoint gives the relative name for the end point
  19.  * This will be accessed via ws://localhost:8080/EchoChamber/echo
  20.  * Where "localhost" is the address of the host,
  21.  * "EchoChamber" is the name of the package
  22.  * and "echo" is the address to access this class from the server
  23.  */
  24. @ServerEndpoint(value = "/echo/{roomnumber}")
  25. public class EchoServer {
  26.  
  27.  
  28.     /**
  29.      * @OnOpen allows us to intercept the creation of a new session.
  30.      * The session class allows us to send data to the user.
  31.      * In the method onOpen, we'll let the user know that the handshake was
  32.      * successful.
  33.      */    
  34.  
  35.    
  36.  
  37.    
  38.     @OnOpen
  39.     public void onOpen(Session session, @PathParam("roomnumber") String roomnumber){
  40.         System.out.println(session.getId() + " has opened a connection");
  41.             session.getUserProperties().put("roomnumber", roomnumber);
  42.  
  43.             SessionHandler.openSessions.put(session.getId(), session);
  44.             SessionHandler.addSession(session);
  45.          
  46.          
  47.     }
  48.  
  49.     /**
  50.      * When a user sends a message to the server, this method will intercept the message
  51.      * and allow us to react to it. For now the message is read as a String.
  52.      */
  53.     @OnMessage
  54.     public void onMessage(String message, Session session){
  55.  
  56.  
  57.         String room = (String) session.getUserProperties().get("roomnumber");
  58.         SessionHandler.sendToSession(session, message);
  59.        
  60.         //if (s.isOpen() && s.getUserProperties().get("roomnumber").equals(room)){
  61.     }
  62.  
  63.     /**
  64.      * The user closes the connection.
  65.      *
  66.      * Note: you can't send messages to the client from this method
  67.      */
  68.     @OnClose
  69.     public void onClose(Session session){
  70.         System.out.println("Session " +session.getId()+" has ended");
  71.         SessionHandler.removeSession(session);
  72.        
  73.     }
  74.    
  75.     @OnError
  76.     public void onError(Throwable error) {
  77.         Logger.getLogger(EchoServer.class.getName()).log(Level.SEVERE, null, error);
  78.     }
  79. }
RAW Paste Data