[英]Web Socket Mapping with Java
我是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?
您可以將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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.