Created
March 9, 2018 16:03
-
-
Save dfalmeida/b81c86f95a8e13e5697e5d8d57bb18c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func joinChannel(with channelId: String, | |
payload: Socket.Payload, | |
channelEventListeners: [ChannelEventListener], | |
onJoinSuccess: @escaping (Socket.Payload) -> (), | |
onJoinError: @escaping (Socket.Payload) -> ()) { | |
guard let socket = socket else { return } | |
let joinChannelBlock: () -> () = {[weak self] in | |
guard | |
let weakSelf = self | |
else { return } | |
let channel: Channel = socket.channel(channelId, payload: payload) | |
//map listeners to channel | |
for listener in channelEventListeners { | |
channel.on(listener.event, callback: listener.callback) | |
} | |
channel.join()? | |
.receive("ok", callback: onJoinSuccess) | |
.receive("error", callback: onJoinError) | |
weakSelf.channels[channelId] = channel | |
} | |
// Verify if the socket is connected | |
if !socket.isConnected { | |
connect(with: joinChannelBlock) | |
} else { | |
joinChannelBlock() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment