In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces SpringBoot how to use WebSocket, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
WebSocket is a protocol that HTML5 began to provide for full-duplex communication over a single TCP connection.
WebSocket makes it easier to exchange data between the client and the server, allowing the server to actively push data to the client. In WebSocket API, only one handshake is needed between the browser and the server, and a persistent connection can be directly created between the browser and the server for two-way data transmission.
In WebSocket API, the browser and the server only need to shake hands, and then a fast channel is formed between the browser and the server. Data can be transmitted directly between the two.
How to write java
Configure Bean
@ Bean public ServerEndpointExporter serverEndpointExporter () {return new ServerEndpointExporter ();}
Java link
/ * token is the identity of the front-end connection, and the backend maintains the binding between the user and webSocket according to this token * / @ Component@ServerEndpoint ("/ webSocket/ {token}") public class WebSocket {private Session session; private static ConcurrentHashMap webSocketMap = new ConcurrentHashMap (); @ OnOpen public void onOpen (@ PathParam (value = "token") String token, Session session) {this.session = session; webSocketMap.put (token, this); System.out.println ("new connect:" + token + ". Total: "+ webSocketMap.size ();} @ OnClose public void onClose (@ PathParam (value =" token ") String token) {webSocketMap.remove (token); System.out.println (" disconnect: "+ token +", total: "+ webSocketMap.size ());} @ OnMessage public void onMessage (String message) {System.out.println (" get message: "+ message) } public void sendMessage (String message) {/ / according to the business logic, find the webSocket WebSocket webSocket = webSocketMap.get (String.valueOf (message.charAt (0)) that token points to; / / cannot find the return if (webSocket = = null) {return;} try {webSocket.session.getBasicRemote (). SendText (message);} catch (IOException e) {e.printStackTrace ();}
How to write the front end?
/ * * WebSocket client * * usage instructions: * 1. WebSocket client receives server messages through callback function. For example, webSocket.onmessage * 2 and WebSocket clients send messages to the server through the send method. For example: webSocket.send (); * / function getWebSocket () {/ * WebSocket client PS:URL begins with domain name in WebSocket protocol and ends with server mapping address * / var webSocket = new WebSocket (/ * [[${webSocketUrl}]] * / 'ws://localhost:8080/webSocket/2') / * when the server opens the connection * / webSocket.onopen = function (event) {console.log ('WebSocket open the connection');}; / * when the server sends a message: 1. Broadcast message 2. Update online count * / webSocket.onmessage = function (event) {console.log ('WebSocket received message:% c' + event.data, 'color:green');}; / * * close connection * / webSocket.onclose = function (event) {console.log (' WebSocket close connection');}; / * * Communication failure * / webSocket.onerror = function (event) {console.log ('WebSocket exception');}; return webSocket }
Thank you for reading this article carefully. I hope the article "how to use WebSocket in SpringBoot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.