Class: PusherClient::Socket
- Inherits:
-
Object
- Object
- PusherClient::Socket
- Defined in:
- lib/pusher-client/socket.rb
Constant Summary collapse
- CLIENT_ID =
'pusher-ruby-client'
- VERSION =
'0.3.0'
- PROTOCOL =
'5'
Instance Attribute Summary collapse
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#connected ⇒ Object
readonly
Returns the value of attribute connected.
-
#encrypted ⇒ Object
Returns the value of attribute encrypted.
-
#global_channel ⇒ Object
readonly
Returns the value of attribute global_channel.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#secure ⇒ Object
Returns the value of attribute secure.
-
#socket_id ⇒ Object
readonly
Returns the value of attribute socket_id.
Instance Method Summary collapse
- #[](channel_name) ⇒ Object
-
#authorize(channel, callback) ⇒ Object
auth for private and presence.
- #authorize_callback(channel, auth_data, channel_data) ⇒ Object
- #bind(event_name, &callback) ⇒ Object
- #connect(async = false) ⇒ Object
- #disconnect ⇒ Object
- #get_presence_auth(channel) ⇒ Object
- #get_private_auth(channel) ⇒ Object
-
#initialize(application_key, options = {}) ⇒ Socket
constructor
A new instance of Socket.
- #is_presence_channel(channel_name) ⇒ Object
- #is_private_channel(channel_name) ⇒ Object
- #send_event(event_name, data) ⇒ Object
- #subscribe(channel_name, user_id = nil) ⇒ Object
- #subscribe_all ⇒ Object (also: #subscribeAll)
- #unsubscribe(channel_name) ⇒ Object
Constructor Details
#initialize(application_key, options = {}) ⇒ Socket
Returns a new instance of Socket.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pusher-client/socket.rb', line 15 def initialize(application_key, ={}) raise ArgumentError if (!application_key.is_a?(String) || application_key.size < 1) @path = "/app/#{application_key}?client=#{CLIENT_ID}&version=#{VERSION}&protocol=#{PROTOCOL}" @key = application_key @secret = [:secret] @socket_id = nil @channels = Channels.new @global_channel = Channel.new('pusher_global_channel') @global_channel.global = true @secure = false @connected = false @encrypted = [:encrypted] || false bind('pusher:connection_established') do |data| socket = JSON.parse(data) @connected = true @socket_id = socket['socket_id'] subscribe_all end bind('pusher:connection_disconnected') do |data| @channels.channels.each { |c| c.disconnect } end bind('pusher:error') do |data| PusherClient.logger.fatal("Pusher : error : #{data.inspect}") end # Keep this in case we're using a websocket protocol that doesn't # implement ping/pong bind('pusher:ping') do send_event('pusher:pong', nil) end end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
13 14 15 |
# File 'lib/pusher-client/socket.rb', line 13 def channels @channels end |
#connected ⇒ Object (readonly)
Returns the value of attribute connected.
13 14 15 |
# File 'lib/pusher-client/socket.rb', line 13 def connected @connected end |
#encrypted ⇒ Object
Returns the value of attribute encrypted.
12 13 14 |
# File 'lib/pusher-client/socket.rb', line 12 def encrypted @encrypted end |
#global_channel ⇒ Object (readonly)
Returns the value of attribute global_channel.
13 14 15 |
# File 'lib/pusher-client/socket.rb', line 13 def global_channel @global_channel end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/pusher-client/socket.rb', line 13 def path @path end |
#secure ⇒ Object
Returns the value of attribute secure.
12 13 14 |
# File 'lib/pusher-client/socket.rb', line 12 def secure @secure end |
#socket_id ⇒ Object (readonly)
Returns the value of attribute socket_id.
13 14 15 |
# File 'lib/pusher-client/socket.rb', line 13 def socket_id @socket_id end |
Instance Method Details
#[](channel_name) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/pusher-client/socket.rb', line 115 def [](channel_name) if @channels[channel_name] @channels[channel_name] else @channels << channel_name end end |
#authorize(channel, callback) ⇒ Object
auth for private and presence
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/pusher-client/socket.rb', line 130 def (channel, callback) if is_private_channel(channel.name) auth_data = get_private_auth(channel) elsif is_presence_channel(channel.name) auth_data = get_presence_auth(channel) channel_data = @user_data end # could both be nil if didn't require auth callback.call(channel, auth_data, channel_data) end |
#authorize_callback(channel, auth_data, channel_data) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/pusher-client/socket.rb', line 141 def (channel, auth_data, channel_data) send_event('pusher:subscribe', { 'channel' => channel.name, 'auth' => auth_data, 'channel_data' => channel_data }) channel.acknowledge_subscription(nil) end |
#bind(event_name, &callback) ⇒ Object
110 111 112 113 |
# File 'lib/pusher-client/socket.rb', line 110 def bind(event_name, &callback) @global_channel.bind(event_name, &callback) return self end |
#connect(async = false) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/pusher-client/socket.rb', line 51 def connect(async = false) if @encrypted || @secure url = "wss://#{HOST}:#{WSS_PORT}#{@path}" else url = "ws://#{HOST}:#{WS_PORT}#{@path}" end PusherClient.logger.debug("Pusher : connecting : #{url}") @connection_thread = Thread.new { = {:ssl => @encrypted || @secure} @connection = PusherWebSocket.new(url, ) PusherClient.logger.debug "Websocket connected" loop do msg = @connection.receive[0] next if msg.nil? params = parser(msg) next if params['socket_id'] && params['socket_id'] == self.socket_id send_local_event params['event'], params['data'], params['channel'] end } @connection_thread.run @connection_thread.join unless async self end |
#disconnect ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/pusher-client/socket.rb', line 79 def disconnect if @connected PusherClient.logger.debug "Pusher : disconnecting" @connection.close @connection_thread.kill if @connection_thread @connected = false else PusherClient.logger.warn "Disconnect attempted... not connected" end end |
#get_presence_auth(channel) ⇒ Object
164 165 166 167 168 |
# File 'lib/pusher-client/socket.rb', line 164 def get_presence_auth(channel) string_to_sign = @socket_id + ':' + channel.name + ':' + @user_data signature = HMAC::SHA256.hexdigest(@secret, string_to_sign) return "#{@key}:#{signature}" end |
#get_private_auth(channel) ⇒ Object
158 159 160 161 162 |
# File 'lib/pusher-client/socket.rb', line 158 def get_private_auth(channel) string_to_sign = @socket_id + ':' + channel.name signature = HMAC::SHA256.hexdigest(@secret, string_to_sign) return "#{@key}:#{signature}" end |
#is_presence_channel(channel_name) ⇒ Object
154 155 156 |
# File 'lib/pusher-client/socket.rb', line 154 def is_presence_channel(channel_name) channel_name.match(/^presence-/) end |
#is_private_channel(channel_name) ⇒ Object
150 151 152 |
# File 'lib/pusher-client/socket.rb', line 150 def is_private_channel(channel_name) channel_name.match(/^private-/) end |
#send_event(event_name, data) ⇒ Object
174 175 176 177 178 |
# File 'lib/pusher-client/socket.rb', line 174 def send_event(event_name, data) payload = {'event' => event_name, 'data' => data}.to_json @connection.send(payload) PusherClient.logger.debug("Pusher : sending event : #{payload}") end |
#subscribe(channel_name, user_id = nil) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/pusher-client/socket.rb', line 90 def subscribe(channel_name, user_id = nil) @user_data = {:user_id => user_id}.to_json unless user_id.nil? channel = @channels << channel_name if @connected (channel, method(:authorize_callback)) end return channel end |
#subscribe_all ⇒ Object Also known as: subscribeAll
123 124 125 126 127 |
# File 'lib/pusher-client/socket.rb', line 123 def subscribe_all @channels.channels.clone.each{ |k,v| subscribe(k) } end |
#unsubscribe(channel_name) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/pusher-client/socket.rb', line 100 def unsubscribe(channel_name) channel = @channels.remove channel_name if @connected send_event('pusher:unsubscribe', { 'channel' => channel_name }) end return channel end |