最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WebSocket實現(xiàn)數(shù)據(jù)庫更新時前端頁面刷新

 更新時間:2019年04月07日 16:21:48   作者:落日流年  
這篇文章主要為大家詳細介紹了WebSocket實現(xiàn)數(shù)據(jù)庫更新時前端頁面刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了WebSocket實現(xiàn)數(shù)據(jù)庫更新時前端頁面刷新,供大家參考,具體內(nèi)容如下

后臺代碼:

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 Map<String, WebSocketServlet> clients = 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<String, WebSocketServlet> 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')
}
 
//連接發(fā)生錯誤的回調(diào)方法
websocket.onerror = function() {
 console.log("WebSocket連接發(fā)生錯誤");
};
 
//連接成功建立的回調(diào)方法
websocket.onopen = function() {
 console.log("WebSocket連接成功");
}
 
//接收到消息的回調(diào)方法
websocket.onmessage = function(event) {
 //返回數(shù)據(jù)轉(zhuǎn)JSON
 var json=JSON.parse(event.data);
 //result為bootstrap table 返回數(shù)據(jù)
 var rows=result.rows;
 for(var i=0;i<rows.length;i++){
 var row=rows[i];
 if(row.id==json.id){
 //判斷列Id相同時刷新表格
 //$('#dataGrid').bootstrapTable('updateByUniqueId', {index: i, row: row});'refresh'
 $('#dataGrid').bootstrapTable('refresh');
 }
 }
 console.log(event.data);
}
 
//連接關(guān)閉的回調(diào)方法
websocket.onclose = function() {
 console.log("WebSocket連接關(guān)閉");
}
 
//監(jiān)聽窗口關(guān)閉事件,當窗口關(guān)閉時,主動去關(guān)閉websocket連接,防止連接還沒斷開就關(guān)閉窗口,server端會拋異常。
window.onbeforeunload = function() {
 closeWebSocket();
}
 
//關(guān)閉WebSocket連接
function closeWebSocket() {
 websocket.close();
}

返回前臺是調(diào)用方法:

@Autowired
private WebSocketServlet scoket;
 
//學生信息
XStudentInfoEntity student = xStudentInfoService.getObjectById(id.replace("\"",""));
//提醒學生數(shù)據(jù)發(fā)生改變
scoket.sendMessageAll(JSONObject.toJSONString(student));

pom.xml: 

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-websocket</artifactId>
</dependency>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

图木舒克市| 峡江县| 黄浦区| 大连市| 余江县| 红原县| 利津县| 抚宁县| 蓬溪县| 那坡县| 来安县| 汝南县| 永济市| 黄石市| 桂阳县| 东方市| 房山区| 汝州市| 常山县| 长顺县| 镇远县| 诸城市| 岑巩县| 乌兰县| 肇源县| 麻阳| 昌乐县| 肥乡县| 河北省| 加查县| 齐齐哈尔市| 九江市| 海晏县| 厦门市| 邻水| 资溪县| 万全县| 石狮市| 陆良县| 达尔| 辽阳县|