@@ -14,7 +14,7 @@ import PromiseKit
1414public class Channel : ChannelProtocol {
1515
1616 public typealias CallBackType = [ String : AnyObject ] -> Void
17- var listeners = [ String: CallBackType] ( )
17+ var listeners = [ String: [ CallBackType] ] ( )
1818 var responders = [ String: CallBackType] ( )
1919 var socketManager : SocketManager
2020 var channelName : String
@@ -47,7 +47,7 @@ public class Channel: ChannelProtocol {
4747 @return Promise<Any>
4848 */
4949 public func listen( name: String , callback: CallBackType ) -> Promise < Any > {
50- listeners. updateValue ( callback, forKey: name)
50+ listeners. updateValue ( createSubList ( name , callback: callback ) , forKey: name)
5151 return subscribe ( generateId ( ) )
5252 }
5353
@@ -67,8 +67,10 @@ public class Channel: ChannelProtocol {
6767 */
6868 public func onBroadcast( dic: [ String : AnyObject ] ) {
6969 let name = dic [ " name " ] as! String
70- if let callBack = self . listeners [ name] {
71- callBack ( dic)
70+ if let subscriberList = self . listeners [ name] {
71+ for callBack in subscriberList {
72+ callBack ( dic)
73+ }
7274 }
7375 }
7476
@@ -111,5 +113,22 @@ public class Channel: ChannelProtocol {
111113 expLogging ( " IDENTIFY \( msg) " )
112114 broadCast ( " 2000 " , params: msg)
113115 }
116+
117+ /**
118+ * Create Subscriber list
119+ * @param name
120+ * @param callback
121+ * @return
122+ */
123+ private func createSubList( name: String , callback: CallBackType ) -> [ CallBackType ] {
124+ var subscriberArray : [ CallBackType ] = [ ]
125+ if let listCallback = self . listeners [ name] {
126+ subscriberArray = listCallback
127+ } else {
128+ self . listeners [ name] = subscriberArray
129+ }
130+ subscriberArray. append ( callback)
131+ return subscriberArray
132+ }
114133
115134}
0 commit comments