lib/pusher-client/socket.rb in pusher-client-0.2.2 vs lib/pusher-client/socket.rb in pusher-client-0.3.0
- old
+ new
@@ -3,13 +3,12 @@
require 'digest/md5'
module PusherClient
class Socket
- # Mimick the JavaScript client
CLIENT_ID = 'pusher-ruby-client'
- VERSION = '0.2.2'
+ VERSION = '0.3.0'
PROTOCOL = '5'
attr_accessor :encrypted, :secure
attr_reader :path, :connected, :channels, :global_channel, :socket_id
@@ -39,10 +38,16 @@
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
def connect(async = false)
if @encrypted || @secure
url = "wss://#{HOST}:#{WSS_PORT}#{@path}"
@@ -50,26 +55,26 @@
url = "ws://#{HOST}:#{WS_PORT}#{@path}"
end
PusherClient.logger.debug("Pusher : connecting : #{url}")
@connection_thread = Thread.new {
- options = {:ssl => @encrypted || @secure}
- @connection = WebSocket.new(url, options)
+ options = {:ssl => @encrypted || @secure}
+ @connection = PusherWebSocket.new(url, options)
PusherClient.logger.debug "Websocket connected"
+
loop do
- msg = @connection.receive[0]
- params = parser(msg)
- next if (params['socket_id'] && params['socket_id'] == self.socket_id)
- event_name = params['event']
- event_data = params['data']
- channel_name = params['channel']
- send_local_event(event_name, event_data, channel_name)
+ 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
- return self
+ self
end
def disconnect
if @connected
PusherClient.logger.debug "Pusher : disconnecting"