【问题标题】:Web Socket Mapping with Java使用 Java 进行 Web 套接字映射
【发布时间】:2016-02-28 20:52:41
【问题描述】:

我是 websocket 的新手,所以我的项目中有一些问题。我正在创建一个聊天室。我有一个由 UsernameServlet 处理的登录页面,然后它将链接到 chatpage.html,它将打开一个 websocket(聊天室服务器端点)。

这是我的 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?

【问题讨论】:

    标签: javascript java websocket mapping chatroom


    【解决方案1】:

    您可以将 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");
            }
         }
    

    【讨论】:

    • 您能否分享一个与映射相关的完整示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多