java部分:
import org.springframework.stereotype.Component; 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.util.HashMap; import java.util.Map; import java.util.concurrent.CopyOnWriteArraySet; /** * @description:TODO * @author:xing.Li * @date:2019/8/26 10:02 */ @Component @ServerEndpoint("/websocket/{code}") //此注解相当于设置访问URL public class WebSocket { private Session session; private static CopyOnWriteArraySet<WebSocket> webSockets =new CopyOnWriteArraySet<>(); private static Map<String,Session> sessionPool = new HashMap<String,Session>(); @OnOpen public void onOpen(Session session, @PathParam(value="code")String code) { this.session = session; webSockets.add(this); sessionPool.put(code, session); Constants.WEBSOCKET = true;//定义常亮是否开启websocket连接 System.out.println("【websocket消息】有新的连接,总数为:"+webSockets.size()); } @OnClose public void onClose() { webSockets.remove(this); Constants.WEBSOCKET = false; System.out.println("【websocket消息】连接断开,总数为:"+webSockets.size()); } @OnMessage public void onMessage(String message) { System.out.println("【websocket消息】收到客户端消息:"+message); } // 此为广播消息 public void sendAllMessage(String message) { for(WebSocket webSocket : webSockets) { System.out.println("【websocket消息】广播消息:"+message); try { webSocket.session.getAsyncRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } } // 此为单点消息 public void sendOneMessage(String code, String message) { Session session = sessionPool.get(code); /*在发送数据之前先确认 session是否已经打开 使用session.isOpen() 为true 则发送消息 * 不然会报错:The WebSocket session [0] has been closed and no method (apart from close()) may be called on a closed session */ if (session != null && session.isOpen()) { try { session.getAsyncRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } }
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * @description:TODO * @author:xing.Li * @date:2019/8/25 17:44 */ @Configuration public class WebSocketConfig { /** * 注入ServerEndpointExporter, * 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
最终要的当然是jar包了
<!--websocket--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>1.5.3.RELEASE</version> <type>pom</type> </dependency>
js部分:
data(){} 中
created() { // 页面创建生命周期函数 this.initWebSocket() }, destroyed: function () { // 离开页面生命周期函数 this.websocketclose(); },
methods中
initWebSocket: function () { // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https key你自定义的key var host = window.location.host; this.websock = new WebSocket("ws://" + host + "/websocket/key"); this.websock.onopen = this.websocketonopen; this.websock.onerror = this.websocketonerror; this.websock.onmessage = this.websocketonmessage; this.websock.onclose = this.websocketclose; }, websocketonopen: function () { console.log("WebSocket连接成功"); console.log("WebSocket正在发送实时数据......."); }, websocketonerror: function (e) { console.log("WebSocket连接发生错误"); }, websocketonmessage: function (e) { JSON.parse(e.data); //这个是收到后端主动推送的值 } }, websocketclose: function (e) { console.log("WebSocket连接断开"); }
实现思路:通过websocket长连接从后端主动推送数据,利用vue的双向绑定实现数据同步
注:因为数据的改变也会刷新表格上复选框的选中状态,本人思路为,element ui中有选中复选框的值得集合,判断复选框里面有没有值,有值的话就是在操作数据,这时停止websocket的连接,操作完成后,复选框值所在集合也就没有值了,这时再开启websocket连接,这样也有问题,但是算是实现了吧。。。。
在此记录方便以后观看
当然上面的部分代码是忘记那位博主的了,时间太久找不到链接了,就不放原文链接了