Skip to content

Commit

Permalink
Fix disconnection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zimbatm committed Mar 24, 2015
1 parent aff8c3b commit 782f93c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/pusher-client/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def initialize(app_key, options={})
end

bind('pusher:connection_disconnected') do |data|
@connection = nil
@connected = false
@socket_id = nil
end
Expand All @@ -71,23 +72,25 @@ def connect(async = false)
rescue => ex
send_local_event "pusher:error", ex
end
@connection_thread = nil
end
else
connect_internal
end
self
end

def disconnect
def disconnect(ex = nil)
return unless @connection
logger.debug("Pusher : disconnecting")
@connected = false

@connection.close
@connection = nil
if @connection_thread
@connection_thread.kill
@connection_thread = nil
end

send_local_event("pusher:connection_disconnected", ex)
end

def closed?
@connection && @connection.closed?
end

def subscribe(channel_name, user_data = nil)
Expand All @@ -100,15 +103,15 @@ def subscribe(channel_name, user_data = nil)
end

channel = @channels.add(channel_name, user_data)
if @connected
if connected
authorize(channel, method(:authorize_callback))
end
return channel
end

def unsubscribe(channel_name)
channel = @channels.remove channel_name
if channel && @connected
if channel && connected
send_event('pusher:unsubscribe', {
'channel' => channel_name
})
Expand Down Expand Up @@ -206,12 +209,14 @@ def connect_internal
@connection.receive.each do |msg|
params = parser(msg)

# why ?
# ignore messages to self
next if params['socket_id'] && params['socket_id'] == self.socket_id

send_local_event(params['event'], params['data'], params['channel'])
end
end
rescue IOError => ex
disconnect(ex)
end

def send_local_event(event_name, event_data, channel_name=nil)
Expand Down
4 changes: 4 additions & 0 deletions lib/pusher-client/websocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def close
logger.debug error.message
end

def closed?
@socket.closed?
end

private

attr_reader :logger
Expand Down

0 comments on commit 782f93c

Please sign in to comment.