WebSocketConfig.java 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. package com.miniframe.websocket;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.socket.WebSocketHandler;
  5. import org.springframework.web.socket.config.annotation.EnableWebSocket;
  6. import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
  7. import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
  8. /**
  9. *
  10. * @author Goma
  11. *
  12. */
  13. @Configuration
  14. @EnableWebSocket
  15. public class WebSocketConfig implements WebSocketConfigurer {
  16. @Override
  17. public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
  18. registry.addHandler(myhandler(), "/websocket").addInterceptors(myInterceptors()).setAllowedOrigins("*");
  19. registry.addHandler(myhandler(), "/sockjs/websocket").addInterceptors(myInterceptors()).withSockJS();
  20. }
  21. @Bean
  22. public WebSocketHandler myhandler() {
  23. return new WebsocketEndPoint();
  24. }
  25. @Bean
  26. public HandShakeInterceptor myInterceptors() {
  27. return new HandShakeInterceptor();
  28. }
  29. }