|
| 1 | +/* |
| 2 | + * Copyright 2012-2013 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 | + |
| 17 | +package samples.websocket.config; |
| 18 | + |
| 19 | +import org.springframework.boot.SpringApplication; |
| 20 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 21 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 22 | +import org.springframework.boot.context.web.SpringBootServletInitializer; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.web.socket.WebSocketHandler; |
| 26 | +import org.springframework.web.socket.config.annotation.EnableWebSocket; |
| 27 | +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; |
| 28 | +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; |
| 29 | +import org.springframework.web.socket.handler.PerConnectionWebSocketHandler; |
| 30 | + |
| 31 | +import samples.websocket.client.GreetingService; |
| 32 | +import samples.websocket.client.SimpleGreetingService; |
| 33 | +import samples.websocket.echo.DefaultEchoService; |
| 34 | +import samples.websocket.echo.EchoService; |
| 35 | +import samples.websocket.echo.EchoWebSocketHandler; |
| 36 | +import samples.websocket.snake.SnakeWebSocketHandler; |
| 37 | + |
| 38 | +@Configuration |
| 39 | +@EnableAutoConfiguration |
| 40 | +@EnableWebSocket |
| 41 | +public class SampleWebSocketsApplication extends SpringBootServletInitializer implements |
| 42 | + WebSocketConfigurer { |
| 43 | + |
| 44 | + @Override |
| 45 | + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { |
| 46 | + registry.addHandler(echoWebSocketHandler(), "/echo").withSockJS(); |
| 47 | + registry.addHandler(snakeWebSocketHandler(), "/snake").withSockJS(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { |
| 52 | + return application.sources(SampleWebSocketsApplication.class); |
| 53 | + } |
| 54 | + |
| 55 | + public static void main(String[] args) { |
| 56 | + SpringApplication.run(SampleWebSocketsApplication.class, args); |
| 57 | + } |
| 58 | + |
| 59 | + @Bean |
| 60 | + public EchoService echoService() { |
| 61 | + return new DefaultEchoService("Did you say \"%s\"?"); |
| 62 | + } |
| 63 | + |
| 64 | + @Bean |
| 65 | + public GreetingService greetingService() { |
| 66 | + return new SimpleGreetingService(); |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + public WebSocketHandler echoWebSocketHandler() { |
| 71 | + return new EchoWebSocketHandler(echoService()); |
| 72 | + } |
| 73 | + |
| 74 | + @Bean |
| 75 | + public WebSocketHandler snakeWebSocketHandler() { |
| 76 | + return new PerConnectionWebSocketHandler(SnakeWebSocketHandler.class); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments