Permalink
Browse files

client events

  • Loading branch information...
stevegraham committed Feb 10, 2012
1 parent 18c1e95 commit 704658122a3b45aac9ad321200e341c205e26c59
Showing with 72 additions and 3 deletions.
  1. +8 −0 lib/slanger/channel.rb
  2. +15 −2 lib/slanger/handler.rb
  3. +49 −1 spec/integration/integration_spec.rb
View
@@ -18,8 +18,16 @@ def channel
@channel ||= EM::Channel.new
end
+ def send_client_message(message)
+ push message.to_json if authenticated?
+ end
+
def dispatch(message, channel)
push(message.to_json) unless channel =~ /^slanger:/
end
+
+ def authenticated?
+ channel_id =~ /^private-/ || channel_id =~ /^presence-/
+ end
end
end
View
@@ -18,18 +18,30 @@ def onmessage(msg)
if event =~ /^pusher_/
send(event, msg) if respond_to? event, true
+ elsif event =~ /^client-/
+ msg['socket_id'] = @socket_id
+ channel = find_channel msg['channel']
+ channel.try :send_client_message, msg
end
+
end
# Unsubscribe this connection from the channel
def onclose
- const = @channel_id =~ /^presence-/ ? 'PresenceChannel' : 'Channel'
- channel = Slanger.const_get(const).find_by_channel_id(@channel_id)
+ channel = find_channel @channel_id
channel.try :unsubscribe, @subscription_id
end
private
+ def find_channel(channel_id)
+ Slanger.const_get(channel_const channel_id).find_by_channel_id(channel_id)
+ end
+
+ def channel_const(channel_name)
+ channel_name =~ /^presence-/ ? 'PresenceChannel' : 'Channel'
+ end
+
# Verify app key. Send connection_established message to connection if it checks out. Send error message and disconnect if invalid.
def authenticate
app_key = @socket.request['path'].split(/\W/)[2]
@@ -58,6 +70,7 @@ def pusher_ping(msg)
def pusher_pong msg; end
+ #TODO: think about moving all subscription stuff into channel classes
# Add connection to channel subscribers
def subscribe_channel
channel = Slanger::Channel.find_or_create_by_channel_id(@channel_id)
@@ -24,7 +24,7 @@ def new_websocket
Slanger::Service.run
end
# Give Slanger a chance to start
- sleep 0.4
+ sleep 0.6
end
after(:each) do
@@ -186,6 +186,54 @@ def new_websocket
messages.length.should == 2
end
end
+
+ describe 'client events' do
+ it "sends event to other channel subscribers" do
+ client1_messages, client2_messages = [], []
+
+ Thread.new do
+ EM.run do
+ client1, client2 = new_websocket, new_websocket
+ client2_messages, client1_messages = [], []
+
+ client1.callback do
+
+ end
+
+ client1.errback &errback
+
+ client1.stream do |message|
+ client1_messages << JSON.parse(message)
+ if client1_messages.length < 2
+ auth = Pusher['private-channel'].authenticate(client1_messages.first['data']['socket_id'])[:auth]
+ client1.send({ event: 'pusher:subscribe', data: { channel: 'private-channel', auth: auth } }.to_json)
+ elsif client1_messages.length == 3
+ EM.stop
+ end
+ end
+
+ client2.callback do
+
+ end
+
+ client2.errback &errback
+
+ client2.stream do |message|
+ client2_messages << JSON.parse(message)
+ if client2_messages.length < 2
+ auth = Pusher['private-channel'].authenticate(client2_messages.first['data']['socket_id'])[:auth]
+ client2.send({ event: 'pusher:subscribe', data: { channel: 'private-channel', auth: auth } }.to_json)
+ else
+ client2.send({ event: 'client-something', data: { some: 'stuff' }, channel: 'private-channel' }.to_json)
+ end
+ end
+ end
+ end.join
+
+ client1_messages.none? { |m| m['event'] == 'client-something' }
+ client2_messages.one? { |m| m['event'] == 'client-something' }
+ end
+ end
end
describe 'presence channels:' do

0 comments on commit 7046581

Please sign in to comment.