A Example Of Spring Boot WebSocket Maven 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 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 public class WebSocket { private static int onlineCount = 0 ; private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<WebSocket>(); private Session session; @OnOpen public void onOpen (Session session) throws IOException { this .session = session; webSocketSet.add(this ); addOnlineCount(); System.out.println("有新连接加入!当前在线人数为" + getOnlineCount()); this .sendMessage("连接成功!!!" ); } @OnClose public void onClose () { webSocketSet.remove(this ); subOnlineCount(); System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); } @OnMessage public void onMessage (String message, Session session) { Map<String, String> map = session.getPathParameters(); System.out.println("来自客户端用户" +map.get("userId" )+"的消息:" + message); for (WebSocket item: webSocketSet){ try { item.sendMessage(message); } catch (IOException e) { e.printStackTrace(); continue ; } } } @OnError public void onError (Session session, Throwable error) { System.out.println("发生错误" ); error.printStackTrace(); } public void sendMessage (String message) throws IOException { this .session.getBasicRemote().sendText(message); } public static synchronized int getOnlineCount () { return onlineCount; } public static synchronized void addOnlineCount () { WebSocket.onlineCount++; } public static synchronized void subOnlineCount () { WebSocket.onlineCount--; } public static CopyOnWriteArraySet<WebSocket> getWebSocketSet () { return webSocketSet; } public static void setWebSocketSet ( CopyOnWriteArraySet<WebSocket> webSocketSet) { WebSocket.webSocketSet = webSocketSet; } public Session getSession () { return session; } public void setSession (Session session) { this .session = session; } }
Annotation 注册服务能够在启动类运行时同时启动,@Component
使得类可以自动注入
1 2 @Component @ServerEndpoint ("/websocket/w" )
Transfer 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public void runMethod (String text) { CopyOnWriteArraySet<WebSocket> webSocketSet = WebSocket.getWebSocketSet(); if (webSocketSet.size() != 0 ){ for (WebSocket item : webSocketSet) { try { item.getSession().getBasicRemote().sendText(text); }catch (IOException e){ e.printStackTrace(); } } }else { System.out.println("WebSocket未连接" ); } }
Web 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 <!DOCTYPE HTML > <html xmlns:th ="http://www.thymeleaf.org" > <head > <title > My WebSocket</title > </head > <body > <input id ="text" type ="text" /> <button onclick ="send()" > Send</button > <button onclick ="closeWebSocket()" > Close</button > <div id ="message" > </div > </body > <script type ="text/javascript" > var websocket = null ; if ('WebSocket' in window ) { websocket = new WebSocket("ws://localhost:8080/websocket/w" ); } else { alert('Not support websocket' ) } websocket.onerror = function () { setMessageInnerHTML("error" ); }; websocket.onopen = function (event) { } websocket.onmessage = function (event) { setMessageInnerHTML(event.data); } websocket.onclose = function () { setMessageInnerHTML("close" ); } window .onbeforeunload = function ( ) { websocket.close(); } function setMessageInnerHTML (innerHTML) { document .getElementById('message' ).innerHTML += innerHTML + '<br/>' ; } function closeWebSocket () { websocket.close(); } function send () { var message = document .getElementById('text' ).value; websocket.send(message); } </script > </html >
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !