Skip to content

Commit 782f93c

Browse files
committed
Fix disconnection logic
1 parent aff8c3b commit 782f93c

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

lib/pusher-client/socket.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def initialize(app_key, options={})
4545
end
4646

4747
bind('pusher:connection_disconnected') do |data|
48+
@connection = nil
4849
@connected = false
4950
@socket_id = nil
5051
end
@@ -71,23 +72,25 @@ def connect(async = false)
7172
rescue => ex
7273
send_local_event "pusher:error", ex
7374
end
75+
@connection_thread = nil
7476
end
7577
else
7678
connect_internal
7779
end
7880
self
7981
end
8082

81-
def disconnect
83+
def disconnect(ex = nil)
8284
return unless @connection
8385
logger.debug("Pusher : disconnecting")
84-
@connected = false
86+
8587
@connection.close
86-
@connection = nil
87-
if @connection_thread
88-
@connection_thread.kill
89-
@connection_thread = nil
90-
end
88+
89+
send_local_event("pusher:connection_disconnected", ex)
90+
end
91+
92+
def closed?
93+
@connection && @connection.closed?
9194
end
9295

9396
def subscribe(channel_name, user_data = nil)
@@ -100,15 +103,15 @@ def subscribe(channel_name, user_data = nil)
100103
end
101104

102105
channel = @channels.add(channel_name, user_data)
103-
if @connected
106+
if connected
104107
authorize(channel, method(:authorize_callback))
105108
end
106109
return channel
107110
end
108111

109112
def unsubscribe(channel_name)
110113
channel = @channels.remove channel_name
111-
if channel && @connected
114+
if channel && connected
112115
send_event('pusher:unsubscribe', {
113116
'channel' => channel_name
114117
})
@@ -206,12 +209,14 @@ def connect_internal
206209
@connection.receive.each do |msg|
207210
params = parser(msg)
208211

209-
# why ?
212+
# ignore messages to self
210213
next if params['socket_id'] && params['socket_id'] == self.socket_id
211214

212215
send_local_event(params['event'], params['data'], params['channel'])
213216
end
214217
end
218+
rescue IOError => ex
219+
disconnect(ex)
215220
end
216221

217222
def send_local_event(event_name, event_data, channel_name=nil)

lib/pusher-client/websocket.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def close
9696
logger.debug error.message
9797
end
9898

99+
def closed?
100+
@socket.closed?
101+
end
102+
99103
private
100104

101105
attr_reader :logger

0 commit comments

Comments
 (0)