日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

在Spring Boot中使用WebSocket實(shí)現(xiàn)實(shí)時(shí)在線人數(shù)統(tǒng)計(jì)

來(lái)源: 責(zé)編: 時(shí)間:2023-11-28 09:37:05 274觀看
導(dǎo)讀在Spring Boot中使用WebSocket實(shí)現(xiàn)實(shí)時(shí)在線人數(shù)統(tǒng)計(jì)在Spring Boot中使用WebSocket實(shí)現(xiàn)實(shí)時(shí)在線人數(shù)統(tǒng)計(jì)可以通過(guò)以下步驟完成。首先,需要添加相關(guān)的依賴和配置,然后創(chuàng)建WebSocket處理程序和相應(yīng)的服務(wù)類。添加依賴在pom

在Spring Boot中使用WebSocket實(shí)現(xiàn)實(shí)時(shí)在線人數(shù)統(tǒng)計(jì)VbX28資訊網(wǎng)——每日最新資訊28at.com

在Spring Boot中使用WebSocket實(shí)現(xiàn)實(shí)時(shí)在線人數(shù)統(tǒng)計(jì)可以通過(guò)以下步驟完成。首先,需要添加相關(guān)的依賴和配置,然后創(chuàng)建WebSocket處理程序和相應(yīng)的服務(wù)類。VbX28資訊網(wǎng)——每日最新資訊28at.com

添加依賴

在pom.xml文件中添加WebSocket和Spring Boot的相關(guān)依賴:VbX28資訊網(wǎng)——每日最新資訊28at.com

<dependencies>    <!-- Spring Boot Starter Web包含了Spring MVC和其他相關(guān)依賴 -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <!-- Spring Boot Starter WebSocket -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-websocket</artifactId>    </dependency></dependencies>

配置WebSocket

在application.properties文件中添加WebSocket的配置:VbX28資訊網(wǎng)——每日最新資訊28at.com

# WebSocket端口號(hào)server.port=8080# WebSocket端點(diǎn)spring.websocket.endpoint=/ws

創(chuàng)建WebSocket處理程序

創(chuàng)建一個(gè)類來(lái)處理WebSocket連接和消息:VbX28資訊網(wǎng)——每日最新資訊28at.com

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.simp.SimpMessagingTemplate;import org.springframework.stereotype.Controller;@Controllerpublic class WebSocketController {    private final SimpMessagingTemplate messagingTemplate;    private final OnlineUsersService onlineUsersService;    @Autowired    public WebSocketController(SimpMessagingTemplate messagingTemplate, OnlineUsersService onlineUsersService) {        this.messagingTemplate = messagingTemplate;        this.onlineUsersService = onlineUsersService;    }    @MessageMapping("/hello")    public void greeting(WebSocketRequest request) {        // 處理收到的消息,這里可以更新在線用戶數(shù)等業(yè)務(wù)邏輯        // 在用戶連接時(shí)調(diào)用此方法        onlineUsersService.userConnected(request.getName());        int onlineUsers = onlineUsersService.getOnlineUsersCount();        WebSocketResponse response = new WebSocketResponse("當(dāng)前在線人數(shù):" + onlineUsers);        // 向客戶端發(fā)送更新后的在線用戶數(shù)        messagingTemplate.convertAndSendToUser(request.getName(), "/topic/onlineUsers", response);    }}

創(chuàng)建WebSocket消息類

創(chuàng)建用于WebSocket通信的消息類:VbX28資訊網(wǎng)——每日最新資訊28at.com

public class WebSocketRequest {    private String name;    // Getter and Setter}javaCopy codepublic class WebSocketResponse {    private String content;    public WebSocketResponse(String content) {        this.content = content;    }    // Getter}

配置WebSocket消息代理

在@SpringBootApplication注解的主應(yīng)用程序類中添加配置,以啟用WebSocket消息代理:VbX28資訊網(wǎng)——每日最新資訊28at.com

import org.springframework.context.annotation.Configuration;import org.springframework.messaging.simp.config.MessageBrokerRegistry;import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;import org.springframework.web.socket.config.annotation.StompEndpointRegistry;import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;@Configuration@EnableWebSocketMessageBrokerpublic class WebSocketConfig implements WebSocketMessageBrokerConfigurer {    @Override    public void configureMessageBroker(MessageBrokerRegistry config) {        // 啟用簡(jiǎn)單的消息代理,以將消息發(fā)送到指定的前綴        config.enableSimpleBroker("/topic");        // 設(shè)置應(yīng)用程序的消息目標(biāo)前綴        config.setApplicationDestinationPrefixes("/app");    }    @Override    public void registerStompEndpoints(StompEndpointRegistry registry) {        // 注冊(cè)一個(gè)WebSocket端點(diǎn),供客戶端連接        registry.addEndpoint("/ws").withSockJS();    }}

創(chuàng)建服務(wù)類

創(chuàng)建一個(gè)服務(wù)類用于處理在線用戶的統(tǒng)計(jì)邏輯:VbX28資訊網(wǎng)——每日最新資訊28at.com

import org.springframework.stereotype.Service;import java.util.HashSet;import java.util.Set;@Servicepublic class OnlineUsersService {    // 使用Set存儲(chǔ)在線用戶的唯一標(biāo)識(shí),例如用戶ID    private final Set<String> onlineUserIds = new HashSet<>();    // 用戶連接時(shí)調(diào)用,將用戶ID添加到在線用戶集合中    public void userConnected(String userId) {        onlineUserIds.add(userId);    }    // 用戶斷開(kāi)連接時(shí)調(diào)用,將用戶ID從在線用戶集合中移除    public void userDisconnected(String userId) {        onlineUserIds.remove(userId);    }    // 獲取在線用戶數(shù)    public int getOnlineUsersCount() {        return onlineUserIds.size();    }}

更新WebSocket處理程序

更新WebSocketController類,使用服務(wù)類獲取在線用戶數(shù):VbX28資訊網(wǎng)——每日最新資訊28at.com

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.simp.SimpMessagingTemplate;import org.springframework.stereotype.Controller;@Controllerpublic class WebSocketController {    private final SimpMessagingTemplate messagingTemplate;    private final OnlineUsersService onlineUsersService;    @Autowired    public WebSocketController(SimpMessagingTemplate messagingTemplate, OnlineUsersService onlineUsersService) {        this.messagingTemplate = messagingTemplate;        this.onlineUsersService = onlineUsersService;    }    @MessageMapping("/hello")    public void greeting(WebSocketRequest request) {        // 處理收到的消息,這里可以更新在線用戶數(shù)等業(yè)務(wù)邏輯        int onlineUsers = onlineUsersService.getOnlineUsersCount();        messagingTemplate.convertAndSend("/topic/onlineUsers", "當(dāng)前在線人數(shù):" + onlineUsers);    }}

這樣,當(dāng)有用戶連接到WebSocket并發(fā)送消息時(shí),greeting方法將被調(diào)用,處理邏輯并將更新后的在線用戶數(shù)發(fā)送到/topic/onlineUsers。VbX28資訊網(wǎng)——每日最新資訊28at.com

示例中完整代碼,可以從下面網(wǎng)址獲取:VbX28資訊網(wǎng)——每日最新資訊28at.com

https://gitee.com/jlearning/wechatdemo.gitVbX28資訊網(wǎng)——每日最新資訊28at.com

https://github.com/icoderoad/wxdemo.gitVbX28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-34675-0.html在Spring Boot中使用WebSocket實(shí)現(xiàn)實(shí)時(shí)在線人數(shù)統(tǒng)計(jì)

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 站點(diǎn)可靠性工程SRE最佳實(shí)踐 -- 黃金監(jiān)控信號(hào)

下一篇: Android使用SharedPreferences存儲(chǔ)輕量級(jí)持久化數(shù)據(jù)

標(biāo)簽:
  • 熱門(mén)焦點(diǎn)
Top 主站蜘蛛池模板: 永康市| 廊坊市| 武胜县| 墨脱县| 九江市| 罗源县| 通河县| 民和| 杭州市| 福泉市| 宜良县| 长沙市| 石楼县| 双鸭山市| 永胜县| 密山市| 樟树市| 枣阳市| 武功县| 略阳县| 罗平县| 宁都县| 万载县| 定结县| 特克斯县| 湟源县| 凉山| 宿迁市| 凤冈县| 南澳县| 琼结县| 郧西县| 铁岭市| 亳州市| 环江| 苍溪县| 鞍山市| 葵青区| 汕尾市| 赤壁市| 泰宁县|