栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

WebSocket实现数据库更新时前端页面刷新

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WebSocket实现数据库更新时前端页面刷新

本文实例为大家分享了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 Map 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 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号