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

當前位置:首頁 > 科技  > 軟件

在Spring Boot中使用WebSocket實現實時在線人數統計

來源: 責編: 時間:2023-11-28 09:37:05 233觀看
導讀在Spring Boot中使用WebSocket實現實時在線人數統計在Spring Boot中使用WebSocket實現實時在線人數統計可以通過以下步驟完成。首先,需要添加相關的依賴和配置,然后創建WebSocket處理程序和相應的服務類。添加依賴在pom

在Spring Boot中使用WebSocket實現實時在線人數統計eQW28資訊網——每日最新資訊28at.com

在Spring Boot中使用WebSocket實現實時在線人數統計可以通過以下步驟完成。首先,需要添加相關的依賴和配置,然后創建WebSocket處理程序和相應的服務類。eQW28資訊網——每日最新資訊28at.com

添加依賴

在pom.xml文件中添加WebSocket和Spring Boot的相關依賴:eQW28資訊網——每日最新資訊28at.com

<dependencies>    <!-- Spring Boot Starter Web包含了Spring MVC和其他相關依賴 -->    <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的配置:eQW28資訊網——每日最新資訊28at.com

# WebSocket端口號server.port=8080# WebSocket端點spring.websocket.endpoint=/ws

創建WebSocket處理程序

創建一個類來處理WebSocket連接和消息:eQW28資訊網——每日最新資訊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) {        // 處理收到的消息,這里可以更新在線用戶數等業務邏輯        // 在用戶連接時調用此方法        onlineUsersService.userConnected(request.getName());        int onlineUsers = onlineUsersService.getOnlineUsersCount();        WebSocketResponse response = new WebSocketResponse("當前在線人數:" + onlineUsers);        // 向客戶端發送更新后的在線用戶數        messagingTemplate.convertAndSendToUser(request.getName(), "/topic/onlineUsers", response);    }}

創建WebSocket消息類

創建用于WebSocket通信的消息類:eQW28資訊網——每日最新資訊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注解的主應用程序類中添加配置,以啟用WebSocket消息代理:eQW28資訊網——每日最新資訊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) {        // 啟用簡單的消息代理,以將消息發送到指定的前綴        config.enableSimpleBroker("/topic");        // 設置應用程序的消息目標前綴        config.setApplicationDestinationPrefixes("/app");    }    @Override    public void registerStompEndpoints(StompEndpointRegistry registry) {        // 注冊一個WebSocket端點,供客戶端連接        registry.addEndpoint("/ws").withSockJS();    }}

創建服務類

創建一個服務類用于處理在線用戶的統計邏輯:eQW28資訊網——每日最新資訊28at.com

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

更新WebSocket處理程序

更新WebSocketController類,使用服務類獲取在線用戶數:eQW28資訊網——每日最新資訊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) {        // 處理收到的消息,這里可以更新在線用戶數等業務邏輯        int onlineUsers = onlineUsersService.getOnlineUsersCount();        messagingTemplate.convertAndSend("/topic/onlineUsers", "當前在線人數:" + onlineUsers);    }}

這樣,當有用戶連接到WebSocket并發送消息時,greeting方法將被調用,處理邏輯并將更新后的在線用戶數發送到/topic/onlineUsers。eQW28資訊網——每日最新資訊28at.com

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

https://gitee.com/jlearning/wechatdemo.giteQW28資訊網——每日最新資訊28at.com

https://github.com/icoderoad/wxdemo.giteQW28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-34675-0.html在Spring Boot中使用WebSocket實現實時在線人數統計

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 站點可靠性工程SRE最佳實踐 -- 黃金監控信號

下一篇: Android使用SharedPreferences存儲輕量級持久化數據

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 库尔勒市| 彰武县| 雷州市| 长宁县| 南开区| 徐水县| 东光县| 天峨县| 兴国县| 永德县| 柳林县| 民乐县| 宝山区| 芦山县| 基隆市| 通辽市| 大兴区| 陆丰市| 桓仁| 阿鲁科尔沁旗| 南雄市| 惠水县| 葵青区| 威宁| 班戈县| 玛曲县| 密云县| 洱源县| 开平市| 泗阳县| 朝阳市| 墨脱县| 新野县| 柘城县| 广西| 随州市| 阜平县| 庆安县| 岳普湖县| 义马市| 漾濞|