原文 https://blog.csdn.net/qq_48721706/article/details/124995148
引入maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
配置文件
mport org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration public class WebSocketConfig { /** * 注入ServerEndpointExporter, * 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } @Bean public CleanSock webSocketClient() { return new CleanSock(); } }
server端
package co.yixiang.modules.order.service.impl; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.util.Collections; import java.util.HashSet; import java.util.Set; @ServerEndpoint("/websocket") @Component public class WebSocketServer { private static Set<Session> sessions = Collections.synchronizedSet(new HashSet<>()); @OnOpen //onOpen方法在新客户端连接时被调用,将客户端会话添加到会话集合中 public void onOpen(Session session) { sessions.add(session); System.out.println("New client connected: " + session.getId()); } @OnMessage //onMessage方法在接收到客户端发送的消息时被调用,并将该消息广播给所有客户端 public void onMessage(String message, Session session) { System.out.println("Received message from client: " + message); broadcastMessage(message); } @OnClose //onClose方法在客户端断开连接时被调用,从会话集合中移除该客户端 public void onClose(Session session) { sessions.remove(session); System.out.println("Client disconnected: " + session.getId()); } //broadcastMessage方法用于将消息广播给所有客户端 private void broadcastMessage(String message) { for (Session session : sessions) { try { session.getBasicRemote().sendText(message); } catch (IOException e) { e.printStackTrace(); } } } @OnError public void onError(Session session, Throwable throwable) { throwable.printStackTrace(); } }
java模拟客户端
package co.yixiang.modules.order.service.impl; import org.springframework.stereotype.Component; import javax.websocket.*; import java.net.URI; import java.util.Scanner; @ClientEndpoint @Component public class CleanSock { private Session session; @OnOpen public void onOpen(Session session) { this.session = session; System.out.println("Connected to server: " + session.getId()); } @OnMessage public void onMessage(String message) { System.out.println("Received message from server: " + message); } @OnClose public void onClose(Session session, CloseReason closeReason) { System.out.println("Connection closed: " + closeReason.getReasonPhrase()); } public void sendMessage(String message) { if (session != null && session.isOpen()) { session.getAsyncRemote().sendText(message); } else { System.out.println("Connection is not open"); } } public static void doSend(String msg) { String serverUrl = "ws://localhost:8002/websocket"; // WebSocket服务器地址 WebSocketContainer container = ContainerProvider.getWebSocketContainer(); try { CleanSock client = new CleanSock(); Session session = container.connectToServer(client, URI.create(serverUrl)); client.onOpen(session); client.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } }