Skip to content
This repository has been archived by the owner on May 30, 2018. It is now read-only.

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kyungw00k committed Dec 29, 2010
1 parent 333e6d5 commit c1ee3bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
18 changes: 8 additions & 10 deletions war/WEB-INF/app/channel.js
Expand Up @@ -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;
},

Expand All @@ -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
});
},

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -124,7 +123,6 @@ extend(Channel.prototype, {
},

createSession: function(nick) {
this.expireOldSessions();
var session = new Session(nick);
if (!session) {
return;
Expand All @@ -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;
},
Expand All @@ -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;
},

Expand Down
5 changes: 5 additions & 0 deletions war/WEB-INF/app/public/js/client.js
Expand Up @@ -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");
Expand Down
18 changes: 12 additions & 6 deletions war/WEB-INF/app/public/js/ringochat.js
@@ -1,5 +1,4 @@
(function($) {

var ringoChat = (window.ringoChat = {
connect: function(basePath) {
return new Channel(basePath);
Expand Down Expand Up @@ -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();
},

Expand Down Expand Up @@ -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 + ')')
Expand All @@ -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)();
Expand All @@ -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;
},
Expand Down

0 comments on commit c1ee3bf

Please sign in to comment.