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

公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出甘谷免費做網(wǎng)站回饋大家。
在Spring Boot中使用WebSocket實現(xiàn)實時在線人數(shù)統(tǒng)計可以通過以下步驟完成。首先,需要添加相關(guān)的依賴和配置,然后創(chuàng)建WebSocket處理程序和相應(yīng)的服務(wù)類。
添加依賴
在pom.xml文件中添加WebSocket和Spring Boot的相關(guān)依賴:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-websocket
配置WebSocket
在application.properties文件中添加WebSocket的配置:
# WebSocket端口號
server.port=8080
# WebSocket端點
spring.websocket.endpoint=/ws
創(chuàng)建WebSocket處理程序
創(chuàng)建一個類來處理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ù)邏輯
// 在用戶連接時調(diào)用此方法
onlineUsersService.userConnected(request.getName());
int onlineUsers = onlineUsersService.getOnlineUsersCount();
WebSocketResponse response = new WebSocketResponse("當前在線人數(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)用程序的消息目標前綴
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// 注冊一個WebSocket端點,供客戶端連接
registry.addEndpoint("/ws").withSockJS();
}
}
創(chuàng)建服務(wù)類
創(chuàng)建一個服務(wù)類用于處理在線用戶的統(tǒng)計邏輯:
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Set;
@Service
public class OnlineUsersService {
// 使用Set存儲在線用戶的唯一標識,例如用戶ID
private final Set onlineUserIds = new HashSet<>();
// 用戶連接時調(diào)用,將用戶ID添加到在線用戶集合中
public void userConnected(String userId) {
onlineUserIds.add(userId);
}
// 用戶斷開連接時調(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", "當前在線人數(shù):" + onlineUsers);
}
}
這樣,當有用戶連接到WebSocket并發(fā)送消息時,greeting方法將被調(diào)用,處理邏輯并將更新后的在線用戶數(shù)發(fā)送到/topic/onlineUsers。
示例中完整代碼,可以從下面網(wǎng)址獲取:
https://gitee.com/jlearning/wechatdemo.git
https://github.com/icoderoad/wxdemo.git
新聞名稱:在SpringBoot中使用WebSocket實現(xiàn)實時在線人數(shù)統(tǒng)計
網(wǎng)站地址:http://m.fisionsoft.com.cn/article/dhohoeo.html


咨詢
建站咨詢
