Merge branch '2.0.x' into 2.1.x
Closes gh-17078
This commit is contained in:
@@ -40,8 +40,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableWebSocket
|
||||
public class SampleTomcatWebSocketApplication extends SpringBootServletInitializer
|
||||
implements WebSocketConfigurer {
|
||||
public class SampleTomcatWebSocketApplication extends SpringBootServletInitializer implements WebSocketConfigurer {
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
|
||||
@@ -36,8 +36,8 @@ public class SimpleClientWebSocketHandler extends TextWebSocketHandler {
|
||||
|
||||
private final AtomicReference<String> messagePayload;
|
||||
|
||||
public SimpleClientWebSocketHandler(GreetingService greetingService,
|
||||
CountDownLatch latch, AtomicReference<String> message) {
|
||||
public SimpleClientWebSocketHandler(GreetingService greetingService, CountDownLatch latch,
|
||||
AtomicReference<String> message) {
|
||||
this.greetingService = greetingService;
|
||||
this.latch = latch;
|
||||
this.messagePayload = message;
|
||||
@@ -50,8 +50,7 @@ public class SimpleClientWebSocketHandler extends TextWebSocketHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleTextMessage(WebSocketSession session, TextMessage message)
|
||||
throws Exception {
|
||||
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
this.logger.info("Received: " + message + " (" + this.latch.getCount() + ")");
|
||||
session.close();
|
||||
this.messagePayload.set(message.getPayload());
|
||||
|
||||
@@ -44,16 +44,14 @@ public class EchoWebSocketHandler extends TextWebSocketHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleTextMessage(WebSocketSession session, TextMessage message)
|
||||
throws Exception {
|
||||
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 {
|
||||
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
||||
session.close(CloseStatus.SERVER_ERROR);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,8 +27,7 @@ public class ReverseWebSocketEndpoint {
|
||||
|
||||
@OnMessage
|
||||
public void handleMessage(Session session, String message) throws IOException {
|
||||
session.getBasicRemote()
|
||||
.sendText("Reversed: " + new StringBuilder(message).reverse());
|
||||
session.getBasicRemote().sendText("Reversed: " + new StringBuilder(message).reverse());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -104,8 +104,7 @@ public class Snake {
|
||||
|
||||
private void handleCollisions(Collection<Snake> snakes) throws Exception {
|
||||
for (Snake snake : snakes) {
|
||||
boolean headCollision = this.id != snake.id
|
||||
&& snake.getHead().equals(this.head);
|
||||
boolean headCollision = this.id != snake.id && snake.getHead().equals(this.head);
|
||||
boolean tailCollision = snake.getTail().contains(this.head);
|
||||
if (headCollision || tailCollision) {
|
||||
kill();
|
||||
@@ -137,15 +136,12 @@ public class Snake {
|
||||
public String getLocationsJson() {
|
||||
synchronized (this.monitor) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(String.format("{x: %d, y: %d}", Integer.valueOf(this.head.x),
|
||||
Integer.valueOf(this.head.y)));
|
||||
sb.append(String.format("{x: %d, y: %d}", Integer.valueOf(this.head.x), Integer.valueOf(this.head.y)));
|
||||
for (Location location : this.tail) {
|
||||
sb.append(',');
|
||||
sb.append(String.format("{x: %d, y: %d}", Integer.valueOf(location.x),
|
||||
Integer.valueOf(location.y)));
|
||||
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(this.id),
|
||||
sb.toString());
|
||||
return String.format("{'id':%d,'body':[%s]}", Integer.valueOf(this.id), sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,8 +69,7 @@ public final class SnakeTimer {
|
||||
|
||||
public static void tick() throws Exception {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Iterator<Snake> iterator = SnakeTimer.getSnakes().iterator(); iterator
|
||||
.hasNext();) {
|
||||
for (Iterator<Snake> iterator = SnakeTimer.getSnakes().iterator(); iterator.hasNext();) {
|
||||
Snake snake = iterator.next();
|
||||
snake.update(SnakeTimer.getSnakes());
|
||||
sb.append(snake.getLocationsJson());
|
||||
|
||||
@@ -47,8 +47,7 @@ public final class SnakeUtils {
|
||||
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);
|
||||
return '#' + Integer.toHexString((color.getRGB() & 0xffffff) | 0x1000000).substring(1);
|
||||
}
|
||||
|
||||
public static Location getRandomLocation() {
|
||||
|
||||
@@ -42,8 +42,7 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
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);
|
||||
return '#' + Integer.toHexString((color.getRGB() & 0xffffff) | 0x1000000).substring(1);
|
||||
}
|
||||
|
||||
public static Location getRandomLocation() {
|
||||
@@ -68,22 +67,18 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
this.snake = new Snake(this.id, session);
|
||||
SnakeTimer.addSnake(this.snake);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Iterator<Snake> iterator = SnakeTimer.getSnakes().iterator(); iterator
|
||||
.hasNext();) {
|
||||
for (Iterator<Snake> iterator = SnakeTimer.getSnakes().iterator(); iterator.hasNext();) {
|
||||
Snake snake = iterator.next();
|
||||
sb.append(String.format("{id: %d, color: '%s'}",
|
||||
Integer.valueOf(snake.getId()), snake.getHexColor()));
|
||||
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()));
|
||||
SnakeTimer.broadcast(String.format("{'type': 'join','data':[%s]}", sb.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message)
|
||||
throws Exception {
|
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
String payload = message.getPayload();
|
||||
if ("west".equals(payload)) {
|
||||
this.snake.setDirection(Direction.WEST);
|
||||
@@ -100,11 +95,9 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status)
|
||||
throws Exception {
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
|
||||
SnakeTimer.removeSnake(this.snake);
|
||||
SnakeTimer.broadcast(
|
||||
String.format("{'type': 'leave', 'id': %d}", Integer.valueOf(this.id)));
|
||||
SnakeTimer.broadcast(String.format("{'type': 'leave', 'id': %d}", Integer.valueOf(this.id)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user