這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用WebSocket怎么實(shí)現(xiàn)數(shù)據(jù)庫更新時(shí)前端頁面刷新,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
通渭網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),通渭網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為通渭成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的通渭做網(wǎng)站的公司定做!
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("給一個(gè)人", 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; //判斷當(dāng)前瀏覽器是否支持WebSocket if ('WebSocket' in window) { websocket = new WebSocket("ws://localhost:8086/websocket/1"); } else { alert('當(dāng)前瀏覽器 Not support websocket') } //連接發(fā)生錯(cuò)誤的回調(diào)方法 websocket.onerror = function() { console.log("WebSocket連接發(fā)生錯(cuò)誤"); }; //連接成功建立的回調(diào)方法 websocket.onopen = function() { console.log("WebSocket連接成功"); } //接收到消息的回調(diào)方法 websocket.onmessage = function(event) { //返回?cái)?shù)據(jù)轉(zhuǎn)JSON var json=JSON.parse(event.data); //result為bootstrap table 返回?cái)?shù)據(jù) var rows=result.rows; for(var i=0;i返回前臺(tái)是調(diào)用方法:
@Autowired private WebSocketServlet scoket; //學(xué)生信息 XStudentInfoEntity student = xStudentInfoService.getObjectById(id.replace("\"","")); //提醒學(xué)生數(shù)據(jù)發(fā)生改變 scoket.sendMessageAll(JSONObject.toJSONString(student));pom.xml:
org.springframework spring-websocket 上述就是小編為大家分享的使用WebSocket怎么實(shí)現(xiàn)數(shù)據(jù)庫更新時(shí)前端頁面刷新了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱:使用WebSocket怎么實(shí)現(xiàn)數(shù)據(jù)庫更新時(shí)前端頁面刷新
標(biāo)題路徑:http://weahome.cn/article/jdjios.html