偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

開發(fā) 前端
這樣,當(dāng)有用戶連接到WebSocket并發(fā)送消息時(shí),greeting?方法將被調(diào)用,處理邏輯并將更新后的在線用戶數(shù)發(fā)送到/topic/onlineUsers。

在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ān)的依賴和配置,然后創(chuàng)建WebSocket處理程序和相應(yīng)的服務(wù)類。

添加依賴

在pom.xml文件中添加WebSocket和Spring Boot的相關(guān)依賴:

<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的配置:

# WebSocket端口號(hào)
server.port=8080

# WebSocket端點(diǎn)
spring.websocket.endpoint=/ws

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

創(chuàng)建一個(gè)類來處理WebSocket連接和消息:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public 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通信的消息類:

public class WebSocketRequest {

    private String name;

    // Getter and Setter
}
javaCopy code
public class WebSocketResponse {

    private String content;

    public WebSocketResponse(String content) {
        this.content = content;
    }

    // Getter
}

配置WebSocket消息代理

在@SpringBootApplication注解的主應(yīng)用程序類中添加配置,以啟用WebSocket消息代理:

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
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // 啟用簡單的消息代理,以將消息發(fā)送到指定的前綴
        config.enableSimpleBroker("/topic");
        // 設(shè)置應(yīng)用程序的消息目標(biāo)前綴
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注冊一個(gè)WebSocket端點(diǎn),供客戶端連接
        registry.addEndpoint("/ws").withSockJS();
    }
}

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

創(chuàng)建一個(gè)服務(wù)類用于處理在線用戶的統(tǒng)計(jì)邏輯:

import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service
public 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);
    }

    // 用戶斷開連接時(shí)調(diào)用,將用戶ID從在線用戶集合中移除
    public void userDisconnected(String userId) {
        onlineUserIds.remove(userId);
    }

    // 獲取在線用戶數(shù)
    public int getOnlineUsersCount() {
        return onlineUserIds.size();
    }
}

更新WebSocket處理程序

更新WebSocketController類,使用服務(wù)類獲取在線用戶數(shù):

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public 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。

示例中完整代碼,可以從下面網(wǎng)址獲?。?/p>

https://gitee.com/jlearning/wechatdemo.git

https://github.com/icoderoad/wxdemo.git

責(zé)任編輯:武曉燕 來源: 路條編程
相關(guān)推薦

2024-06-12 08:46:19

2023-11-17 09:35:58

2024-09-02 09:31:19

2013-04-12 10:05:49

HTML5WebSocket

2023-07-17 18:42:47

gRPCDemo項(xiàng)目

2022-02-08 17:07:54

Spring BooSpring Aop日志記錄

2024-04-09 09:05:47

SpringRedis系統(tǒng)

2024-04-02 08:17:40

2020-05-28 07:15:00

機(jī)器學(xué)習(xí)TensorFlow人工智能

2012-12-25 09:36:11

Storm大數(shù)據(jù)分析

2023-03-30 07:48:46

接口鑒權(quán)SpringBoot

2018-05-11 15:36:43

數(shù)據(jù)科學(xué)算法數(shù)據(jù)分析

2009-06-15 16:23:39

Eclipse中使用SEclipse RCP

2024-09-29 10:39:48

RSocketWebSocket通信

2023-07-27 08:53:44

2022-07-26 16:54:08

QuartzJava

2024-12-03 08:00:00

2024-09-05 09:35:58

CGLIBSpring動(dòng)態(tài)代理

2025-07-01 07:34:03

2022-05-30 10:14:10

jupyter
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)