public static void tick() throws Exception { StringBuilder sb = new StringBuilder(); for (Iterator<Snake> 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 { Collection<Snake> snakes = new CopyOnWriteArrayList<>(SnakeTimer.getSnakes()); for (Snake snake : snakes) { try { snake.sendMessage(message); } catch (Throwable ex) { // if Snake#sendMessage fails the client is removed removeSnake(snake); } } }