diff --git a/war/WEB-INF/app/channel.js b/war/WEB-INF/app/channel.js index 2be3326..1717cc3 100644 --- a/war/WEB-INF/app/channel.js +++ b/war/WEB-INF/app/channel.js @@ -27,12 +27,11 @@ inherits(Channel, EventEmitter); extend(Channel.prototype, { fetchFromMemcache : function() { var channelCache = memcache.get("ringo-chat-history"); - - if ( !channelCache ) { - memcache.set("ringo-chat-history", this.serialize()); - } else { + if ( channelCache ) { this.deserialize(channelCache); + this.expireOldSessions(); } + memcache.set("ringo-chat-history", this.serialize()); return this; }, @@ -48,8 +47,8 @@ extend(Channel.prototype, { return JSON.stringify({ nextMessageId : this.nextMessageId, messages : this.messages, - callbacks : this.callbacks, - sessions : this.sessions + sessions : this.sessions, + callbacks : this.callbacks }); }, @@ -90,7 +89,7 @@ extend(Channel.prototype, { for ( var key in sessions ) { if ( sessions[key] ) { // sys.print("Send Message : "+JSON.stringify(message)); - channelService.sendMessage(encodeURIComponent(sessions[key].nick), "{messages : ["+ JSON.stringify(message) + "]}"); + channelService.sendMessage(sessions[key].nick, "{messages : ["+ JSON.stringify(message) + "]}"); } } return id; @@ -124,7 +123,6 @@ extend(Channel.prototype, { }, createSession: function(nick) { - this.expireOldSessions(); var session = new Session(nick); if (!session) { return; @@ -137,7 +135,7 @@ extend(Channel.prototype, { } } this.sessions[session.id] = session; - session.token = channelService.createChannel(encodeURIComponent(nick)); + session.token = channelService.createChannel(nick); session.since = this.appendMessage(nick, "join"); return session; }, @@ -147,7 +145,7 @@ extend(Channel.prototype, { return false; } var eventId = this.appendMessage(this.sessions[id].nick, "part"); - delete this.sessions[id]; + delete this.sessions[id]; return eventId; }, diff --git a/war/WEB-INF/app/public/js/client.js b/war/WEB-INF/app/public/js/client.js index c222740..0f56f0d 100644 --- a/war/WEB-INF/app/public/js/client.js +++ b/war/WEB-INF/app/public/js/client.js @@ -6,8 +6,13 @@ var title = document.title, log, message; +//flush sessions! +setInterval(function() {channel.request("/flush", {type : "POST"});}, 2000); + + // TODO: handle connectionerror + $(function() { log = $("#chat-log"); message = $("#message"); diff --git a/war/WEB-INF/app/public/js/ringochat.js b/war/WEB-INF/app/public/js/ringochat.js index 6e84abd..146970e 100644 --- a/war/WEB-INF/app/public/js/ringochat.js +++ b/war/WEB-INF/app/public/js/ringochat.js @@ -1,5 +1,4 @@ (function($) { - var ringoChat = (window.ringoChat = { connect: function(basePath) { return new Channel(basePath); @@ -58,11 +57,11 @@ $.extend(Channel.prototype, { $.each(data.messages, function(i, message) { channel.lastMessageId = Math.max(channel.lastMessageId, message.id); $(channel).triggerHandler(message.type, message); - if ( message.type == "part" && message.nick == channel.nick ) { - window.location = '/'; - } }); } + if ( this.nick == null ) { + window.location = '/'; + } // this.poll(); }, @@ -100,7 +99,6 @@ $.extend(Channel.prototype, { channel.socket.onopen = function () { message.val("").removeAttr("disabled").focus(); - setInterval(function() {channel.request("/flush", {type : "POST"});}, 2000); }; channel.socket.onmessage = function(evt) { var data = eval('(' + evt.data + ')') @@ -113,11 +111,15 @@ $.extend(Channel.prototype, { }; channel.socket.onerror = function() { channel.nick = null; + alert("Connection Error!"); message.val("Connection Error!").attr("disabled", true); + window.location = '/'; }; channel.socket.onclose = function() { channel.nick = null; + alert("Session Timeout!"); message.val("Session Timeout!").attr("disabled", true); + window.location = '/'; }; (options.success || $.noop)(); @@ -129,7 +131,11 @@ $.extend(Channel.prototype, { part: function() { if (!this.id) { return; } this.request("/part", { - data: { id: this.id } + data: { id: this.id }, + success: function(data) { + message.val("Closed Session").attr("disabled", true); + window.location = '/'; + } }); this.nick = null; },