2016-02-28 38 views
0

我是websocket的新手,所以我在我的項目中遇到了一些問題。我正在創建一個聊天室。我有一個登錄頁面,將由UsernameServlet處理,然後它將鏈接到chatpage.html,它將打開一個websocket(聊天室服務器端點)。與Java的Web套接字映射

這裏是我的loginpage.html

<body> 
    <form action="localhost:8080/chatRoom/UserNameServlet" name="submitForm" onchange="handleNewRoom()"> 
     <mark>Please select a room:</mark> 
     <select id="roomSelect" name="roomSelect"> 
      <option value="room1">Room 1</option> 
      <option value="room2">Room 2</option> 
     </select> 

     <mark>Please Enter username</mark> 
     <input type="text" name="username" size="20"/> 
     <input type="submit" value="Enter" /> 
    </form> 

    <script> 
     window.onload = function(){document.submitForm.action=submitAction();} 
     function submitAction(){ 
      return document.location.pathname; 
     }    
    </script> 
</body> 

這裏是我的UserNameServlet @WebServlet( 「/ UserNameServlet」)

public class UserNameServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * 
    * @param request 
    * @param response 
    * @throws ServletException 
    * @throws IOException 
    */ 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html"); 

     String username = request.getParameter("username"); 
     HttpSession session = request.getSession(true); 

     String roomName = request.getParameter("roomSelect"); 
     session.setAttribute("username", username); 
     PrintWriter out = response.getWriter(); 
     if(username == null) response.sendRedirect("loginpage.html"); 
     else response.sendRedirect("chatPage.html"); 

     } 

這裏是我的chatPage.html

<script> 
     var websocket = new WebSocket("ws://""+"+document.location.host+"+"document.location.pathname+"+""chatroomServerEndpoint/"+roomName+""); 
</script> 

我ChatroomServerEndpoint.java

@ServerEndpoint(value="/chatroomServerEndpoint/{chatroom}", configurator=ChatroomServerConfigurator.class) 

因此,我的問題是:我的鏈接到websocket服務器是否正確,我如何在chatpage.html中訪問roomName?

回答

0

您可以將roomName參數添加爲通過loginpage.html定義的HTML會話的會話屬性。重定向後,您可以通過構建WebSocket端點配置程序來訪問HTML會話的屬性。

  import javax.servlet.http.HttpSession; 
      import javax.websocket.HandshakeResponse; 
      import javax.websocket.server.HandshakeRequest; 
      import javax.websocket.server.ServerEndpointConfig; 
      import javax.websocket.server.ServerEndpointConfig.Configurator; 

      public class ChatRoomEndpointConfigurator extends Configurator { 
       @Override 
       public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { 
       HttpSession httpSession = (HttpSession) request.getHttpSession(); 
       sec.getUserProperties().put(HttpSession.class.getName(), httpSession); 
      } 
     } 

之後,在ServerEndpoint,你會:

@ServerEndpoint(value = "/chatroomServerEndpoint/{chatroom}",configurator=ChatRoomEndpointConfigurator.class) 
    public class WSServer { 
     protected Session wsSession; 
     protected HttpSession httpSession;  

//Now, you can access the http's session attributes: 

     @OnOpen 
     public void onOpen(Session session, EndpointConfig config) { 
      this.wsSession=session; 
      this.httpSession=(HttpSession) config.getUserProperties().get(HttpSession.class.getName()); 
      String chatRoom = (String) httpSession.getAttribute("chatRoom"); 
     } 
    }