WebSocket实现数据库更新时前端页⾯刷新本⽂实例为⼤家分享了WebSocket实现数据库更新时前端页⾯刷新,供⼤家参考,具体内容如下
后台代码:
WebSocketConfig:
websocket;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
WebSocketServlet:
websocket;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
import urrent.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;前端websocket怎么用
addOnlineCount();
clients.put(userId, this);
System.out.println("已连接");
}
@OnClose
public void onClose() throws IOException {
subOnlineCount();
}
@OnMessage
public void onMessage(String message) throws IOException {
JSONObject jsonTo = JSONObject.parseObject(message);
if (!("To").equals("All")){
sendMessageTo("给⼀个⼈", ("To").toString());
}else{
sendMessageAll("给所有⼈");
}
}
@OnError
public void onError(Session session, Throwable error) {
error.printStackTrace();
}
public void sendMessageTo(String message, String To) throws IOException { // BasicRemote().sendText(message);
//AsyncRemote().sendText(message);
for (WebSocketServlet item : clients.values()) {
if (item.userId.equals(To) ){
AsyncRemote().sendText(message);
}
}
}
public void sendMessageAll(String message) throws IOException {
for (WebSocketServlet item : clients.values()) {
AsyncRemote().sendText(message);
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
}
public static synchronized void subOnlineCount() {
}
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')
}
//连接发⽣错误的回调⽅法
console.log("WebSocket连接发⽣错误");
};
//连接成功建⽴的回调⽅法
console.log("WebSocket连接成功");
}
/
/接收到消息的回调⽅法
//返回数据转JSON
var json=JSON.parse(event.data);
//result为bootstrap table 返回数据
var ws;
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);
}
//连接关闭的回调⽅法
console.log("WebSocket连接关闭");
}
//监听窗⼝关闭事件,当窗⼝关闭时,主动去关闭websocket连接,防⽌连接还没断开就关闭窗⼝,server端会抛异常。beforeunload = function() {
closeWebSocket();
}
//关闭WebSocket连接
function closeWebSocket() {
websocket.close();
}
返回前台是调⽤⽅法:
@Autowired
private WebSocketServlet scoket;
//学⽣信息
XStudentInfoEntity student = place("\"",""));
//提醒学⽣数据发⽣改变
scoket.JSONString(student));
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
</dependency>
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论