Skip to content

Instantly share code, notes, and snippets.

@dfalmeida
Created March 9, 2018 16:03
Show Gist options
  • Save dfalmeida/b81c86f95a8e13e5697e5d8d57bb18c5 to your computer and use it in GitHub Desktop.
Save dfalmeida/b81c86f95a8e13e5697e5d8d57bb18c5 to your computer and use it in GitHub Desktop.
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