0
目前在我的應用程序中,每10秒鐘會發送一個消息與Spring網絡套接字。這就是我的Spring應用程序中向用戶廣播的消息。Websocket使用Spring網絡套接字向SockJS廣播不同的用戶
@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
@Component
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Autowired
private SimpMessagingTemplate template;
private TaskScheduler scheduler = new ConcurrentTaskScheduler();
public WebSocketConfig() {
System.out.printf(" ---INIT----------");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/simplemessages").withSockJS();
}
// @Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic/", "/queue/");
config.setApplicationDestinationPrefixes("/app");
}
@PostConstruct
private void broadcastTimePeriodically() {
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
try{
template.convertAndSend("/topic/simplemessagesresponse", "{shares:true,price:100.00}");
}catch(MessagingException e){
System.err.println("!!!!!! websocket timer error :>"+e.toString());
}
}
}, 10000);
}
@PreDestroy
private void destroyServices(){
}
// @Override
public void configureClientInboundChannel(ChannelRegistration registration) {
}
// @Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.taskExecutor().corePoolSize(4).maxPoolSize(10);
}
//@Override
public boolean configureMessageConverters(List<MessageConverter> arg0) {
// TODO Auto-generated method stub
return true;
}
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration arg0) {
// TODO Auto-generated method stub
}
}
這是瀏覽器如何接收,
var socket = new SockJS(desz);
stompClient = Stomp.over(socket);
stompClient.connect('user', 'guest', function(frame) {
stompClient.subscribe("/topic/simplemessagesresponse", function(servermessage) {
var stompResponse = JSON.parse((servermessage.body));
console.log('server msg: '+stompResponse);
});
});
我要廣播相同的消息給一些用戶,而另一組用戶定期有另一條消息。我應該如何修改我的上述代碼來實現這一目標?