Permalink
Browse files

renaming payload to connection

  • Loading branch information...
1 parent 6b76580 commit 267ace06f0af971c1a75197bcb4bc82ddb18078c @markburns committed Apr 18, 2012
View
10 lib/slanger/connection.rb
@@ -1,32 +1,32 @@
module Slanger
- class Payload
+ class Connection
attr_accessor :socket, :socket_id
def initialize socket, socket_id=nil
@socket, @socket_id = socket, socket_id
end
- def send *args
def send_message m
msg = JSON.parse(m)
s = msg.delete 'socket_id'
socket.send msg.to_json unless s == socket_id
end
+ def send_payload *args
socket.send format(*args)
end
- def establish_connection
+ def establish
@socket_id = SecureRandom.uuid
- send nil, 'pusher:connection_established', { socket_id: @socket_id }
+ send_payload nil, 'pusher:connection_established', { socket_id: @socket_id }
end
def format(channel_id, event_name, payload = {})
{ channel: channel_id, event: event_name, data: payload }.to_json
end
def error e
- send nil, 'pusher:error', e
+ send_payload nil, 'pusher:error', e
end
end
end
View
14 lib/slanger/handler.rb
@@ -9,11 +9,11 @@
module Slanger
class Handler
- attr_accessor :payload
+ attr_accessor :connection
def initialize(socket)
@socket = socket
- @payload = Payload.new(@socket)
+ @connection = Connection.new(@socket)
@subscriptions = {}
authenticate
end
@@ -32,7 +32,7 @@ def onmessage(msg)
end
rescue JSON::ParserError
- payload.error({ code: '5001', message: "Invalid JSON" })
+ connection.error({ code: '5001', message: "Invalid JSON" })
end
def onclose
@@ -42,14 +42,14 @@ def onclose
private
def authenticate
- return payload.establish_connection if valid_app_key?
+ return connection.establish if valid_app_key?
- payload.error({ code: '4001', message: "Could not find app by key #{app_key}" })
+ connection.error({ code: '4001', message: "Could not find app by key #{app_key}" })
@socket.close_websocket
end
def ping(msg)
- send_payload nil, 'pusher:ping'
+ connection.send_payload nil, 'pusher:ping'
end
def pong msg; end
@@ -59,7 +59,7 @@ def subscribe(msg)
klass = subscription_klass channel_id
- @subscriptions[channel_id] = klass.new(payload.socket, payload.socket_id, msg).handle
+ @subscriptions[channel_id] = klass.new(connection, msg).handle
end
def valid_app_key?
View
4 lib/slanger/presence_subscription.rb
@@ -4,7 +4,7 @@ def handle
return handle_invalid_signature if invalid_signature?
unless channel_data?
- return payload.error({
+ return connection.error({
message: "presence-channel is a presence channel and subscription must include channel_data"
})
end
@@ -20,7 +20,7 @@ def channel_data?
def callback
Proc.new {
- payload.send(channel_id, 'pusher_internal:subscription_succeeded', {
+ connection.send_payload(channel_id, 'pusher_internal:subscription_succeeded', {
presence: {
count: channel.subscribers.size,
ids: channel.ids,
View
2 lib/slanger/private_subscription.rb
@@ -4,7 +4,7 @@ def handle
if @msg['data']['auth'] && invalid_signature?
handle_invalid_signature
else
- Subscription.new(payload.socket, payload.socket_id, @msg).handle
+ Subscription.new(connection, @msg).handle
end
end
end
View
18 lib/slanger/subscription.rb
@@ -1,15 +1,21 @@
module Slanger
class Subscription
- attr_accessor :payload, :socket
+ attr_accessor :connection, :socket
- def initialize socket, socket_id, msg
- @payload = Payload.new socket, socket_id
delegate :send_message, to: :connection
+
+ def self.from socket, socket_id, msg
+ connection = Connection.new socket, socket_id
+ new connection, msg
+ end
+
+ def initialize connection, msg
+ @connection = connection
@msg = msg
end
def handle
- payload.send channel_id, 'pusher_internal:subscription_succeeded'
+ connection.send_payload channel_id, 'pusher_internal:subscription_succeeded'
channel.subscribe { |m| send_message m }
end
@@ -39,9 +45,9 @@ def auth
def handle_invalid_signature
message = "Invalid signature: Expected HMAC SHA256 hex digest of "
- message << "#{payload.socket_id}:#{@msg['data']['channel']}, but got #{@msg['data']['auth']}"
+ message << "#{connection.socket_id}:#{channel_id}, but got #{auth}"
- payload.error({ message: message})
+ connection.error({ message: message})
end
end
end

0 comments on commit 267ace0

Please sign in to comment.