// Notify logged users about the leaving player
public WebSocket<JsonNode> socket() {
        final Http.Session session = session();
        final User currentUser = Application.getLocalUser(session());
        return new WebSocket<JsonNode>() {
            @Override
            public void onReady(In<JsonNode> in, Out<JsonNode> out) {
                try {
                    User u = Application.getLocalUser(session);
                    //Logger.debug("User " + u.id + " connected");
                    // Add the new user to the data structures
                    boolean alreadyOnline = onlineUsers.containsValue(u);
                    onlineUsers.put(out, u);
                    /*try (Jedis j = jedisPool.getResource()) {
                        j.sadd("online_users", Long.toString(u.id));
                    }*/
                    // Notify logged users about the new player
                    if (!alreadyOnline) {
                        ObjectMapper mapper = new ObjectMapper();
                        ObjectNode notification = JsonNodeFactory.instance.objectNode();
                        notification.put("action", "newuser");
                        notification.put("newuser", mapper.writeValueAsString(currentUser));
                        broadcastMessage(notification, new HashSet<User>() {{ add(u); }});
                    }
                } catch (RuntimeException e) {
                    //Logger.debug("Unknown user connected");
                } catch (JsonProcessingException e) {
                    Logger.error(e.getMessage(), e);
                }
                in.onMessage((data) -> {
                    Logger.debug(currentUser.id + " - " + data.toString());
                    String action = data.findPath("action").textValue();
                    switch (action) {
                        // TODO: add error handling
                        case "newgame":
                            onNewGameResponse(data, currentUser);
                            break;
                        case "ready":
                            onUserReady(data, currentUser);
                            break;
                        case "setrowboat":
                            onSetShip(data, currentUser, new Rowboat());
                            break;
                        case "setdestructor":
                            onSetShip(data, currentUser, new Destructor());
                            break;
                        case "setflattop":
                            onSetShip(data, currentUser, new Flattop());
                            break;
                        case "shoot":
                            onShoot(data, currentUser);
                            break;
                        case "userleaves":
                            onUserLeaves(data, currentUser);
                            break;
                        case "getOnlineUsers":
                            onGetOnlineUsers(data, currentUser);
                            break;
                        default:
                    }
                });
                in.onClose(() -> {
                    User u = onlineUsers.get(out);
                    if (u != null) {
                        //Logger.debug("User " + u.id + " disconnected");
                        // Remove user from the data structures
                        onlineUsers.remove(out);
                        // Wait for 100 ms to see if the user connects through another websocket
                        //Thread.sleep(100);
                        boolean stillOnline = onlineUsers.containsValue(u);
                        /*try (Jedis j = jedisPool.getResource()) {
                            j.srem("online_users", Long.toString(u.id));
                        }*/
                        // Notify logged users about the leaving player
                        if (!stillOnline) {
                            try {
                                ObjectMapper mapper = new ObjectMapper();
                                ObjectNode notification = JsonNodeFactory.instance.objectNode();
                                notification.put("action", "userleaves");
                                notification.put("leavinguser", mapper.writeValueAsString(currentUser));
                                broadcastMessage(notification, new HashSet<User>() {{ add(u); }});
                            } catch (JsonProcessingException e) {
                                Logger.error(e.getMessage(), e);
                            }
                        }
                    } else {
                        //Logger.debug("Unknown user disconnected");
                    }
                });
            }
        };
    }
j.srem("online_users", Long.toString(u.id));
}*/
// Notify logged users about the leaving player
if (!stillOnline) {
try {
if (u != null) {
//Logger.debug("User " + u.id + " disconnected");
// Remove user from the data structures
onlineUsers.remove(out);
// Wait for 100 ms to see if the user connects through another websocket
//Thread.sleep(100);
boolean stillOnline = onlineUsers.containsValue(u);
/*try (Jedis j = jedisPool.getResource()) {
j.srem("online_users", Long.toString(u.id));
}*/
// Notify logged users about the leaving player
if (!stillOnline) {
try {
ObjectMapper mapper = new ObjectMapper();
ObjectNode notification = JsonNodeFactory.instance.objectNode();
notification.put("action", "userleaves");
notification.put("leavinguser", mapper.writeValueAsString(currentUser));
broadcastMessage(notification, new HashSet<User>() {{ add(u); }});
} catch (JsonProcessingException e) {
Logger.error(e.getMessage(), e);
}
onUserLeaves(data, currentUser);
break;
case "getOnlineUsers":
onGetOnlineUsers(data, currentUser);
break;
default:
}
});
in.onClose(() -> {
User u = onlineUsers.get(out);
if (u != null) {
//Logger.debug("User " + u.id + " disconnected");
// Remove user from the data structures
onlineUsers.remove(out);
// Wait for 100 ms to see if the user connects through another websocket
//Thread.sleep(100);
boolean stillOnline = onlineUsers.containsValue(u);
/*try (Jedis j = jedisPool.getResource()) {
j.srem("online_users", Long.toString(u.id));
}*/
// Notify logged users about the leaving player
if (!stillOnline) {
try {
ObjectMapper mapper = new ObjectMapper();
ObjectNode notification = JsonNodeFactory.instance.objectNode();
notification.put("action", "userleaves");
notification.put("leavinguser", mapper.writeValueAsString(currentUser));
broadcastMessage(notification, new HashSet<User>() {{ add(u); }});
} catch (JsonProcessingException e) {
Logger.error(e.getMessage(), e);
}
}
} else {
//Logger.debug("Unknown user disconnected");
}
});
}
};
}