詳解spring boot實(shí)現(xiàn)websocket
前言
QQ這類即時(shí)通訊工具多數(shù)是以桌面應(yīng)用的方式存在。在沒(méi)有websocket出現(xiàn)之前,如果開(kāi)發(fā)一個(gè)網(wǎng)頁(yè)版的即時(shí)通訊應(yīng)用,則需要定時(shí)刷新頁(yè)面或定時(shí)調(diào)用ajax請(qǐng)求,這無(wú)疑會(huì)加大服務(wù)器的負(fù)載和增加了客戶端的流量。而websocket的出現(xiàn),則完美的解決了這些問(wèn)題。
spring boot對(duì)websocket進(jìn)行了封裝,這對(duì)實(shí)現(xiàn)一個(gè)websocket網(wǎng)頁(yè)即時(shí)通訊應(yīng)用來(lái)說(shuō),變得非常簡(jiǎn)單。
一、準(zhǔn)備工作
pom.xml引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
完整的pom.xml文件代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>spring-boot-16</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-16</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、代碼編寫(xiě)
1.創(chuàng)建名為“WebSocketConfig.java”的類來(lái)配置websocket,并繼承抽象類“AbstractWebSocketMessageBrokerConfigurer”
此類聲明“@EnableWebSocketMessageBroker”的注解
package com.example; 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; @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/my-websocket").withSockJS(); } }
這里配置了以“/app”開(kāi)頭的websocket請(qǐng)求url。和名為“my-websocket”的endpoint(端點(diǎn))
2.編寫(xiě)一個(gè)DTO類來(lái)承載消息:
package com.example; public class SocketMessage { public String message; public String date; }
3.創(chuàng)建App.java類,用于啟用spring boot和用于接收、發(fā)送消息的控制器。
package com.example; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller @EnableScheduling @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Autowired private SimpMessagingTemplate messagingTemplate; @GetMapping("/") public String index() { return "index"; } @MessageMapping("/send") @SendTo("/topic/send") public SocketMessage send(SocketMessage message) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); message.date = df.format(new Date()); return message; } @Scheduled(fixedRate = 1000) @SendTo("/topic/callback") public Object callback() throws Exception { // 發(fā)現(xiàn)消息 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); messagingTemplate.convertAndSend("/topic/callback", df.format(new Date())); return "callback"; } }
“send”方法用于接收客戶端發(fā)送過(guò)來(lái)的websocket請(qǐng)求。
@EnableScheduling注解為:?jiǎn)⒂胹pring boot的定時(shí)任務(wù),這與“callback”方法相呼應(yīng),用于每隔1秒推送服務(wù)器端的時(shí)間。
4.在“resources/templates”目錄下創(chuàng)建index.html文件:
<!DOCTYPE html> <html> <head> <title>玩轉(zhuǎn)spring boot——websocket</title> <script src="http://cdn.bootcss.com/angular.js/1.5.6/angular.min.js"></script> <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 type="text/javascript"> /*<![CDATA[*/ var stompClient = null; var app = angular.module('app', []); app.controller('MainController', function($rootScope, $scope, $http) { $scope.data = { //連接狀態(tài) connected : false, //消息 message : '', rows : [] }; //連接 $scope.connect = function() { var socket = new SockJS('/my-websocket'); stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { // 注冊(cè)發(fā)送消息 stompClient.subscribe('/topic/send', function(msg) { $scope.data.rows.push(JSON.parse(msg.body)); $scope.data.connected = true; $scope.$apply(); }); // 注冊(cè)推送時(shí)間回調(diào) stompClient.subscribe('/topic/callback', function(r) { $scope.data.time = '當(dāng)前服務(wù)器時(shí)間:' + r.body; $scope.data.connected = true; $scope.$apply(); }); $scope.data.connected = true; $scope.$apply(); }); }; $scope.disconnect = function() { if (stompClient != null) { stompClient.disconnect(); } $scope.data.connected = false; } $scope.send = function() { stompClient.send("/app/send", {}, JSON.stringify({ 'message' : $scope.data.message })); } }); /*]]>*/ </script> </head> <body ng-app="app" ng-controller="MainController"> <h2>玩轉(zhuǎn)spring boot——websocket</h2> <h4> 出處:劉冬博客 <a rel="external nofollow" >http://www.cnblogs.com/goodhelper</a> </h4> <label>WebSocket連接狀態(tài):</label> <button type="button" ng-disabled="data.connected" ng-click="connect()">連接</button> <button type="button" ng-click="disconnect()" ng-disabled="!data.connected">斷開(kāi)</button> <br /> <br /> <div ng-show="data.connected"> <label>{{data.time}}</label> <br /> <br /> <input type="text" ng-model="data.message" placeholder="請(qǐng)輸入內(nèi)容..." /> <button ng-click="send()" type="button">發(fā)送</button> <br /> <br /> 消息列表: <br /> <table> <thead> <tr> <th>內(nèi)容</th> <th>時(shí)間</th> </tr> </thead> <tbody> <tr ng-repeat="row in data.rows"> <td>{{row.message}}</td> <td>{{row.date}}</td> </tr> </tbody> </table> </div> </body> </html>
除了引用angular.js的CDN文件外,還需要引用sockjs和stomp。
完整的項(xiàng)目結(jié)構(gòu),如下圖所示:
三、運(yùn)行效果
點(diǎn)擊“連接”按鈕,出現(xiàn)發(fā)送消息的輸入框。并接收到服務(wù)器端的時(shí)間推送。
輸入發(fā)送內(nèi)容并點(diǎn)擊“發(fā)送”按鈕后,頁(yè)面顯示出剛才發(fā)送的消息。
點(diǎn)擊“斷開(kāi)”按鈕,則服務(wù)器端不會(huì)再推送消息。
總結(jié)
在開(kāi)發(fā)一個(gè)基于web的即時(shí)通訊應(yīng)用的過(guò)程中,我們還需考慮session的機(jī)制。
還需要一個(gè)集合來(lái)承載當(dāng)前的在線用戶,并做一個(gè)定時(shí)任務(wù),其目的是用輪詢的方式定時(shí)處理在線用戶的狀態(tài),有哪些用戶在線,又有哪些用戶離線。
參考:
http://spring.io/guides/gs/scheduling-tasks/
http://spring.io/guides/gs/messaging-stomp-websocket/
代碼地址:https://github.com/carter659/spring-boot-16
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 關(guān)于Spring Boot WebSocket整合以及nginx配置詳解
- SpringBoot+Websocket實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)聊天功能代碼
- Spring Boot 開(kāi)發(fā)私有即時(shí)通信系統(tǒng)(WebSocket)
- 詳解spring boot Websocket使用筆記
- SpringBoot webSocket實(shí)現(xiàn)發(fā)送廣播、點(diǎn)對(duì)點(diǎn)消息和Android接收
- 詳解在Spring Boot框架下使用WebSocket實(shí)現(xiàn)消息推送
- 使用 Spring Boot 實(shí)現(xiàn) WebSocket實(shí)時(shí)通信
相關(guān)文章
SpringMVC學(xué)習(xí)之JSTL條件行為和遍歷行為詳解
這篇文章主要介紹了SpringMVC學(xué)習(xí)之JSTL條件行為和遍歷行為詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java C++解決在排序數(shù)組中查找數(shù)字出現(xiàn)次數(shù)問(wèn)題
本文終于介紹了分別通過(guò)Java和C++實(shí)現(xiàn)統(tǒng)計(jì)一個(gè)數(shù)字在排序數(shù)組中出現(xiàn)的次數(shù)。文中詳細(xì)介紹了實(shí)現(xiàn)思路,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2021-12-12JUC中的wait與notify方法實(shí)現(xiàn)原理詳解
這篇文章主要介紹了JUC中的wait與notify方法實(shí)現(xiàn)原理,在進(jìn)行wait()之前,就代表著需要爭(zhēng)奪Synchorized,而Synchronized代碼塊通過(guò)javap生成的字節(jié)碼中包含monitor?enter和monitor?exit兩個(gè)指令2023-03-03關(guān)于BindingResult的使用總結(jié)及注意事項(xiàng)
這篇文章主要介紹了關(guān)于BindingResult的使用總結(jié)及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12IntelliJ IDEA使用maven實(shí)現(xiàn)tomcat的熱部署
這篇文章主要介紹了IntelliJ IDEA使用maven實(shí)現(xiàn)tomcat的熱部署,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之隊(duì)列
這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之隊(duì)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01