1.pom文件添加websocket依赖
<!--webSocket依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-websocket</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
2.创建WebSocketConfig类文件
package XXX.websocket; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @EnableWebSocket @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
3.创建WebSocket文件
package XXX.websocket; import org.springframework.stereotype.Controller; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CopyOnWriteArraySet; //注意是controller注解 @Controller @ServerEndpoint("/websocket/{tableId}") public class WebSocket { private Session session; public static CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>(); private static Map<String, Session> sessionPool = new HashMap<String, Session>(); @OnOpen public void onOpen(Session session, @PathParam(value = "tableId") String code) { this.session = session; webSockets.add(this); sessionPool.put(code, session); // Constants.WEBSOCKET = true;//定义常量 是否开启websocket连接 System.out.println("【websocket消息】有新的连接,总数为:" + webSockets.size()); } @OnClose public void onClose() { webSockets.remove(this); //Constants.WEBSOCKET = false; System.out.println("【websocket消息】连接断开,总数为:" + webSockets.size()); } @OnMessage public void onMessage(String message) { System.out.println("【websocket消息】收到客户端消息:" + message); } // 此为广播消息 public void sendAllMessage(String message) { for (WebSocket webSocket : webSockets) { System.out.println("【websocket消息】广播消息:" + message); try { webSocket.session.getAsyncRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } } // 此为单点消息 public void sendOneMessage(String code, String message) { Session session = sessionPool.get(code); System.out.println(code); /*在发送数据之前先确认 session是否已经打开 使用session.isOpen() 为true 则发送消息 * 不然会报错:The WebSocket session [0] has been closed and no method (apart from close()) may be called on a closed session */ if (session != null && session.isOpen()) { try { session.getAsyncRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } } }
4.springBoot启动类上添加注解进行启动
@EnableDiscoveryClient
5.添加推送接口
@Autowired private WebSocket webSocketServer; // @Inner(value = false) @PostMapping("/pushCarWarningInfo") public R pushCarWarningInfo(String message,String license) { webSocketServer.sendAllMessage(message); return R.ok(message); }
6.测试页面html添加socket链接
socket = new WebSocket("ws:127.0.0.1:9003/websocket/12311");
7.打开页面,看是否socket open
8.给接口发送消息,看页面是否正常接收显示