目录
[TOC]
参考
https://blog.csdn.net/hjq_ku/article/details/127503180
依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
|
配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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(); } }
|
WebSocket处理类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;
import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet;
@Component @ServerEndpoint("/websocket/{userId}") public class WebSocketServiceImpl {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private Session session;
private String userId;
private static CopyOnWriteArraySet<WebSocketServiceImpl> webSockets = new CopyOnWriteArraySet<>();
private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<String, Session>();
@OnOpen public void onOpen(Session session, @PathParam(value = "userId") String userId) { try { this.session = session; this.userId = userId; webSockets.add(this); sessionPool.put(userId, session); logger.info("【websocket消息】有新的连接,总数为:" + webSockets.size()); } catch (Exception e) { } }
@OnClose public void onClose() { try { webSockets.remove(this); sessionPool.remove(this.userId); logger.info("【websocket消息】连接断开,总数为:" + webSockets.size()); } catch (Exception e) { } }
@OnMessage public void onMessage(String message) { logger.info("【websocket消息】收到客户端消息:" + message); }
@OnError public void onError(Session session, Throwable error) { logger.error("用户错误,原因:" + error.getMessage()); error.printStackTrace(); }
public void sendAllMessage(String message) { logger.info("【websocket消息】广播消息:" + message); for (WebSocketServiceImpl webSocket : webSockets) { try { if (webSocket.session.isOpen()) { webSocket.session.getAsyncRemote().sendText(message); } } catch (Exception e) { e.printStackTrace(); } } }
public void sendOneMessage(String userId, String message) { Session session = sessionPool.get(userId); if (session != null && session.isOpen()) { try { logger.info("【websocket消息】 单点消息:" + message); session.getAsyncRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } }
public void sendMoreMessage(String[] userIds, String message) { for (String userId : userIds) { Session session = sessionPool.get(userId); if (session != null && session.isOpen()) { try { logger.info("【websocket消息】 单点消息:" + message); session.getAsyncRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } }
} }
|
给客户端返回消息接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@RestController @RequestMapping(value = "/api/v1/websocket") @Api(tags = "websocket接口", value = "AlarmDpController") public class WebSocketController { @Autowired private WebSocketServer webSocketServer;
@ApiOperation(value = "模拟数据发送", notes = "模拟数据发送") @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", name = "message", value = "模拟消息", required = true, dataType = "String"), }) @RequestMapping(value = "/sendTestMessage", method = RequestMethod.GET) public void sendTestMessage(String message) { webSocketServer.sendAllMessage(); } }
|
前端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>websocket通讯</title> </head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> let socket; function openSocket() { const socketUrl = "ws://localhost:8003/websocket/admin"; console.log(socketUrl); if(socket!=null){ socket.close(); socket=null; } socket = new WebSocket(socketUrl); socket.onopen = function() { console.log("websocket已打开"); }; socket.onmessage = function(msg) { console.log(msg.data); }; socket.onclose = function() { console.log("websocket已关闭"); }; socket.onerror = function() { console.log("websocket发生了错误"); } } function sendMessage() {
socket.send('{"toUserId":"'+$("#toUserId").val()+'","contentText":"'+$("#contentText").val()+'"}'); console.log('{"toUserId":"'+$("#toUserId").val()+'","contentText":"'+$("#contentText").val()+'"}'); } </script> <body> <p>【socket开启者的ID信息】:<div><input id="userId" name="userId" type="text" value="10"></div> <p>【客户端向服务器发送的内容】:<div><input id="toUserId" name="toUserId" type="text" value="20"> <input id="contentText" name="contentText" type="text" value="hello websocket"></div> <p>【操作】:<button><a onclick="openSocket()">开启socket</a></button> <p>【操作】:<button><a onclick="sendMessage()">发送消息</a></button> </body>
</html>
|