您好,登录后才能下订单哦!
本篇内容介绍了“websocket建立连接时传递参数的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
后台获取参数4中方法:
websocket的url
"ws:localhost:82/chat/123?username=ydh";
socket的注释
@ServerEndpoint("/chat/{param}")
socket的open方法
@OnOpen public void open(Session session, @PathParam("param")String param) { System.out.println("用户"+param+" 登录"); Map<String, String> map = session.getPathParameters(); System.out.println("session.getPathParameters()"+map.toString()); String str = session.getQueryString(); System.out.println("session.getQueryString()"+str); String uri = session.getRequestURI().toString(); System.out.println("session.getRequestURI().toString()"+uri); }
结果
用户123 登录 session.getPathParameters(){param=123} session.getQueryString()username=baidu session.getRequestURI().toString()/chat/123?username=baidu
1、url站位,通过@PathParam注释获取
2、url站位,通过session.getPathParameters()获取
3、url传参,通过session.getQueryString()
4、url传参,通过session.getRequestURI().toString()
从结果可以看出1、2两个方法不需要截取,可以直接使用;而3、4两个方法都需要截取字符串,稍微麻烦,但是请求的url比较清爽。
下面的是完整代码
config代码
import javax.websocket.Endpoint; import javax.websocket.server.ServerApplicationConfig; import javax.websocket.server.ServerEndpointConfig; import java.util.Set; public class ChatConfig implements ServerApplicationConfig { // 注解 @Override public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) { System.out.println("websocket......启动!"+scanned.size()); return scanned; } // 接口 @Override public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scanned) { return null; } }
socket代码
import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.util.Map; /** * @description: * @author: yangdehong * @version: 2017/6/4. */ @ServerEndpoint("/chat/{param}") public class ChatSocket { @OnOpen public void open(Session session, @PathParam("param")String param) { System.out.println("用户"+param+" 登录"); Map<String, String> map = session.getPathParameters(); System.out.println("session.getPathParameters()"+map.toString()); String str = session.getQueryString(); System.out.println("session.getQueryString()"+str); String uri = session.getRequestURI().toString(); System.out.println("session.getRequestURI().toString()"+uri); } @OnClose public void close(Session session){ System.out.println("sessionId:"+session.getId()+"关闭"); } @OnMessage public void message(Session session, String message, boolean last) throws IOException { System.out.println("sessionId:"+session.getId()+"-------"+message+"------"+last); session.getBasicRemote().sendText("接受成功"); } }
模拟登录servlert
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @description: * @author: yangdehong * @version: 2017/6/3. */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); request.getSession().setAttribute("username", username); response.sendRedirect("chat.jsp"); } }
chat.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script type="text/javascript"> var ws; var target = "ws:localhost:82/chat/123?username=" + '${username}'; window.onload = function () { if ('WebSocket' in window) { ws = new WebSocket(target); } else if ('MozWebSocket' in window) { ws = new MozWebSocket(target); } else { alert('WebSocket is not supported by this browser.'); return; } }; </script> </head> <body> <div id="content"> </div> <div id="userList"> </div> </body> </html>
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="LoginServlet" method="get"> name:<input name="username"> <input type="submit" /> </form> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>websocket</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
maven
<dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-websocket</artifactId> <version>8.0.26</version> </dependency>
“websocket建立连接时传递参数的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。