@@ -23,8 +23,9 @@ module Cable
2323 class Server
2424 include Debug
2525
26+ # The String key is the `connection_identifier` value for `Cable::Connection`
27+ getter connections = {} of String => Cable ::Connection
2628 getter errors = 0
27- getter connections = {} of String => Connection
2829 getter fiber_channel = ::Channel ({String , String }).new
2930 getter pinger : Cable ::RedisPinger do
3031 Cable ::RedisPinger .new(self )
@@ -40,7 +41,6 @@ module Cable
4041 end
4142
4243 @channels = {} of String => Channels
43- @_internal_subscriptions = {} of String => Cable ::Connection
4444 @channel_mutex = Mutex .new
4545
4646 def initialize
@@ -65,18 +65,6 @@ module Cable
6565 connections.delete(connection_id).try(& .close)
6666 end
6767
68- def add_internal_subscription (internal_channel : String , connection : Cable ::Connection )
69- if internal_channel.presence && ! connection.closed?
70- @_internal_subscriptions [internal_channel] = connection
71- end
72- end
73-
74- def remove_internal_subscription (internal_channel : String )
75- if @_internal_subscriptions .has_key?(internal_channel)
76- @_internal_subscriptions .delete(internal_channel)
77- end
78- end
79-
8068 def subscribe_channel (channel : Channel , identifier : String )
8169 @channel_mutex .synchronize do
8270 if ! @channels .has_key?(identifier)
@@ -132,12 +120,13 @@ module Cable
132120 Cable .settings.on_error.call(e, " IO::Error Exception: #{ e.message } -> #{ self .class.name} #send_to_channels(channel, message)" )
133121 end
134122
135- def send_to_internal_channels (channel_identifier : String , message : String )
136- if internal_channel = @_internal_subscriptions [channel_identifier ]?
123+ def send_to_internal_connections (connection_identifier : String , message : String )
124+ if internal_connection = connections[connection_identifier ]?
137125 case message
138126 when Cable .message(:disconnect )
139- Cable ::Logger .info { " Removing connection (#{ channel_identifier } )" }
140- internal_channel.close
127+ Cable ::Logger .info { " Removing connection (#{ connection_identifier } )" }
128+ internal_connection.close
129+ remove_connection(connection_identifier)
141130 end
142131 end
143132 end
@@ -182,8 +171,10 @@ module Cable
182171 spawn(name: " Cable::Server - process_subscribed_messages" ) do
183172 while received = fiber_channel.receive
184173 channel, message = received
185- if channel.includes?(" cable_internal" )
186- server.send_to_internal_channels(channel, message)
174+ if channel.starts_with?(" cable_internal" )
175+ identifier = channel.split('/' ).last
176+ connection_identifier = server.connections.keys.find!(& .starts_with?(identifier))
177+ server.send_to_internal_connections(connection_identifier, message)
187178 else
188179 server.send_to_channels(channel, message)
189180 end
0 commit comments