@@ -23,7 +23,7 @@ class Commands
2323 # The queue should store entries in the format:
2424 # [ action, data ]
2525 attr_accessor :message_queue , :_subscribed
26- attr_accessor :_subscribed_callback , :_pinged_callback , :_connected_callback , :_disconnected_callback
26+ attr_accessor :_subscribed_callback , :_rejected_callback , : _pinged_callback, :_connected_callback , :_disconnected_callback
2727
2828 def_delegator :_websocket_client , :onerror , :errored
2929 def_delegator :_websocket_client , :send , :send_msg
@@ -109,6 +109,19 @@ def connected
109109 end
110110 end
111111
112+ # callback when the server rejects the subscription
113+ #
114+ # @example
115+ # client = ActionCableClient.new(uri, 'RoomChannel')
116+ # client.rejected do
117+ # # do things after the server rejects the subscription
118+ # end
119+ def rejected
120+ self . _rejected_callback = proc do |json |
121+ yield ( json )
122+ end
123+ end
124+
112125 # callback when the client receives a confirm_subscription message
113126 # from the action_cable server.
114127 # This is only called once, and signifies that you can now send
@@ -162,6 +175,8 @@ def handle_received_message(message)
162175 elsif is_welcome? ( json )
163176 subscribe
164177 _connected_callback &.call ( json )
178+ elsif is_rejection? ( json )
179+ _rejected_callback &.call ( json )
165180 elsif !subscribed?
166181 check_for_subscribe_confirmation ( json )
167182 else
@@ -174,7 +189,7 @@ def handle_received_message(message)
174189 # {"identifier" => "_ping","type" => "confirm_subscription"}
175190 def check_for_subscribe_confirmation ( message )
176191 message_type = message [ Message ::TYPE_KEY ]
177- return unless Message ::TYPE_CONFIRM_SUBSCRIPTION == message_type
192+ return unless Message ::TYPE_CONFIRM_SUBSCRIPTION == message_type
178193
179194 self . _subscribed = true
180195 _subscribed_callback &.call
@@ -193,6 +208,11 @@ def is_welcome?(message)
193208 Message ::IDENTIFIER_WELCOME == message_identifier
194209 end
195210
211+ def is_rejection? ( message )
212+ message_type = message [ Message ::TYPE_KEY ]
213+ Message ::TYPE_REJECT_SUBSCRIPTION == message_type
214+ end
215+
196216 def subscribe
197217 msg = _message_factory . create ( Commands ::SUBSCRIBE )
198218 send_msg ( msg . to_json )
0 commit comments