protected void onMessage(AtmosphereResource resource, String message) {
   Snake snake = snake(resource);
   if (snake != null) {
     if ("west".equals(message)) {
       snake.setDirection(Direction.WEST);
     } else if ("north".equals(message)) {
       snake.setDirection(Direction.NORTH);
     } else if ("east".equals(message)) {
       snake.setDirection(Direction.EAST);
     } else if ("south".equals(message)) {
       snake.setDirection(Direction.SOUTH);
     }
   }
 }
  public void onOpen(AtmosphereResource resource) throws IOException {
    int id = snakeIds.getAndIncrement();
    resource.session().setAttribute("id", id);
    Snake snake = new Snake(id, resource);

    resource.session().setAttribute("snake", snake);
    snakeBroadcaster().addSnake(snake);
    StringBuilder sb = new StringBuilder();
    for (Iterator<Snake> iterator = snakeBroadcaster().getSnakes().iterator();
        iterator.hasNext(); ) {
      snake = iterator.next();
      sb.append(
          String.format(
              "{id: %d, color: '%s'}", Integer.valueOf(snake.getId()), snake.getHexColor()));
      if (iterator.hasNext()) {
        sb.append(',');
      }
    }
    snakeBroadcaster().broadcast(String.format("{'type': 'join','data':[%s]}", sb.toString()));
  }
  private void displaySnake(Graphics g) {

    LinkedList<Point> coordinates = snake.segmentsToDraw();

    // Draw head in grey
    g.setColor(Color.BLACK); // Changed snake head to be drawn in black to enhance minimalist look.
    // As the snake is always moving forward, it should be relatively
    // intuitive to see where the head is.
    Point head = coordinates.pop();
    g.fillRect((int) head.getX(), (int) head.getY(), SnakeGame.squareSize, SnakeGame.squareSize);

    g.setColor(Color.BLACK);
    for (Point p : coordinates) {
      g.fillRect((int) p.getX(), (int) p.getY(), SnakeGame.squareSize, SnakeGame.squareSize);
    }
  }
        @Override
        public void keyPressed(KeyEvent e) {

          System.out.println("KEY: " + e.getKeyCode());

          if (e.getKeyCode() != snake1.getCurDirection().getKey()
              && e.getKeyCode() != snake1.getCurDirection().getOpposite()) {

            if (e.getKeyCode() == KeyEvent.VK_UP) {

              snake1.setCurDirection(Direction.UP);

            } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {

              snake1.setCurDirection(Direction.DOWN);

            } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {

              snake1.setCurDirection(Direction.LEFT);

            } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

              snake1.setCurDirection(Direction.RIGHT);
            }
          }

          if (e.getKeyCode() != snake2.getCurDirection().getP2key()
              && e.getKeyCode() != snake2.getCurDirection().getP2Opposite()) {

            if (e.getKeyCode() == KeyEvent.VK_W) {

              snake2.setCurDirection(Direction.UP);

            } else if (e.getKeyCode() == KeyEvent.VK_S) {

              snake2.setCurDirection(Direction.DOWN);

            } else if (e.getKeyCode() == KeyEvent.VK_A) {

              snake2.setCurDirection(Direction.LEFT);

            } else if (e.getKeyCode() == KeyEvent.VK_D) {

              snake2.setCurDirection(Direction.RIGHT);
            }
          }
        }