此异常非彼异常,标题所说的异常是业务上的异常。
最近做了一个需求,消防的设备巡检,如果巡检发现异常,通过手机端提交,后台的实时监控页面实时获取到该设备的信息及位置,然后安排员工去处理。
因为需要服务端主动向客户端发送消息,所以很容易的就想到了用WebSocket来实现这一功能。
WebSocket就不做介绍了,上链接:https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket
前端略微复杂,需要在一张位置分布图上进行鼠标描点定位各个设备和根据不同屏幕大小渲染,本文不做介绍,只是简单地用页面样式进行效果呈现。
绿色代表正常,红色代表异常
预期效果,未接收到请求前----->id为3的提交了异常,id为3的王五变成了红色
实现: 前端:直接贴代码
1 2 3 4 5后端:实时监控 6 78 .item { 9 display: flex; 10 border-bottom: 1px solid #000000; 11 justify-content: space-between; 12 width: 30%; 13 line-height: 50px; 14 height: 50px; 15 } 16 17 .item span:nth-child(2){ 18 margin-right: 10px; 19 margin-top: 15px; 20 width: 20px; 21 height: 20px; 22 border-radius: 50%; 23 background: #55ff00; 24 } 25 .nowI{ 26 background: #ff0000 !important; 27 } 28 29 30 31 32 {{item.id}}.{{item.name}} 33 34 35 36 37 38 117
项目结构是这样子的,后面的代码关键注释都有,就不重复描述了
1、新建SpringBoot工程,选择web和WebSocket依赖
2、配置application.yml
1 2 3 4 5实时监控 6 78 .item { 9 display: flex; 10 border-bottom: 1px solid #000000; 11 justify-content: space-between; 12 width: 30%; 13 line-height: 50px; 14 height: 50px; 15 } 16 17 .item span:nth-child(2){ 18 margin-right: 10px; 19 margin-top: 15px; 20 width: 20px; 21 height: 20px; 22 border-radius: 50%; 23 background: #55ff00; 24 } 25 .nowI{ 26 background: #ff0000 !important; 27 } 28 29 30 31 32 {{item.id}}.{{item.name}} 33 34 35 36 37 38 117
3、WebSocketConfig配置类
1 @Configuration
2 public class WebSocketConfig {
3
4
7 @Bean
8 public ServerEndpointExporter serverEndpointExporter(){
9 return new ServerEndpointExporter();
10 }
11 }
4、WebSocketServer类,用来进行服务端和客户端之间的交互
1
5
6 @ServerEndpoint("/webSocket/{uid}")
7 @Component
8 public class WebSocketServer {
9
10 private static Logger log = LoggerFactory.getLogger(WebSocketServer.class);
11
12 //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
13 private static final AtomicInteger onlineNum = new AtomicInteger(0);
14
15 //concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。
16 private static CopyOnWriteArraySet sessionPools = new CopyOnWriteArraySet();
17
18
21 @onOpen
22 public void onOpen(Session session, @PathParam(value = "uid") String uid){
23 sessionPools.add(session);
24 onlineNum.incrementAndGet();
25 log.info(uid + "加入webSocket!当前人数为" + onlineNum);
26 }
27
28
31 @onClose
32 public void onClose(Session session) {
33 sessionPools.remove(session);
34 int cnt = onlineNum.decrementAndGet();
35 log.info("有连接关闭,当前连接数为:{}", cnt);
36 }
37
38
41 public void sendMessage(Session session, String message) throws IOException {
42 if(session != null){
43 synchronized (session) {
44 session.getBasicRemote().sendText(message);
45 }
46 }
47 }
48
49
52 public void broadCastInfo(String message) throws IOException {
53 for (Session session : sessionPools) {
54 if(session.isOpen()){
55 sendMessage(session, message);
56 }
57 }
58 }
59
60
63 @onError
64 public void onError(Session session, Throwable throwable){
65 log.error("发生错误");
66 throwable.printStackTrace();
67 }
68
69 }
5、WebSocketController类,用于进行接口测试
1 @RestController
2 @RequestMapping("/open/socket")
3 public class WebSocketController {
4
5 @Value("${mySocket.myPwd}")
6 public String myPwd;
7
8 @Autowired
9 private WebSocketServer webSocketServer;
10
11
17 @PostMapping(value = "/onReceive")
18 public void onReceive(String id,String pwd) throws IOException {
19 if(pwd.equals(myPwd)){ //密码校验一致(这里举例,实际开发还要有个密码加密的校验的),则进行群发
20 webSocketServer.broadCastInfo(id);
21 }
22 }
23
24 }
测试
1、打开前端页面,进行WebSocket连接
控制台输出,连接成功
2、因为是模拟数据,所以全部显示正常,没有异常提交时的页面呈现
3、接下来,我们用接口测试工具Postman提交一个异常
注意id为3的这个数据的状态变化
我们可以看到,id为3的王五状态已经变成异常的了,实时通讯成功。
参考:https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket
最后工作中有这方面关于实时监控的需求,可以参考一下哦。
不足之处欢迎指出,觉得有用的话就点个推荐吧!



