这是我的 WebSocketConfiguration 代码
@EnableWebSocketMessageBroker
@Configuration
@RequiredArgsConstructor
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/my-chat").setAllowedOriginPatterns("*");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/pub");
registry.enableSimpleBroker("/sub");
}
}
然后我连接 STOMP Connection 。 网址为“ws://localhost:8080/my-chat”,连接类型:STOMP,订阅网址:“sub/hello”。
我已经检查了 Spring Output 终端的连接。
[MessageBroker-8] o.s.w.s.c.WebSocketMessageBrokerStats : WebSocketSession[2 current WS(2)-HttpStream(0)-HttpPoll(0), 3 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(1)-CONNECTED(1)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 9, active threads = 0, queued tasks = 0, completed tasks = 9], outboundChannel[pool size = 2, active threads = 0, queued tasks = 0, completed tasks = 2], sockJsScheduler[pool size = 8, active threads = 1, queued tasks = 1, completed tasks = 60]
但是,当我无法在 Spring 代码中使用会话编号时。 这是我的代码。
@Autowired
private SimpUserRegistry simpUserRegistry;
@Scheduled(fixedRate = 1000)
public void getConnectedUserSessions() {
System.out.println(simpUserRegistry.getUserCount());
}
它总是打印 0。 我怎样才能获得连接的踩踏会话?