diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index 84288a653e2..f524b1d5557 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -26,7 +26,6 @@ spring-boot-sample-traditional spring-boot-sample-web-static spring-boot-sample-web-ui - spring-boot-sample-websocket spring-boot-sample-xml diff --git a/spring-boot-samples/spring-boot-sample-websocket/pom.xml b/spring-boot-samples/spring-boot-sample-websocket/pom.xml deleted file mode 100644 index 30822d7ae6f..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/pom.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - 4.0.0 - spring-boot-sample-websocket - war - - - org.springframework.boot - spring-boot-starter-parent - 0.5.0.M1 - ../../spring-boot-starters/spring-boot-starter-parent - - - - 1.7 - 8.0-SNAPSHOT - org.springframework.boot.samples.websocket.config.ApplicationConfiguration - - - - - - org.springframework.boot - spring-boot-starter-websocket - ${spring.boot.version} - - - - org.eclipse.jetty.websocket - websocket-client - 9.0.3.v20130506 - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - - tomcat-snapshots - https://repository.apache.org/content/repositories/snapshots - - true - - - false - - - - - diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/GreetingService.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/GreetingService.java deleted file mode 100644 index 1b6793f1f8b..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/GreetingService.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.client; - -public interface GreetingService { - - String getGreeting(); - -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/SimpleClientWebSocketHandler.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/SimpleClientWebSocketHandler.java deleted file mode 100644 index 4b8ca4a838d..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/SimpleClientWebSocketHandler.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.client; - -import java.util.concurrent.CountDownLatch; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketSession; -import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter; - -public class SimpleClientWebSocketHandler extends TextWebSocketHandlerAdapter { - - protected Log logger = LogFactory.getLog(SimpleClientWebSocketHandler.class); - - private final GreetingService greetingService; - - private CountDownLatch latch; - - @Autowired - public SimpleClientWebSocketHandler(GreetingService greetingService, CountDownLatch latch) { - this.greetingService = greetingService; - this.latch = latch; - } - - @Override - public void afterConnectionEstablished(WebSocketSession session) throws Exception { - TextMessage message = new TextMessage(this.greetingService.getGreeting()); - session.sendMessage(message); - } - - @Override - public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { - logger.info("Received: " + message + " (" + latch.getCount() + ")"); - session.close(); - latch.countDown(); - } - -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/SimpleGreetingService.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/SimpleGreetingService.java deleted file mode 100644 index 726dd879f45..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/client/SimpleGreetingService.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.client; - -public class SimpleGreetingService implements GreetingService { - - @Override - public String getGreeting() { - return "Hello world!"; - } - -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/config/SampleWebSocketsApplication.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/config/SampleWebSocketsApplication.java deleted file mode 100644 index f8f2aad3a56..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/config/SampleWebSocketsApplication.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.samples.websocket.config; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.catalina.Context; -import org.apache.catalina.startup.Tomcat; -import org.apache.tomcat.websocket.server.WsSci; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; -import org.springframework.boot.samples.websocket.client.GreetingService; -import org.springframework.boot.samples.websocket.client.SimpleGreetingService; -import org.springframework.boot.samples.websocket.echo.DefaultEchoService; -import org.springframework.boot.samples.websocket.echo.EchoService; -import org.springframework.boot.samples.websocket.echo.EchoWebSocketHandler; -import org.springframework.boot.samples.websocket.snake.SnakeWebSocketHandler; -import org.springframework.boot.web.SpringServletInitializer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; -import org.springframework.web.servlet.DispatcherServlet; -import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; -import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler; -import org.springframework.web.socket.sockjs.SockJsService; -import org.springframework.web.socket.sockjs.support.DefaultSockJsService; -import org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler; -import org.springframework.web.socket.support.PerConnectionWebSocketHandler; - -@Configuration -public class SampleWebSocketsApplication extends SpringServletInitializer { - - @Override - protected Class[] getConfigClasses() { - return new Class[] { SampleWebSocketsApplication.class }; - } - - public static void main(String[] args) { - SpringApplication.run(SampleWebSocketsApplication.class, args); - } - - @ConditionalOnClass(Tomcat.class) - @Configuration - @EnableAutoConfiguration - protected static class InitializationConfiguration { - @Bean - public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() { - TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory() { - @Override - protected void postProcessContext(Context context) { - context.addServletContainerInitializer(new WsSci(), null); - } - }; - return factory; - } - } - - @Bean - public EchoService echoService() { - return new DefaultEchoService("Did you say \"%s\"?"); - } - - @Bean - public GreetingService greetingService() { - return new SimpleGreetingService(); - } - - @Bean - public SimpleUrlHandlerMapping handlerMapping() { - - SockJsService sockJsService = new DefaultSockJsService(sockJsTaskScheduler()); - - Map urlMap = new HashMap(); - - urlMap.put("/echo", new WebSocketHttpRequestHandler(echoWebSocketHandler())); - urlMap.put("/snake", new WebSocketHttpRequestHandler(snakeWebSocketHandler())); - - urlMap.put("/sockjs/echo/**", new SockJsHttpRequestHandler(sockJsService, echoWebSocketHandler())); - urlMap.put("/sockjs/snake/**", new SockJsHttpRequestHandler(sockJsService, snakeWebSocketHandler())); - - SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); - handlerMapping.setOrder(-1); - handlerMapping.setUrlMap(urlMap); - - return handlerMapping; - } - - @Bean - public DispatcherServlet dispatcherServlet() { - DispatcherServlet servlet = new DispatcherServlet(); - servlet.setDispatchOptionsRequest(true); - return servlet; - } - - @Bean - public WebSocketHandler echoWebSocketHandler() { - return new PerConnectionWebSocketHandler(EchoWebSocketHandler.class); - } - - @Bean - public WebSocketHandler snakeWebSocketHandler() { - return new SnakeWebSocketHandler(); - } - - @Bean - public ThreadPoolTaskScheduler sockJsTaskScheduler() { - ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); - taskScheduler.setThreadNamePrefix("SockJS-"); - return taskScheduler; - } -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/DefaultEchoService.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/DefaultEchoService.java deleted file mode 100644 index bfc3b324746..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/DefaultEchoService.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.echo; - -public class DefaultEchoService implements EchoService { - - private final String echoFormat; - - public DefaultEchoService(String echoFormat) { - this.echoFormat = (echoFormat != null) ? echoFormat : "%s"; - } - - @Override - public String getMessage(String message) { - return String.format(this.echoFormat, message); - } - -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/EchoService.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/EchoService.java deleted file mode 100644 index 60751cccbb4..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/EchoService.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.echo; - -public interface EchoService { - - String getMessage(String message); - -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/EchoWebSocketHandler.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/EchoWebSocketHandler.java deleted file mode 100644 index fe76ee96862..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/echo/EchoWebSocketHandler.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.springframework.boot.samples.websocket.echo; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.socket.CloseStatus; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.WebSocketSession; -import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter; - -/** - * Echo messages by implementing a Spring {@link WebSocketHandler} abstraction. - */ -public class EchoWebSocketHandler extends TextWebSocketHandlerAdapter { - - private static Logger logger = LoggerFactory.getLogger(EchoWebSocketHandler.class); - - private final EchoService echoService; - - @Autowired - public EchoWebSocketHandler(EchoService echoService) { - this.echoService = echoService; - } - - @Override - public void afterConnectionEstablished(WebSocketSession session) { - logger.debug("Opened new session in instance " + this); - } - - @Override - public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { - String echoMessage = this.echoService.getMessage(message.getPayload()); - logger.debug(echoMessage); - session.sendMessage(new TextMessage(echoMessage)); - } - - @Override - public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { - session.close(CloseStatus.SERVER_ERROR); - } - -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Direction.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Direction.java deleted file mode 100644 index 60e18f7b034..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Direction.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.snake; - -public enum Direction { - NONE, NORTH, SOUTH, EAST, WEST -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Location.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Location.java deleted file mode 100644 index 493b9c69aef..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Location.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.snake; - -import org.springframework.boot.samples.websocket.snake.Direction; - - -public class Location { - - public int x; - public int y; - public static final int GRID_SIZE = 10; - public static final int PLAYFIELD_HEIGHT = 480; - public static final int PLAYFIELD_WIDTH = 640; - - public Location(int x, int y) { - this.x = x; - this.y = y; - } - - public Location getAdjacentLocation(Direction direction) { - switch (direction) { - case NORTH: - return new Location(x, y - Location.GRID_SIZE); - case SOUTH: - return new Location(x, y + Location.GRID_SIZE); - case EAST: - return new Location(x + Location.GRID_SIZE, y); - case WEST: - return new Location(x - Location.GRID_SIZE, y); - case NONE: - // fall through - default: - return this; - } - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Location location = (Location) o; - - if (x != location.x) return false; - if (y != location.y) return false; - - return true; - } - - @Override - public int hashCode() { - int result = x; - result = 31 * result + y; - return result; - } -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Snake.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Snake.java deleted file mode 100644 index 0ccf717281e..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/Snake.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.snake; - -import java.util.ArrayDeque; -import java.util.Collection; -import java.util.Deque; - -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketSession; - - -public class Snake { - - private static final int DEFAULT_LENGTH = 5; - - private final int id; - private final WebSocketSession session; - - private Direction direction; - private int length = DEFAULT_LENGTH; - private Location head; - private final Deque tail = new ArrayDeque(); - private final String hexColor; - - public Snake(int id, WebSocketSession session) { - this.id = id; - this.session = session; - this.hexColor = SnakeUtils.getRandomHexColor(); - resetState(); - } - - private void resetState() { - this.direction = Direction.NONE; - this.head = SnakeUtils.getRandomLocation(); - this.tail.clear(); - this.length = DEFAULT_LENGTH; - } - - private synchronized void kill() throws Exception { - resetState(); - sendMessage("{'type': 'dead'}"); - } - - private synchronized void reward() throws Exception { - length++; - sendMessage("{'type': 'kill'}"); - } - - - protected void sendMessage(String msg) throws Exception { - session.sendMessage(new TextMessage(msg)); - } - - public synchronized void update(Collection snakes) throws Exception { - Location nextLocation = head.getAdjacentLocation(direction); - if (nextLocation.x >= SnakeUtils.PLAYFIELD_WIDTH) { - nextLocation.x = 0; - } - if (nextLocation.y >= SnakeUtils.PLAYFIELD_HEIGHT) { - nextLocation.y = 0; - } - if (nextLocation.x < 0) { - nextLocation.x = SnakeUtils.PLAYFIELD_WIDTH; - } - if (nextLocation.y < 0) { - nextLocation.y = SnakeUtils.PLAYFIELD_HEIGHT; - } - if (direction != Direction.NONE) { - tail.addFirst(head); - if (tail.size() > length) { - tail.removeLast(); - } - head = nextLocation; - } - - handleCollisions(snakes); - } - - private void handleCollisions(Collection snakes) throws Exception { - for (Snake snake : snakes) { - boolean headCollision = id != snake.id && snake.getHead().equals(head); - boolean tailCollision = snake.getTail().contains(head); - if (headCollision || tailCollision) { - kill(); - if (id != snake.id) { - snake.reward(); - } - } - } - } - - public synchronized Location getHead() { - return head; - } - - public synchronized Collection getTail() { - return tail; - } - - public synchronized void setDirection(Direction direction) { - this.direction = direction; - } - - public synchronized String getLocationsJson() { - StringBuilder sb = new StringBuilder(); - sb.append(String.format("{x: %d, y: %d}", - Integer.valueOf(head.x), Integer.valueOf(head.y))); - for (Location location : tail) { - sb.append(','); - sb.append(String.format("{x: %d, y: %d}", - Integer.valueOf(location.x), Integer.valueOf(location.y))); - } - return String.format("{'id':%d,'body':[%s]}", - Integer.valueOf(id), sb.toString()); - } - - public int getId() { - return id; - } - - public String getHexColor() { - return hexColor; - } -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeTimer.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeTimer.java deleted file mode 100644 index e0c70a6b07f..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeTimer.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.snake; - -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.juli.logging.Log; -import org.apache.juli.logging.LogFactory; - -/** - * Sets up the timer for the multi-player snake game WebSocket example. - */ -public class SnakeTimer { - - private static final Log log = - LogFactory.getLog(SnakeTimer.class); - - private static Timer gameTimer = null; - - private static final long TICK_DELAY = 100; - - private static final ConcurrentHashMap snakes = - new ConcurrentHashMap(); - - public static synchronized void addSnake(Snake snake) { - if (snakes.size() == 0) { - startTimer(); - } - snakes.put(Integer.valueOf(snake.getId()), snake); - } - - - public static Collection getSnakes() { - return Collections.unmodifiableCollection(snakes.values()); - } - - - public static synchronized void removeSnake(Snake snake) { - snakes.remove(Integer.valueOf(snake.getId())); - if (snakes.size() == 0) { - stopTimer(); - } - } - - - public static void tick() throws Exception { - StringBuilder sb = new StringBuilder(); - for (Iterator iterator = SnakeTimer.getSnakes().iterator(); - iterator.hasNext();) { - Snake snake = iterator.next(); - snake.update(SnakeTimer.getSnakes()); - sb.append(snake.getLocationsJson()); - if (iterator.hasNext()) { - sb.append(','); - } - } - broadcast(String.format("{'type': 'update', 'data' : [%s]}", - sb.toString())); - } - - public static void broadcast(String message) throws Exception { - for (Snake snake : SnakeTimer.getSnakes()) { - snake.sendMessage(message); - } - } - - - public static void startTimer() { - gameTimer = new Timer(SnakeTimer.class.getSimpleName() + " Timer"); - gameTimer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - try { - tick(); - } catch (Throwable e) { - log.error("Caught to prevent timer from shutting down", e); - } - } - }, TICK_DELAY, TICK_DELAY); - } - - - public static void stopTimer() { - if (gameTimer != null) { - gameTimer.cancel(); - } - } -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeUtils.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeUtils.java deleted file mode 100644 index 81527238fd8..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeUtils.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.snake; - -import java.awt.Color; -import java.util.Random; - -public class SnakeUtils { - - public static final int PLAYFIELD_WIDTH = 640; - public static final int PLAYFIELD_HEIGHT = 480; - public static final int GRID_SIZE = 10; - - private static final Random random = new Random(); - - - public static String getRandomHexColor() { - float hue = random.nextFloat(); - // sat between 0.1 and 0.3 - float saturation = (random.nextInt(2000) + 1000) / 10000f; - float luminance = 0.9f; - Color color = Color.getHSBColor(hue, saturation, luminance); - return '#' + Integer.toHexString( - (color.getRGB() & 0xffffff) | 0x1000000).substring(1); - } - - - public static Location getRandomLocation() { - int x = roundByGridSize(random.nextInt(PLAYFIELD_WIDTH)); - int y = roundByGridSize(random.nextInt(PLAYFIELD_HEIGHT)); - return new Location(x, y); - } - - - private static int roundByGridSize(int value) { - value = value + (GRID_SIZE / 2); - value = value / GRID_SIZE; - value = value * GRID_SIZE; - return value; - } - -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeWebSocketHandler.java b/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeWebSocketHandler.java deleted file mode 100644 index f2eba5c3496..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/java/org/springframework/boot/samples/websocket/snake/SnakeWebSocketHandler.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.snake; - -import java.awt.Color; -import java.util.Iterator; -import java.util.Random; -import java.util.concurrent.atomic.AtomicInteger; - -import org.springframework.web.socket.CloseStatus; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketSession; -import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter; - -public class SnakeWebSocketHandler extends TextWebSocketHandlerAdapter { - - public static final int PLAYFIELD_WIDTH = 640; - public static final int PLAYFIELD_HEIGHT = 480; - public static final int GRID_SIZE = 10; - - private static final AtomicInteger snakeIds = new AtomicInteger(0); - private static final Random random = new Random(); - - - private final int id; - private Snake snake; - - public static String getRandomHexColor() { - float hue = random.nextFloat(); - // sat between 0.1 and 0.3 - float saturation = (random.nextInt(2000) + 1000) / 10000f; - float luminance = 0.9f; - Color color = Color.getHSBColor(hue, saturation, luminance); - return '#' + Integer.toHexString( - (color.getRGB() & 0xffffff) | 0x1000000).substring(1); - } - - - public static Location getRandomLocation() { - int x = roundByGridSize(random.nextInt(PLAYFIELD_WIDTH)); - int y = roundByGridSize(random.nextInt(PLAYFIELD_HEIGHT)); - return new Location(x, y); - } - - - private static int roundByGridSize(int value) { - value = value + (GRID_SIZE / 2); - value = value / GRID_SIZE; - value = value * GRID_SIZE; - return value; - } - - public SnakeWebSocketHandler() { - this.id = snakeIds.getAndIncrement(); - } - - - @Override - public void afterConnectionEstablished(WebSocketSession session) throws Exception { - this.snake = new Snake(id, session); - SnakeTimer.addSnake(snake); - StringBuilder sb = new StringBuilder(); - for (Iterator iterator = SnakeTimer.getSnakes().iterator(); - iterator.hasNext();) { - Snake snake = iterator.next(); - sb.append(String.format("{id: %d, color: '%s'}", - Integer.valueOf(snake.getId()), snake.getHexColor())); - if (iterator.hasNext()) { - sb.append(','); - } - } - SnakeTimer.broadcast(String.format("{'type': 'join','data':[%s]}", - sb.toString())); - } - - - @Override - protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { - String payload = message.getPayload(); - if ("west".equals(payload)) { - snake.setDirection(Direction.WEST); - } else if ("north".equals(payload)) { - snake.setDirection(Direction.NORTH); - } else if ("east".equals(payload)) { - snake.setDirection(Direction.EAST); - } else if ("south".equals(payload)) { - snake.setDirection(Direction.SOUTH); - } - } - - - @Override - public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { - SnakeTimer.removeSnake(snake); - SnakeTimer.broadcast(String.format("{'type': 'leave', 'id': %d}", - Integer.valueOf(id))); - } -} diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/echo.html b/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/echo.html deleted file mode 100644 index cbaef719dd3..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/echo.html +++ /dev/null @@ -1,140 +0,0 @@ - - - - - Apache Tomcat WebSocket Examples: Echo - - - - - -
-
-
- -
-
- - -
-
- -
-
- -
-
-
-
-
-
- - \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/index.html b/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/index.html deleted file mode 100644 index 39069b15d76..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/index.html +++ /dev/null @@ -1,31 +0,0 @@ - - - - - Apache Tomcat WebSocket Examples: Index - - - -

Please select the sample you would like to try.

- - - \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/snake.html b/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/snake.html deleted file mode 100644 index b00debf5709..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/main/resources/static/snake.html +++ /dev/null @@ -1,259 +0,0 @@ - - - - - - Apache Tomcat WebSocket Examples: Multiplayer Snake - - - - - -
- -
-
-
-
- - - diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/test/java/org/springframework/boot/samples/websocket/echo/StandardClientApp.java b/spring-boot-samples/spring-boot-sample-websocket/src/test/java/org/springframework/boot/samples/websocket/echo/StandardClientApp.java deleted file mode 100644 index 76c5dfd5130..00000000000 --- a/spring-boot-samples/spring-boot-sample-websocket/src/test/java/org/springframework/boot/samples/websocket/echo/StandardClientApp.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.boot.samples.websocket.echo; - -import static org.junit.Assert.assertEquals; - -import java.util.concurrent.CountDownLatch; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.junit.Test; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.samples.websocket.client.GreetingService; -import org.springframework.boot.samples.websocket.client.SimpleClientWebSocketHandler; -import org.springframework.boot.samples.websocket.client.SimpleGreetingService; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.socket.client.WebSocketConnectionManager; -import org.springframework.web.socket.client.endpoint.StandardWebSocketClient; - -public class StandardClientApp { - - private static Log logger = LogFactory.getLog(StandardClientApp.class); - - private static final String WS_URI = "ws://localhost:8080/echo"; - - @Test - public void runAndWait() throws Exception { - ApplicationContext context = SpringApplication.run(ClientConfiguration.class, "--spring.main.web_environment=false"); - assertEquals(0, context.getBean(ClientConfiguration.class).latch.getCount()); - } - - @Configuration - static class ClientConfiguration implements CommandLineRunner { - - private CountDownLatch latch = new CountDownLatch(1); - - @Override - public void run(String... args) throws Exception { - logger.info("Waiting for response: latch=" + latch.getCount()); - latch.await(); - logger.info("Got response: latch=" + latch.getCount()); - } - - @Bean - public WebSocketConnectionManager wsConnectionManager() { - - WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), handler(), WS_URI); - manager.setAutoStartup(true); - - return manager; - } - - @Bean - public StandardWebSocketClient client() { - return new StandardWebSocketClient(); - } - - @Bean - public SimpleClientWebSocketHandler handler() { - return new SimpleClientWebSocketHandler(greetingService(), latch); - } - - @Bean - public GreetingService greetingService() { - return new SimpleGreetingService(); - } - } - -} diff --git a/spring-boot-starters/pom.xml b/spring-boot-starters/pom.xml index 14ecebc52fb..236fefc71a9 100644 --- a/spring-boot-starters/pom.xml +++ b/spring-boot-starters/pom.xml @@ -25,7 +25,6 @@ spring-boot-starter-security spring-boot-starter-tomcat spring-boot-starter-web - spring-boot-starter-websocket diff --git a/spring-boot-starters/spring-boot-starter-websocket/pom.xml b/spring-boot-starters/spring-boot-starter-websocket/pom.xml deleted file mode 100644 index f3b6116e77e..00000000000 --- a/spring-boot-starters/spring-boot-starter-websocket/pom.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - 4.0.0 - - org.springframework.boot - spring-boot-starters - 0.5.0.M1 - - spring-boot-starter-websocket - jar - - ${basedir}/../.. - 8.0-SNAPSHOT - - - - - org.apache.tomcat.embed - tomcat-embed-core - ${tomcat.version} - - - org.apache.tomcat.embed - tomcat-embed-logging-juli - ${tomcat.version} - - - - - - ${project.groupId} - spring-boot-up-web - ${project.version} - - - ${project.groupId} - spring-boot-up-tomcat - - - - - org.springframework - spring-websocket - ${spring.version} - - - org.apache.tomcat.embed - tomcat-embed-core - - - org.apache.tomcat.embed - tomcat-embed-logging-juli - - - - - tomcat-snapshots - https://repository.apache.org/content/repositories/snapshots - - true - - - false - - - -