pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/config/WebSocketConfig.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/pojo/MotorSystemInf.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/service/MotorSystemInfService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/ws/DeviceWebSocket.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/ws/WebSocketEncoder.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/resources/config/application.yml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
pom.xml
@@ -123,6 +123,11 @@ <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--websocket--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> </dependencies> <build> src/main/java/com/whyc/config/WebSocketConfig.java
New file @@ -0,0 +1,15 @@ package com.whyc.config; 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(); } } src/main/java/com/whyc/pojo/MotorSystemInf.java
@@ -32,6 +32,7 @@ private String devName; //设备名称 private String devIp; //设备ip private String note; //备用 private Integer position; //设备的固定标识 public Long getNum() { return num; @@ -88,4 +89,12 @@ public void setNote(String note) { this.note = note; } public Integer getPosition() { return position; } public void setPosition(Integer position) { this.position = position; } } src/main/java/com/whyc/service/MotorSystemInfService.java
@@ -1,6 +1,7 @@ package com.whyc.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.whyc.dto.Response; import com.whyc.mapper.MotorSystemInfMapper; import com.whyc.pojo.MotorSystemInf; @@ -20,7 +21,9 @@ public Response getAll() { try { List<MotorSystemInf> motorSystemInfs = mapper.selectList(null); QueryWrapper<MotorSystemInf> wrapper = Wrappers.query(); wrapper.orderByAsc("position"); List<MotorSystemInf> motorSystemInfs = mapper.selectList(wrapper); return new Response<>().set(1,motorSystemInfs); }catch (Exception e){ e.printStackTrace(); src/main/java/com/whyc/ws/DeviceWebSocket.java
New file @@ -0,0 +1,74 @@ package com.whyc.ws; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.io.IOException; @ServerEndpoint(value = "/device", encoders = WebSocketEncoder.class) @Component @Api(tags = "设备ws") @Slf4j public class DeviceWebSocket { private Session session; private volatile Thread thread; @OnOpen public void onOpen(Session session) { log.warn("socket会话开启了:{}",session); this.session = session; } @OnMessage public void onMessage(String message) { try { this.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } private void sendMessage(String message) { if(session!=null) { thread = new Thread("Thread_device") { public void run() { while (!thread.isInterrupted()) { try { if (session.isOpen()) { //session.getBasicRemote().sendObject("1"); session.getBasicRemote().sendObject(new Object()); } sleep(1000); } catch (IOException | InterruptedException | EncodeException e) { interrupt(); } } } }; thread.start(); } } @OnClose public void onClose() { if (thread != null && thread.isAlive()) { thread.interrupt(); } log.warn("webSocket会话关闭了:{}",session); } @OnError public void onError(Throwable error) { error.printStackTrace(); if (thread != null && thread.isAlive()) { thread.interrupt(); } } } src/main/java/com/whyc/ws/WebSocketEncoder.java
New file @@ -0,0 +1,31 @@ package com.whyc.ws; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.whyc.dto.Response; import javax.websocket.EncodeException; import javax.websocket.Encoder; import javax.websocket.EndpointConfig; /** * 编译器 */ public class WebSocketEncoder implements Encoder.Text<Response> { @Override public String encode(Response o) throws EncodeException { return new GsonBuilder().create().toJson(o); } @Override public void init(EndpointConfig endpointConfig) { } @Override public void destroy() { } } src/main/resources/config/application.yml
@@ -10,7 +10,7 @@ type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver # url: jdbc:mysql://localhost:3360/db_3.5mw_web?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true url: jdbc:mysql://192.168.10.221:3360/db_3.5mw_web?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true url: jdbc:mysql://192.168.10.79:3360/db_3.5mw_web?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true # url: jdbc:mysql://192.168.10.221:3360?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true # url: jdbc:mysql://192.168.10.222:3360/db_user?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true # url: jdbc:mysql://118.89.139.230:3360/db_user?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true