Browse files

Join and leave events

  • Loading branch information...
1 parent 6c81211 commit e81cb8535fbab0cdd0dfedc0b4c39f9b9589cde7 @winton committed Feb 28, 2011
Showing with 99 additions and 9 deletions.
  1. +2 −1 lib/puggernaut/server/channel.rb
  2. +14 −0 lib/puggernaut/server/http.rb
  3. +12 −0 lib/puggernaut/spec_server.rb
  4. +48 −8 public/puggernaut.js
  5. +23 −0 public/spec.js
View
3 lib/puggernaut/server/channel.rb
@@ -62,7 +62,7 @@ def inhabitants(channel_name)
user_ids.compact
end
- def say(messages)
+ def say(messages, exclude_user_id=nil)
@messages ||= {}
messages = messages.inject({}) do |hash, (channel_name, messages)|
messages = messages.collect do |message|
@@ -77,6 +77,7 @@ def say(messages)
hash
end
@channels.each do |channel|
+ next if exclude_user_id && channel.user_id == exclude_user_id
push = channel.channels.collect do |channel_name|
if messages[channel_name]
messages[channel_name].collect { |message|
View
14 lib/puggernaut/server/http.rb
@@ -22,13 +22,21 @@ def receive_data(data)
end
if path == '/'
+ @join_leave = query['join_leave'].dup[0] rescue nil
+
channels = query['channel'].dup rescue []
lasts = query['last'].dup rescue []
time = query['time'].dup[0] rescue nil
user_id = query['user_id'].dup[0] rescue nil
unless channels.empty?
@channel = Channel.create(channels, user_id)
+ if @join_leave && user_id
+ Channel.say channels.inject({}) { |hash, channel|
+ hash[channel] = "!PUGJOIN#{user_id}"
+ hash
+ }, user_id
+ end
messages = []
if time
messages = channels.inject([]) { |array, channel|
@@ -80,6 +88,12 @@ def unbind
if @subscription_id
@channel.unsubscribe(@subscription_id)
logger.info "Sever::Http#unbind - Unsubscribe - #{@channel.channels.join(", ")}"
+ if @join_leave && @channel.user_id
+ Channel.say @channel.channels.inject({}) { |hash, channel|
+ hash[channel] = "!PUGLEAVE#{@channel.user_id}"
+ hash
+ }, @channel.user_id
+ end
Channel.channels.delete @channel
end
end
View
12 lib/puggernaut/spec_server.rb
@@ -33,6 +33,18 @@ class SpecServer < Sinatra::Base
e.message
end
end
+
+ get '/join_leave_join' do
+ begin
+ client = Puggernaut::Client.new("localhost:8101")
+ client.push :single => [
+ "!PUGJOINtest", "!PUGLEAVEtest", "!PUGJOINtest"
+ ]
+ client.close
+ rescue Exception => e
+ e.message
+ end
+ end
get '/multiple' do
begin
View
56 public/puggernaut.js
@@ -8,24 +8,27 @@ var Puggernaut = new function() {
this.inhabitants = inhabitants;
this.unwatch = unwatch;
this.watch = watch;
+ this.watch_join = watch_join;
+ this.watch_leave = watch_leave;
var channels = {};
var errors = 0;
var events = $('<div/>');
+ var leaves = {};
var started = false;
var request;
- function ajax(time, user_id) {
+ function ajax(join_leave, time, user_id) {
if (channelLength() > 0 && !self.disabled && errors <= 10) {
started = true;
request = $.ajax({
cache: false,
- data: params(time, user_id),
+ data: params(join_leave, time, user_id),
dataType: 'text',
error: function(xhr, status, error) {
if (started && status != 'abort') {
errors += 1;
- ajax();
+ ajax(join_leave, null, user_id);
}
},
success: function(data, status, xhr) {
@@ -35,10 +38,23 @@ var Puggernaut = new function() {
line = line.split('|', 4);
if (line[0] && typeof channels[line[0]] != 'undefined') {
channels[line[0]] = line[1];
- events.trigger(line[0], [ line[2], new Date(line[3]) ]);
+ if (line[2].substring(0, 8) == '!PUGJOIN') {
+ var id = line[2].substring(8)
+ if (leaves[id]) {
+ delete leaves[id];
+ clearTimeout(leaves[id]);
+ } else
+ events.trigger('join_' + line[0], id);
+ } else if (line[2].substring(0, 9) == '!PUGLEAVE') {
+ var id = line[2].substring(9)
+ leaves[id] = setTimeout(function() {
+ events.trigger('leave_' + line[0], id);
+ }, 10000);
+ } else
+ events.trigger(line[0], [ line[2], new Date(line[3]) ]);
}
});
- ajax();
+ ajax(join_leave, null, user_id);
}
},
timeout: 100000,
@@ -72,7 +88,7 @@ var Puggernaut = new function() {
});
}
- function params(time, user_id) {
+ function params(join_leave, time, user_id) {
var ch = [];
var la = [];
@@ -86,6 +102,8 @@ var Puggernaut = new function() {
if (la.length)
data.last = la;
+ if (join_leave)
+ data.join_leave = join_leave;
if (time)
data.time = time + '';
if (user_id)
@@ -102,6 +120,8 @@ var Puggernaut = new function() {
$.each(args, function(i, item) {
delete channels[item];
events.unbind(item);
+ events.unbind('join_' + item);
+ events.unbind('leave_' + item);
});
} else
events.unbind();
@@ -111,10 +131,12 @@ var Puggernaut = new function() {
function watch() {
var ch = $.makeArray(arguments);
var fn = ch.pop();
- var user_id, time;
+ var join_leave, time, user_id;
if (ch[ch.length-1] && ch[ch.length-1].constructor === Object) {
var options = ch.pop();
+
+ join_leave = options.join_leave;
time = options.time;
user_id = options.user_id;
}
@@ -126,9 +148,27 @@ var Puggernaut = new function() {
});
if (!started)
- ajax(time, user_id);
+ ajax(join_leave, time, user_id);
}
return this;
}
+
+ function watch_join() {
+ var args = $.makeArray(arguments);
+ var fn = args.pop();
+ $.each(args, function(i, item) {
+ events.bind('join_' + item, fn);
+ });
+ return this;
+ }
+
+ function watch_leave() {
+ var args = $.makeArray(arguments);
+ var fn = args.pop();
+ $.each(args, function(i, item) {
+ events.bind('leave_' + item, fn);
+ });
+ return this;
+ }
};
View
23 public/spec.js
@@ -72,6 +72,29 @@ $(function() {
start();
});
});
+
+ module("Single message join/leave/join", {
+ setup: function() {
+ Puggernaut
+ .watch('single', { join_leave: true }, function(e, message, time) {})
+ .watch_join('single', function(e, user_id) {
+ equals(user_id, 'test');
+ setTimeout(function() {
+ Puggernaut.unwatch('single');
+ start();
+ }, 1000);
+ })
+ .watch_leave('single', function(e, user_id) {
+ ok(false);
+ });
+ }
+ });
+
+ test("should trigger join event without leave", function() {
+ stop();
+ expect(1);
+ $.get('/join_leave_join');
+ });
module("Multiple messages", {
setup: function() {

0 comments on commit e81cb85

Please sign in to comment.