In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what you need to pay attention to when using webSocket. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. What is webSocket?
A network communication protocol that HTML5 began to provide for full-duplex communication over a single TCP connection.
Defect of HTTP protocol: communication can only be initiated by the client, and the server can not actively send resources to the client.
If you want to know whether the server is ready for resources, you have to use polling to request, which is a waste of resources (because you have to connect constantly, or the HTTP connection is always open). Websocket solves this problem and can achieve two-way communication.
WebSocket currently supports two unified resource markers, ws and wss, similar to HTTP and HTTPS, mainly encrypted and unencrypted protocols.
One picture understands the principle difference between HTTP and webSocket:
2. Notes and use of webSocket
Common notes:
@ ServerEndpoint (value = "/ webSocketProxy/ {nickName}")
Represents the path of a URI map, and the standard restfulAPI, {nickName} is the path parameter. Used to annotate classes.
@ OnOpen @ OnMessage @ OnClose @ OnError
The above four annotations are used in the method and correspond to different events: open the connection, receive a message, close the connection, and an error occurs. When a corresponding event occurs, the method will be triggered. Business development is usually done mainly within the @ OnMessage method in development.
@ PathParam ("nickName")
This annotation is used to get the path parameter {nickName}.
Five connection states:
NOT_YET_CONNECTED, CONNECTING, OPEN, CLOSING, CLOSED
A simple instance of the server
@ ServerEndpoint (value = "/ customWebSocket/ {nickName}") @ Component@Slf4jpublic class CustomWebSocket {/ / is used to store the MyWebSocket object global object private static CopyOnWriteArraySet user = new CopyOnWriteArraySet () corresponding to each client; / / A connection session with a client through which data private Session session is sent to the client. @ OnMessage public void onMessage (String message, Session session, @ PathParam ("nickName") String nickName) throws IOException {/ / Group message for (CustomWebSocket myWebSocket: user) {log.info ("request url:" + session.getRequestURI ()); myWebSocket.session.getBasicRemote () .sendText (nickName + ":" + message) Log.info ("custom received message:" + nickName + ":" + message);}} @ OnOpen public void onOpen (Session session) {log.info ("custom" + session.getId () + "open..."); this.session = session; user.add (this) } @ OnClose public void onClose () {System.out.println ("custom" + this.session.getId () + "close..."); session=null; user.remove (this);} @ OnError public void onError (Session session, Throwable error) {session=null Log.warn ("custom" + this.session.getId () + "error...", error);} 3. Considerations of WebSocket when the WebSocket server is managed by spring, the default is singleton, but for WebSocket objects, each connection creates a new object, so you need to pay attention to the elegant use of the static keyword. One session corresponds to one session.
Pay special attention to this code, using the static decoration is a class attribute, which is used to store client information for all connections. Static CopyOnWriteArraySet user = new CopyOnWriteArraySet ()
If an error is reported: The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method, then there is a situation where multiple threads send messages concurrently. The synchronization mechanism can be used.
Synchronized (session) {session.getBasicRemote (). SendText (JSON.toJSONString (message));} this is the end of the article on "what you need to pay attention to when using webSocket". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.