In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the method of passing parameters when websocket establishes a connection". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The method in parameter 4 obtained by backend:
Url of websocket
"ws:localhost:82/chat/123?username=ydh"
Comments on socket
@ ServerEndpoint ("/ chat/ {param}")
Open method of socket
OnOpenpublic void open (Session session, @ PathParam ("param") String param) {System.out.println ("user" + param+ "login"); Map 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);}
Result
User 123 logs in to session.getPathParameters () {param=123} session.getQueryString () username=baidusession.getRequestURI () .toString () / chat/123?username=baidu
1. Url site, which is obtained through @ PathParam annotation
2. Url site, which is obtained by session.getPathParameters ()
3. Url passes parameters through session.getQueryString ()
4. Url passes parameters through session.getRequestURI (). ToString ()
From the results, we can see that methods 1 and 2 do not need to be intercepted and can be used directly, while methods 3 and 4 need to intercept strings, which is a little troublesome, but the requested url is relatively refreshing.
Here is the complete code
Config code
Import javax.websocket.Endpoint; import javax.websocket.server.ServerApplicationConfig; import javax.websocket.server.ServerEndpointConfig; import java.util.Set; public class ChatConfig implements ServerApplicationConfig {/ / comments @ Override public Set websocket index.html
Maven
Javax.websocket javax.websocket-api 1.1 org.apache.tomcat tomcat-websocket 8.0.26 "how to pass parameters when websocket establishes a connection" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.