这期内容当中小编将会给大家带来有关使用WebSocket怎么实现数据库更新时前端页面刷新,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
通渭网站建设公司创新互联,通渭网站设计制作,有大型网站制作公司丰富经验。已为通渭成百上千提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的通渭做网站的公司定做!
WebSocketConfig:
package com.x.common.websocket; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
WebSocketServlet:
package com.x.common.websocket; import com.alibaba.fastjson.JSONObject; import org.springframework.stereotype.Component; import java.io.IOException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; @ServerEndpoint("/websocket/{userId}") @Component public class WebSocketServlet { private static int onlineCount = 0; private static Mapclients = new ConcurrentHashMap<>(); private Session session; private String userId; @OnOpen public void onOpen(@PathParam("userId") String userId, Session session) throws IOException { this.userId = userId; this.session = session; addOnlineCount(); clients.put(userId, this); System.out.println("已连接"); } @OnClose public void onClose() throws IOException { clients.remove(userId); subOnlineCount(); } @OnMessage public void onMessage(String message) throws IOException { JSONObject jsonTo = JSONObject.parseObject(message); if (!jsonTo.get("To").equals("All")){ sendMessageTo("给一个人", jsonTo.get("To").toString()); }else{ sendMessageAll("给所有人"); } } @OnError public void onError(Session session, Throwable error) { error.printStackTrace(); } public void sendMessageTo(String message, String To) throws IOException { // session.getBasicRemote().sendText(message); //session.getAsyncRemote().sendText(message); for (WebSocketServlet item : clients.values()) { if (item.userId.equals(To) ){ item.session.getAsyncRemote().sendText(message); } } } public void sendMessageAll(String message) throws IOException { for (WebSocketServlet item : clients.values()) { item.session.getAsyncRemote().sendText(message); } } public static synchronized int getOnlineCount() { return onlineCount; } public static synchronized void addOnlineCount() { WebSocketServlet.onlineCount++; } public static synchronized void subOnlineCount() { WebSocketServlet.onlineCount--; } public static synchronized Map getClients() { return clients; } }
JS代码:
var websocket = null; //判断当前浏览器是否支持WebSocket if ('WebSocket' in window) { websocket = new WebSocket("ws://localhost:8086/websocket/1"); } else { alert('当前浏览器 Not support websocket') } //连接发生错误的回调方法 websocket.onerror = function() { console.log("WebSocket连接发生错误"); }; //连接成功建立的回调方法 websocket.onopen = function() { console.log("WebSocket连接成功"); } //接收到消息的回调方法 websocket.onmessage = function(event) { //返回数据转JSON var json=JSON.parse(event.data); //result为bootstrap table 返回数据 var rows=result.rows; for(var i=0;i返回前台是调用方法:
@Autowired private WebSocketServlet scoket; //学生信息 XStudentInfoEntity student = xStudentInfoService.getObjectById(id.replace("\"","")); //提醒学生数据发生改变 scoket.sendMessageAll(JSONObject.toJSONString(student));pom.xml:
org.springframework spring-websocket 上述就是小编为大家分享的使用WebSocket怎么实现数据库更新时前端页面刷新了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
本文标题:使用WebSocket怎么实现数据库更新时前端页面刷新
浏览地址:http://bjjierui.cn/article/jdjios.html