您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Java中Springboot websocket怎么用,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
WebSocket是一種在單個(gè)TCP連接上進(jìn)行全雙工通信的協(xié)議 …
如果說(shuō),連接隨意創(chuàng)建,不管的話,會(huì)存在錯(cuò)誤,broken pipe
表面看單純報(bào)錯(cuò),并沒什么功能缺陷等,但實(shí)際,請(qǐng)求數(shù)增加,容易導(dǎo)致系統(tǒng)奔潰。這邊畫重點(diǎn)。
出現(xiàn)原因有很多種,目前我這邊出現(xiàn)的原因,是因?yàn)榭蛻舳艘殃P(guān)閉連接,服務(wù)端還持續(xù)推送導(dǎo)致。
下面將使用springboot集成的webSocket
導(dǎo)入Maven
首先SpringBoot版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent>
集成websocket
// 加個(gè)web集成吧 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
主要用來(lái)監(jiān)控客戶端握手連接進(jìn)來(lái)以及揮手關(guān)閉連接
需要一個(gè)管理Socket的類
package com.li.manager; import lombok.extern.slf4j.Slf4j; import org.springframework.web.socket.WebSocketSession; import java.util.concurrent.ConcurrentHashMap; /** * socket管理器 */ @Slf4j public class SocketManager { private static ConcurrentHashMap<String, WebSocketSession> manager = new ConcurrentHashMap<String, WebSocketSession>(); public static void add(String key, WebSocketSession webSocketSession) { log.info("新添加webSocket連接 {} ", key); manager.put(key, webSocketSession); } public static void remove(String key) { log.info("移除webSocket連接 {} ", key); manager.remove(key); } public static WebSocketSession get(String key) { log.info("獲取webSocket連接 {}", key); return manager.get(key); } }
package com.li.factory; import com.li.manager.SocketManager; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.WebSocketHandlerDecorator; import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory; import java.security.Principal; /** * 服務(wù)端和客戶端在進(jìn)行握手揮手時(shí)會(huì)被執(zhí)行 */ @Component @Slf4j public class WebSocketDecoratorFactory implements WebSocketHandlerDecoratorFactory { @Override public WebSocketHandler decorate(WebSocketHandler handler) { return new WebSocketHandlerDecorator(handler) { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { log.info("有人連接啦 sessionId = {}", session.getId()); Principal principal = session.getPrincipal(); if (principal != null) { log.info("key = {} 存入", principal.getName()); // 身份校驗(yàn)成功,緩存socket連接 SocketManager.add(principal.getName(), session); } super.afterConnectionEstablished(session); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { log.info("有人退出連接啦 sessionId = {}", session.getId()); Principal principal = session.getPrincipal(); if (principal != null) { // 身份校驗(yàn)成功,移除socket連接 SocketManager.remove(principal.getName()); } super.afterConnectionClosed(session, closeStatus); } }; } }
getId() : 返回的是唯一的會(huì)話標(biāo)識(shí)符。
getPrincipal() : 經(jīng)過(guò)身份驗(yàn)證,返回Principal實(shí)例,未經(jīng)過(guò)身份驗(yàn)證,返回null
Principal: 委托人的抽象概念,可以是公司id,名字,用戶唯一識(shí)別token等
package com.li.handler; import lombok.extern.slf4j.Slf4j; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.server.support.DefaultHandshakeHandler; import javax.servlet.http.HttpServletRequest; import java.security.Principal; import java.util.Map; /** * 我們可以通過(guò)請(qǐng)求信息,比如token、或者session判用戶是否可以連接,這樣就能夠防范非法用戶 */ @Slf4j @Component public class PrincipalHandshakeHandler extends DefaultHandshakeHandler { @Override protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) { /** * 這邊可以按你的需求,如何獲取唯一的值,既unicode * 得到的值,會(huì)在監(jiān)聽處理連接的屬性中,既WebSocketSession.getPrincipal().getName() * 也可以自己實(shí)現(xiàn)Principal() */ if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest servletServerHttpRequest = (ServletServerHttpRequest) request; HttpServletRequest httpRequest = servletServerHttpRequest.getServletRequest(); /** * 這邊就獲取你最熟悉的陌生人,攜帶參數(shù),你可以cookie,請(qǐng)求頭,或者url攜帶,這邊我采用url攜帶 */ final String token = httpRequest.getParameter("token"); if (StringUtils.isEmpty(token)) { return null; } return new Principal() { @Override public String getName() { return token; } }; } return null; } }
package com.li.config; import com.li.factory.WebSocketDecoratorFactory; import com.li.handler.PrincipalHandshakeHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration; /** * WebSocketConfig配置 */ @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Autowired private WebSocketDecoratorFactory webSocketDecoratorFactory; @Autowired private PrincipalHandshakeHandler principalHandshakeHandler; @Override public void registerStompEndpoints(StompEndpointRegistry registry) { /** * myUrl表示 你前端到時(shí)要對(duì)應(yīng)url映射 */ registry.addEndpoint("/myUrl") .setAllowedOrigins("*") .setHandshakeHandler(principalHandshakeHandler) .withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { /** * queue 點(diǎn)對(duì)點(diǎn) * topic 廣播 * user 點(diǎn)對(duì)點(diǎn)前綴 */ registry.enableSimpleBroker("/queue", "/topic"); registry.setUserDestinationPrefix("/user"); } @Override public void configureWebSocketTransport(WebSocketTransportRegistration registration) { registration.addDecoratorFactory(webSocketDecoratorFactory); super.configureWebSocketTransport(registration); } }
package com.li.controller; import com.li.manager.SocketManager; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.socket.WebSocketSession; import java.util.Map; @RestController @Slf4j public class TestController { @Autowired private SimpMessagingTemplate template; /** * 服務(wù)器指定用戶進(jìn)行推送,需要前端開通 var socket = new SockJS(host+'/myUrl' + '?token=1234'); */ @RequestMapping("/sendUser") public void sendUser(String token) { log.info("token = {} ,對(duì)其發(fā)送您好", token); WebSocketSession webSocketSession = SocketManager.get(token); if (webSocketSession != null) { /** * 主要防止broken pipe */ template.convertAndSendToUser(token, "/queue/sendUser", "您好"); } } /** * 廣播,服務(wù)器主動(dòng)推給連接的客戶端 */ @RequestMapping("/sendTopic") public void sendTopic() { template.convertAndSend("/topic/sendTopic", "大家晚上好"); } /** * 客戶端發(fā)消息,服務(wù)端接收 * * @param message */ // 相當(dāng)于RequestMapping @MessageMapping("/sendServer") public void sendServer(String message) { log.info("message:{}", message); } /** * 客戶端發(fā)消息,大家都接收,相當(dāng)于直播說(shuō)話 * * @param message * @return */ @MessageMapping("/sendAllUser") @SendTo("/topic/sendTopic") public String sendAllUser(String message) { // 也可以采用template方式 return message; } /** * 點(diǎn)對(duì)點(diǎn)用戶聊天,這邊需要注意,由于前端傳過(guò)來(lái)json數(shù)據(jù),所以使用@RequestBody * 這邊需要前端開通var socket = new SockJS(host+'/myUrl' + '?token=4567'); token為指定name * @param map */ @MessageMapping("/sendMyUser") public void sendMyUser(@RequestBody Map<String, String> map) { log.info("map = {}", map); WebSocketSession webSocketSession = SocketManager.get(map.get("name")); if (webSocketSession != null) { log.info("sessionId = {}", webSocketSession.getId()); template.convertAndSendToUser(map.get("name"), "/queue/sendUser", map.get("message")); } } }
可以直接啟動(dòng)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Spring Boot WebSocket+廣播式</title> </head> <body> <noscript> <h3 >貌似你的瀏覽器不支持websocket</h3> </noscript> <div> <div> <button id="connect" onclick="connect()">連接</button> <button id="disconnect" onclick="disconnect();">斷開連接</button> </div> <div id="conversationDiv"> <label>輸入你的名字</label> <input type="text" id="name" /> <br> <label>輸入消息</label> <input type="text" id="messgae" /> <button id="send" onclick="send();">發(fā)送</button> <p id="response"></p> </div> </div> <script src="https://cdn.bootcss.com/sockjs-client/1.1.4/sockjs.min.js"></script> <script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.min.js"></script> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> var stompClient = null; //gateway網(wǎng)關(guān)的地址 var host="http://127.0.0.1:8888"; function setConnected(connected) { document.getElementById('connect').disabled = connected; document.getElementById('disconnect').disabled = !connected; document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden'; $('#response').html(); } // SendUser *********************************************** function connect() { //地址+端點(diǎn)路徑,構(gòu)建websocket鏈接地址,注意,對(duì)應(yīng)config配置里的addEndpoint var socket = new SockJS(host+'/myUrl' + '?token=4567'); stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { setConnected(true); console.log('Connected:' + frame); //監(jiān)聽的路徑以及回調(diào) stompClient.subscribe('/user/queue/sendUser', function(response) { showResponse(response.body); }); }); } /* function connect() { //地址+端點(diǎn)路徑,構(gòu)建websocket鏈接地址,注意,對(duì)應(yīng)config配置里的addEndpoint var socket = new SockJS(host+'/myUrl'); stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { setConnected(true); console.log('Connected:' + frame); //監(jiān)聽的路徑以及回調(diào) stompClient.subscribe('/topic/sendTopic', function(response) { showResponse(response.body); }); }); }*/ function disconnect() { if (stompClient != null) { stompClient.disconnect(); } setConnected(false); console.log("Disconnected"); } function send() { var name = $('#name').val(); var message = $('#messgae').val(); /*//發(fā)送消息的路徑,由客戶端發(fā)送消息到服務(wù)端 stompClient.send("/sendServer", {}, message); */ /*// 發(fā)送給所有廣播sendTopic的人,客戶端發(fā)消息,大家都接收,相當(dāng)于直播說(shuō)話 注:連接需開啟 /topic/sendTopic stompClient.send("/sendAllUser", {}, message); */ /* 這邊需要注意,需要啟動(dòng)不同的前端html進(jìn)行測(cè)試,需要改不同token ,例如 token=1234,token=4567 * 然后可以通過(guò)寫入name 為token 進(jìn)行指定用戶發(fā)送 */ stompClient.send("/sendMyUser", {}, JSON.stringify({name:name,message:message})); } function showResponse(message) { var response = $('#response'); response.html(message); } </script> </body> </html>
關(guān)于“Java中Springboot websocket怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。