File tree Expand file tree Collapse file tree 2 files changed +37
-14
lines changed
Expand file tree Collapse file tree 2 files changed +37
-14
lines changed Original file line number Diff line number Diff line change @@ -106,13 +106,16 @@ def run_loop
106106
107107 # Ensure the server is running, and ping the remote server if no other messages were sent.
108108 def keep_alive
109+ # We can't ping the remote server if we aren't connected.
110+ return false if @socket . nil? or !@socket . connected?
111+
109112 time_since_last_message = @socket . time_since_last_message
110113
111114 # If the server responded within the specified time, we are okay:
112115 return true if time_since_last_message < websocket_ping
113116
114- # If the client is not connected or the server has not responded for a while:
115- if ! @socket . connected? || time_since_last_message > ( websocket_ping * 2 )
117+ # If the server has not responded for a while:
118+ if time_since_last_message > ( websocket_ping * 2 )
116119 return false
117120 end
118121
Original file line number Diff line number Diff line change 11require 'async/websocket'
2+ require 'async/notification'
23require 'async/clock'
34
45module Slack
@@ -25,33 +26,42 @@ def start_async(client)
2526
2627 def start_reactor ( client )
2728 Async do |task |
28- self . restart_async ( client , @url )
29+ @restart = :: Async :: Notification . new
2930
3031 if client . run_ping?
31- task . async do |subtask |
32+ @ping_task = task . async do |subtask |
3233 subtask . annotate 'client keep-alive'
3334
34- while true
35+ # The timer task will naturally exit after the driver is set to nil.
36+ while @restart
3537 subtask . sleep client . websocket_ping
36- run_ping!
38+ client . run_ping! if @restart
3739 end
3840 end
3941 end
42+
43+ while @restart
44+ if @client_task
45+ @client_task . stop
46+ end
47+
48+ @client_task = task . async do |subtask |
49+ subtask . annotate 'client run-loop'
50+ client . run_loop
51+ end
52+
53+ @restart . wait
54+ end
55+
56+ @ping_task . stop if @ping_task
4057 end
4158 end
4259
4360 def restart_async ( client , new_url )
4461 @url = new_url
4562 @last_message_at = current_time
4663
47- if @client_task
48- @client_task . stop
49- end
50-
51- @client_task = task . async do |subtask |
52- subtask . annotate 'client run-loop'
53- client . run_loop
54- end
64+ @restart . signal if @restart
5565 end
5666
5767 def current_time
@@ -63,6 +73,16 @@ def connect!
6373 run_loop
6474 end
6575
76+ # Kill the restart/ping loop.
77+ def disconnect!
78+ super
79+ ensure
80+ if restart = @restart
81+ @restart = nil
82+ restart . signal
83+ end
84+ end
85+
6686 # Close the socket.
6787 def close
6888 super
You can’t perform that action at this time.
0 commit comments