最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合WebSocket實(shí)現(xiàn)后端向前端主動(dòng)推送消息方式

 更新時(shí)間:2022年10月31日 10:47:42   作者:z.haoui  
這篇文章主要介紹了SpringBoot整合WebSocket實(shí)現(xiàn)后端向前端主動(dòng)推送消息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、引入websocket依賴

<dependency>
? ? <groupId>org.springframework.boot</groupId>
? ? <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

二、WebSocket配置

@Configuration
public class WebSocketConfig {
? ? @Bean
? ? public ServerEndpointExporter serverEndpointExporter() {
? ? ? ? return new ServerEndpointExporter();
? ? }
}

三、WebSocket服務(wù)

(前端連接地址ws://ip:端口/websocket,請(qǐng)自行替換ip、端口和接口名稱)

@ServerEndpoint(value = "/websocket")
@Component
public class WebSocketServer {
? ? private final static Logger log = LoggerFactory.getLogger(WebSocketServer.class);
?
? ? //靜態(tài)變量,用來記錄當(dāng)前在線連接數(shù)。應(yīng)該把它設(shè)計(jì)成線程安全的。
? ? private static int onlineCount = 0;
? ? //concurrent包的線程安全Set,用來存放每個(gè)客戶端對(duì)應(yīng)的MyWebSocket對(duì)象。
? ? private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
?
? ? //與某個(gè)客戶端的連接會(huì)話,需要通過它來給客戶端發(fā)送數(shù)據(jù)
? ? private Session session;
?
? ? /**
? ? ?* 連接建立成功調(diào)用的方法
? ? ?*/
? ? @OnOpen
? ? public void onOpen(Session session) {
? ? ? ? this.session = session;
? ? ? ? //加入set中
? ? ? ? webSocketSet.add(this);
? ? ? ? //在線數(shù)加1
? ? ? ? addOnlineCount();
? ? ? ? log.info("有新連接加入!當(dāng)前在線人數(shù)為" + getOnlineCount());
? ? ? ? try {
? ? ? ? ? ? sendMessage("連接成功");
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? log.error("websocket IO異常");
? ? ? ? }
? ? }
?
? ? /**
? ? ?* 連接關(guān)閉調(diào)用的方法
? ? ?*/
? ? @OnClose
? ? public void onClose() {
? ? ? ? //從set中刪除
? ? ? ? webSocketSet.remove(this);
? ? ? ? //在線數(shù)減1
? ? ? ? subOnlineCount();
? ? ? ? log.info("有一連接關(guān)閉!當(dāng)前在線人數(shù)為" + getOnlineCount());
? ? }
?
? ? /**
? ? ?* 收到客戶端消息后調(diào)用的方法
? ? ?*
? ? ?* @param message 客戶端發(fā)送過來的消息
? ? ?*/
? ? @OnMessage
? ? public void onMessage(String message, Session session) {
? ? ? ? log.info("來自客戶端的消息:" + message);
?
? ? ? ? //群發(fā)消息
? ? ? ? for (WebSocketServer item : webSocketSet) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? item.sendMessage(message);
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? /**
? ? ?* @param session
? ? ?* @param error
? ? ?*/
? ? @OnError
? ? public void onError(Session session, Throwable error) {
? ? ? ? log.error("發(fā)生錯(cuò)誤");
? ? ? ? error.printStackTrace();
? ? }
?
? ? public void sendMessage(String message) throws IOException {
? ? ? ? this.session.getBasicRemote().sendText(message);
? ? }
?
? ? /**
? ? ?* 群發(fā)自定義消息
? ? ?*/
? ? public static void sendInfo(String message) throws IOException {
? ? ? ? log.info(message);
? ? ? ? for (WebSocketServer item : webSocketSet) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? item.sendMessage(message);
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? public static synchronized int getOnlineCount() {
? ? ? ? return onlineCount;
? ? }
?
? ? public static synchronized void addOnlineCount() {
? ? ? ? WebSocketServer.onlineCount++;
? ? }
?
? ? public static synchronized void subOnlineCount() {
? ? ? ? WebSocketServer.onlineCount--;
? ? }
}

四、消息推送

后端調(diào)用WebServer的sendInfo接口(例如:WebSocketServer.sendInfo("Hello World");)實(shí)現(xiàn)主動(dòng)向前端推送消息

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

安丘市| 赤水市| 准格尔旗| 伽师县| 汉阴县| 和平县| 普安县| 丰镇市| 张掖市| 兰溪市| 资阳市| 务川| 和硕县| 固镇县| 天长市| 吴旗县| 九龙坡区| 吉首市| 绿春县| 惠水县| 万安县| 霍州市| 于田县| 高要市| 邳州市| 连南| 庆云县| 开封县| 来凤县| 盖州市| 汽车| 台北市| 商水县| 格尔木市| 深圳市| 茌平县| 通州市| 阿拉善右旗| 琼海市| 旌德县| 合山市|