# spring-boot-demo-websocket > 此 demo 主要演示了 Spring Boot 如何集成 WebSocket,实现后端主动往前端推送数据。网上大部分websocket的例子都是聊天室,本例主要是推送服务器状态信息。前端页面基于vue和element-ui实现。 ## 1. 代码 ### 1.1. pom.xml ```xml 4.0.0 spring-boot-demo-websocket 1.0.0-SNAPSHOT spring-boot-demo-websocket Demo project for Spring Boot com.xkcoding spring-boot-demo 1.0.0-SNAPSHOT UTF-8 UTF-8 1.8 3.9.1 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-websocket org.springframework.boot spring-boot-starter-test test com.github.oshi oshi-core ${oshi.version} cn.hutool hutool-all com.google.guava guava org.projectlombok lombok true spring-boot-demo-websocket org.springframework.boot spring-boot-maven-plugin ``` ### 1.2. WebSocketConfig.java ```java /** *

* WebSocket配置 *

* * @author yangkai.shen * @date Created in 2018-12-14 15:58 */ @Configuration @EnableWebSocket @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { // 注册一个 /notification 端点,前端通过这个端点进行连接 registry.addEndpoint("/notification") //解决跨域问题 .setAllowedOrigins("*") .withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { //定义了一个客户端订阅地址的前缀信息,也就是客户端接收服务端发送消息的前缀信息 registry.enableSimpleBroker("/topic"); } } ``` ### 1.3. 服务器相关实体 > 此部分实体 参见包路径 [com.xkcoding.websocket.model](./src/main/java/com/xkcoding/websocket/model) ### 1.4. ServerTask.java ```java /** *

* 服务器定时推送任务 *

* * @author yangkai.shen * @date Created in 2018-12-14 16:04 */ @Slf4j @Component public class ServerTask { @Autowired private SimpMessagingTemplate wsTemplate; /** * 按照标准时间来算,每隔 2s 执行一次 */ @Scheduled(cron = "0/2 * * * * ?") public void websocket() throws Exception { log.info("【推送消息】开始执行:{}", DateUtil.formatDateTime(new Date())); // 查询服务器状态 Server server = new Server(); server.copyTo(); ServerVO serverVO = ServerUtil.wrapServerVO(server); Dict dict = ServerUtil.wrapServerDict(serverVO); wsTemplate.convertAndSend(WebSocketConsts.PUSH_SERVER, JSONUtil.toJsonStr(dict)); log.info("【推送消息】执行结束:{}", DateUtil.formatDateTime(new Date())); } } ``` ### 1.5. server.html ```html 服务器信息
手动连接 断开连接
CPU信息
内存信息
服务器信息
Java虚拟机信息
磁盘状态
``` ## 2. 运行方式 1. 启动 `SpringBootDemoWebsocketApplication.java` 2. 访问 http://localhost:8080/demo/server.html ## 3. 运行效果 ![image-20181217110240322](http://static.xkcoding.com/spring-boot-demo/websocket/064107.jpg) ![image-20181217110304065](http://static.xkcoding.com/spring-boot-demo/websocket/064108.jpg) ![image-20181217110328810](http://static.xkcoding.com/spring-boot-demo/websocket/064109.jpg) ![image-20181217110336017](http://static.xkcoding.com/spring-boot-demo/websocket/064109-1.jpg) ## 4. 参考 ### 4.1. 后端 1. Spring Boot 整合 Websocket 官方文档:https://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/web.html#websocket 2. 服务器信息采集 oshi 使用:https://github.com/oshi/oshi ### 4.2. 前端 1. vue.js 语法:https://cn.vuejs.org/v2/guide/ 2. element-ui 用法:http://element-cn.eleme.io/#/zh-CN 3. stomp.js 用法:https://github.com/jmesnil/stomp-websocket 4. sockjs 用法:https://github.com/sockjs/sockjs-client 5. axios.js 用法:https://github.com/axios/axios#example