spring boot + mybatis + websocket + js实战

项目技术:spring boot + mybatis + websocket + js

需求背景:当添加一个女孩时,页面的socket收到消息,打印最新的所有女生list,这样可以进一步在react/vue前端框架下,实现当A用户新增了某业务数据后,B用户的该业务list页面能自动将最新的业务数据list清单刷新出来,而不是需要点击刷新按钮才能刷出最新业务数据list。

准入条件:先学习如下二篇文档

SpringBoot 集成WebSocket详解-CSDN博客

spring boot学习第三篇:spring boot与mybatis plus结合-CSDN博客 

然后在这2篇文档的基础上继续学习。

 1、在GirlController.java文件中添加如下代码:

@Autowired
private WebSocketServer webSocketServer;

然后在addGirl方法中添加如下代码:

List list = girlService.findAll();
String str = JSON.toJSONString(list);
webSocketServer.sendToAll(str);

代码截图如下:

spring boot + mybatis + websocket + js实战_第1张图片

2、然后,WebSocketServer.java的sendToAll方法要做如下改动:

spring boot + mybatis + websocket + js实战_第2张图片3、然后启动服务做验证

3.1、插入数据

spring boot + mybatis + websocket + js实战_第3张图片

3.2、查看各个页面的console

user-1页面截图如下:

spring boot + mybatis + websocket + js实战_第4张图片 

user-2 页面截图如下:

spring boot + mybatis + websocket + js实战_第5张图片

 user-3页面截图如下:

spring boot + mybatis + websocket + js实战_第6张图片

你可能感兴趣的:(Spring,Boot,spring,boot,mybatis,websocket)