log out button

This commit is contained in:
Andy Janata 2012-01-06 15:53:04 -08:00
parent ce273d533c
commit 9508575949
9 changed files with 82 additions and 2 deletions

View File

@ -49,6 +49,12 @@
padding: 0px;
}
#logout {
position: absolute;
top: 0px;
right: 0px;
}
#chat_submit {
width: 50px;
height: 19px;

View File

@ -31,6 +31,7 @@
</div>
<div id="canvass">
<input type="button" id="logout" value="Log out" />
<div id="chat_area">
<div id="log"></div>
<input type="text" id="chat" maxlength="200" />

View File

@ -46,3 +46,7 @@ cah.ajax.after_registered = function() {
cah.ajax.SuccessHandlers.chat = function(data) {
// pass
};
cah.ajax.SuccessHandlers.logout = function(data) {
window.location.reload();
};

View File

@ -15,6 +15,11 @@ $(document).ready(function() {
$("#chat").keyup(chat_keyup);
$("#chat_submit").click(chatsubmit_click);
// TODO: have some sort of mechanism to alert the server that we have unloaded the page, but
// have not expressed an interest in being cleared out yet.
// $(window).bind("beforeunload", window_beforeunload);
$("#logout").click(logout_click);
});
function nickbox_keyup(e) {
@ -48,3 +53,7 @@ function chatsubmit_click(e) {
$("#chat").val("");
$("#chat").focus();
}
function logout_click(e) {
cah.Ajax.request("logout", {});
}

View File

@ -15,6 +15,23 @@ cah.longpoll.EventHandlers.new_player = function(data) {
cah.log.status(data.nickname + " has connected.");
};
cah.longpoll.EventHandlers.player_leave = function(data) {
var friendly_reason = "Leaving";
// see net.socialgamer.cah.data.User.DisconnectReason
switch (data.reason) {
case "MANUAL":
friendly_reason = "Leaving";
break;
case "PING_TIMEOUT":
friendly_reason = "Ping timeout";
break;
case "KICKED":
friendly_reason = "Kicked by server";
break;
}
cah.log.status(data.nickname + " has disconnected (" + friendly_reason + ").");
};
cah.longpoll.EventHandlers.noop = function(data) {
// pass
};

View File

@ -38,7 +38,7 @@ public class QueuedMessage implements Comparable<QueuedMessage> {
*
*/
public enum Type {
PING(0), NEW_PLAYER(5), CHAT(5);
PING(0), NEW_PLAYER(3), PLAYER_DISCONNECT(3), CHAT(5);
private final int weight;

View File

@ -5,7 +5,7 @@ import java.util.concurrent.PriorityBlockingQueue;
public class User {
enum DisconnectReason {
public enum DisconnectReason {
MANUAL, PING_TIMEOUT, KICKED
}
@ -72,6 +72,9 @@ public class User {
lastHeardFrom = System.nanoTime();
}
/**
* @return The time the user was last heard from, in nanoseconds.
*/
public long getLastHeardFrom() {
return lastHeardFrom;
}

View File

@ -12,6 +12,7 @@ public class Handlers {
LIST = new HashMap<String, Class<? extends Handler>>();
LIST.put(ChatHandler.OP, ChatHandler.class);
LIST.put(FirstLoadHandler.OP, FirstLoadHandler.class);
LIST.put(LogoutHandler.OP, LogoutHandler.class);
LIST.put(RegisterHandler.OP, RegisterHandler.class);
LIST.put(TestHandler.OP, TestHandler.class);
}

View File

@ -0,0 +1,39 @@
package net.socialgamer.cah.handlers;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Server;
import net.socialgamer.cah.data.ConnectedUsers;
import net.socialgamer.cah.data.User;
import net.socialgamer.cah.data.User.DisconnectReason;
import com.google.inject.Inject;
public class LogoutHandler extends Handler {
public final static String OP = "logout";
private final ConnectedUsers users;
@Inject
public LogoutHandler(final Server server) {
this.users = server.getConnectedUsers();
}
@Override
public Map<String, Object> handle(final Map<String, String[]> parameters,
final HttpSession session) {
final Map<String, Object> data = new HashMap<String, Object>();
final User user = (User) session.getAttribute("user");
assert (user != null);
users.removeUser(user, DisconnectReason.MANUAL);
session.invalidate();
return data;
}
}