WebSocketConfig.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright 2002-2016 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample.config;
  17. import org.springframework.context.annotation.Bean;
  18. import org.springframework.context.annotation.Configuration;
  19. import org.springframework.messaging.simp.SimpMessageSendingOperations;
  20. import org.springframework.messaging.simp.config.MessageBrokerRegistry;
  21. import org.springframework.scheduling.annotation.EnableScheduling;
  22. import org.springframework.session.ExpiringSession;
  23. import org.springframework.session.web.socket.config.annotation.AbstractSessionWebSocketMessageBrokerConfigurer;
  24. import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
  25. import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
  26. import sample.data.ActiveWebSocketUserRepository;
  27. import sample.websocket.WebSocketConnectHandler;
  28. import sample.websocket.WebSocketDisconnectHandler;
  29. @Configuration
  30. @EnableScheduling
  31. @EnableWebSocketMessageBroker
  32. public class WebSocketConfig<S extends ExpiringSession> extends
  33. AbstractSessionWebSocketMessageBrokerConfigurer<S> {
  34. @Override
  35. protected void configureStompEndpoints(StompEndpointRegistry registry) {
  36. registry.addEndpoint("/chat").withSockJS();
  37. }
  38. @Override
  39. public void configureMessageBroker(MessageBrokerRegistry registry) {
  40. registry.enableSimpleBroker("/queue/", "/topic/");
  41. registry.setApplicationDestinationPrefixes("/app");
  42. }
  43. @Bean
  44. public WebSocketConnectHandler webSocketConnectHandler(
  45. SimpMessageSendingOperations messagingTemplate,
  46. ActiveWebSocketUserRepository repository) {
  47. return new WebSocketConnectHandler(messagingTemplate, repository);
  48. }
  49. @Bean
  50. public WebSocketDisconnectHandler webSocketDisconnectHandler(
  51. SimpMessageSendingOperations messagingTemplate,
  52. ActiveWebSocketUserRepository repository) {
  53. return new WebSocketDisconnectHandler(messagingTemplate, repository);
  54. }
  55. }