22
33require "thread"
44
5- gem "redis" , ">= 3" , "< 5 "
5+ gem "redis" , ">= 3" , "< 6 "
66require "redis"
77
88require "active_support/core_ext/hash/except"
@@ -56,7 +56,7 @@ def redis_connection_for_broadcasts
5656 end
5757
5858 def redis_connection
59- self . class . redis_connector . call ( @server . config . cable . merge ( id : identifier ) )
59+ self . class . redis_connector . call ( @server . config . cable . symbolize_keys . merge ( id : identifier ) )
6060 end
6161
6262 class Listener < SubscriberMap
@@ -69,7 +69,7 @@ def initialize(adapter, event_loop)
6969 @subscribe_callbacks = Hash . new { |h , k | h [ k ] = [ ] }
7070 @subscription_lock = Mutex . new
7171
72- @raw_client = nil
72+ @subscribed_client = nil
7373
7474 @when_connected = [ ]
7575
@@ -78,13 +78,13 @@ def initialize(adapter, event_loop)
7878
7979 def listen ( conn )
8080 conn . without_reconnect do
81- original_client = conn . respond_to? ( :_client ) ? conn . _client : conn . client
81+ original_client = extract_subscribed_client ( conn )
8282
8383 conn . subscribe ( "_action_cable_internal" ) do |on |
8484 on . subscribe do |chan , count |
8585 @subscription_lock . synchronize do
8686 if count == 1
87- @raw_client = original_client
87+ @subscribed_client = original_client
8888
8989 until @when_connected . empty?
9090 @when_connected . shift . call
@@ -106,7 +106,7 @@ def listen(conn)
106106 on . unsubscribe do |chan , count |
107107 if count == 0
108108 @subscription_lock . synchronize do
109- @raw_client = nil
109+ @subscribed_client = nil
110110 end
111111 end
112112 end
@@ -119,8 +119,8 @@ def shutdown
119119 return if @thread . nil?
120120
121121 when_connected do
122- send_command ( " unsubscribe" )
123- @raw_client = nil
122+ @subscribed_client . unsubscribe
123+ @subscribed_client = nil
124124 end
125125 end
126126
@@ -131,13 +131,13 @@ def add_channel(channel, on_success)
131131 @subscription_lock . synchronize do
132132 ensure_listener_running
133133 @subscribe_callbacks [ channel ] << on_success
134- when_connected { send_command ( " subscribe" , channel ) }
134+ when_connected { @subscribed_client . subscribe ( channel ) }
135135 end
136136 end
137137
138138 def remove_channel ( channel )
139139 @subscription_lock . synchronize do
140- when_connected { send_command ( " unsubscribe" , channel ) }
140+ when_connected { @subscribed_client . unsubscribe ( channel ) }
141141 end
142142 end
143143
@@ -156,22 +156,49 @@ def ensure_listener_running
156156 end
157157
158158 def when_connected ( &block )
159- if @raw_client
159+ if @subscribed_client
160160 block . call
161161 else
162162 @when_connected << block
163163 end
164164 end
165165
166- def send_command ( *command )
167- @raw_client . write ( command )
166+ if ::Redis ::VERSION < "5"
167+ class SubscribedClient
168+ def initialize ( raw_client )
169+ @raw_client = raw_client
170+ end
171+
172+ def subscribe ( *channel )
173+ send_command ( "subscribe" , *channel )
174+ end
175+
176+ def unsubscribe ( *channel )
177+ send_command ( "unsubscribe" , *channel )
178+ end
179+
180+ private
181+ def send_command ( *command )
182+ @raw_client . write ( command )
168183
169- very_raw_connection =
170- @raw_client . connection . instance_variable_defined? ( :@connection ) &&
171- @raw_client . connection . instance_variable_get ( :@connection )
184+ very_raw_connection =
185+ @raw_client . connection . instance_variable_defined? ( :@connection ) &&
186+ @raw_client . connection . instance_variable_get ( :@connection )
172187
173- if very_raw_connection && very_raw_connection . respond_to? ( :flush )
174- very_raw_connection . flush
188+ if very_raw_connection && very_raw_connection . respond_to? ( :flush )
189+ very_raw_connection . flush
190+ end
191+ nil
192+ end
193+ end
194+
195+ def extract_subscribed_client ( conn )
196+ raw_client = conn . respond_to? ( :_client ) ? conn . _client : conn . client
197+ SubscribedClient . new ( raw_client )
198+ end
199+ else
200+ def extract_subscribed_client ( conn )
201+ conn
175202 end
176203 end
177204 end
0 commit comments