本篇内容介绍了“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建立连接时传递参数的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
本文题目:websocket建立连接时传递参数的方法
网页路径:https://www.cdcxhl.com/article28/pdihcp.html
成都网站建设公司_创新互联,为您提供品牌网站建设、用户体验、网站收录、网站排名、移动网站建设、品牌网站设计
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联