Advertisement
MagnusArias

PS2 | WebSocekt 2

Apr 10th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.logging.Level;
  3. import java.util.logging.Logger;
  4. import javax.inject.Inject;
  5.  
  6. import javax.websocket.OnClose;
  7. import javax.websocket.OnError;
  8. import javax.websocket.OnMessage;
  9. import javax.websocket.OnOpen;
  10. import javax.websocket.Session;
  11. import javax.websocket.server.ServerEndpoint;
  12.  
  13. /**
  14.  * @ServerEndpoint gives the relative name for the end point
  15.  * This will be accessed via ws://localhost:8080/EchoChamber/echo
  16.  * Where "localhost" is the address of the host,
  17.  * "EchoChamber" is the name of the package
  18.  * and "echo" is the address to access this class from the server
  19.  */
  20. @ServerEndpoint("/echo")
  21. public class EchoServer {
  22.     /**
  23.      * @OnOpen allows us to intercept the creation of a new session.
  24.      * The session class allows us to send data to the user.
  25.      * In the method onOpen, we'll let the user know that the handshake was
  26.      * successful.
  27.      */    
  28.     @OnOpen
  29.     public void onOpen(Session session){
  30.         System.out.println(session.getId() + " has opened a connection");
  31.         try {
  32.             session.getBasicRemote().sendText("Connection Established");
  33.             SessionHandler.addSession(session);
  34.         } catch (IOException ex) {
  35.             ex.printStackTrace();
  36.         }
  37.     }
  38.  
  39.     /**
  40.      * When a user sends a message to the server, this method will intercept the message
  41.      * and allow us to react to it. For now the message is read as a String.
  42.      */
  43.     @OnMessage
  44.     public void onMessage(String message, Session session){
  45.         SessionHandler.sendToallConnectedSessions(message);
  46.     }
  47.  
  48.     /**
  49.      * The user closes the connection.
  50.      *
  51.      * Note: you can't send messages to the client from this method
  52.      */
  53.     @OnClose
  54.     public void onClose(Session session){
  55.         System.out.println("Session " +session.getId()+" has ended");
  56.         SessionHandler.removeSession(session);
  57.        
  58.     }
  59.    
  60.     @OnError
  61.     public void onError(Throwable error) {
  62.         Logger.getLogger(EchoServer.class.getName()).log(Level.SEVERE, null, error);
  63.     }
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. import java.io.IOException;
  74. import java.util.HashSet;
  75. import java.util.Set;
  76. import java.util.logging.Level;
  77. import java.util.logging.Logger;
  78. import javax.websocket.Session;
  79.  
  80. /*
  81.  * To change this license header, choose License Headers in Project Properties.
  82.  * To change this template file, choose Tools | Templates
  83.  * and open the template in the editor.
  84.  */
  85.  
  86. /**
  87.  *
  88.  * @author student_10
  89.  */
  90. public class SessionHandler {
  91.     private static final Set<Session> sessions = new HashSet<>();
  92.    
  93.     public static void addSession(Session session){
  94.         sessions.add(session);
  95.     }
  96.    
  97.     public static void removeSession(Session session){
  98.         sessions.remove(session);
  99.     }
  100.    
  101.     public static void sendToSession(Session session, String message){
  102.        try {
  103.             session.getBasicRemote().sendText(message.toString());
  104.         } catch (IOException ex) {
  105.             sessions.remove(session);
  106.             Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
  107.         }
  108.     }
  109.    
  110.     public static void sendToallConnectedSessions(String message){
  111.          for (Session session : sessions) {
  112.             sendToSession(session, message);
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement