Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use websocket to realize front-end communication

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use websocket to achieve front and back-end communication, 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 communication is a fun and useful way to communicate, as follows:

The first step is to introduce dependencies in the pom.xml file because springboot integrates websocket well.

Org.springframework.boot spring-boot-starter-websocket

The second step is to use websocket, which is written in the HTML file, in the front-end interface

Var websocket = null; if ('WebSocket' in window) {websocket = new WebSocket (' ws://yesell.natapp1.cc/sell/webSocket');} else {alert ('this browser does not support websocketers');} websocket.onopen = function (event) {console.log ('establish connection');} websocket.onclose = function (event) {console.log ('connection closed') } websocket.onmessage = function (event) {console.log ('received message:' + event.data) / / Action to be performed} websocket.onerror = function () {alert ('websocket communication error!') ;} window.onbeforeunload = function () {websocket.close ();}

The third step, generally speaking, we implement the interaction in the controller layer, while the interaction in websocket is in the service layer.

Where:

@ ServerEndpoint ("/ webSocket") is the address that defines the interaction @ Slf4j is a log. If you are interested in learning about it, please see this article. The three methods https://www.cnblogs.com/yemengshen/p/11478293.html@OnOpen, @ OnClose and @ OnMessage interact with the three front-end methods of the same name. Call the method at the location where you need to use it as follows, and you are almost done here. @ Component@ServerEndpoint ("/ webSocket") @ Slf4jpublic class WebSocket {private Session session; private static CopyOnWriteArraySet webSocketSet=new CopyOnWriteArraySet (); @ OnOpen public void onOpen (Session session) {this.session=session; webSocketSet.add (this); log.info ("[websocket message] has new connections, total: {}", webSocketSet.size ());} @ OnClose public void onClose () {webSocketSet.remove (this) Log.info ("[websocket message]] disconnected, total: {}", webSocketSet.size ());} @ OnMessage public void onMessage (String message) {log.info ("[websocket message] received message from client: {}", message) } public void sendMessage (String message) {for (WebSocket webSocket:webSocketSet) {log.info ("[websocket message] broadcast message: {}", message); try {webSocket.session.getBasicRemote (). SendText (message);} catch (IOException e) {e.printStackTrace ();}

Mode of use:

@ Autowiredprivate WebSocket webSocket;webSocket.sendMessage ("transmitted parameters"); thank you for reading this article carefully. I hope the article "how to use websocket to achieve front-end communication" shared by the editor will be helpful to you. At the same time, I also hope 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report