Skip to content

Instantly share code, notes, and snippets.

@fitomad

fitomad/ni_connect.swift Secret

Last active Jun 25, 2020
Embed
What would you like to do?
extension CercaViewModel: MCNearbyServiceBrowserDelegate
{
...
/// 1
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String: String]?) -> Void
{
guard let info = info,
let identity = info["identity"],
let multipeerSession = self.multipeerSession,
(identity == self.serviceIdentity && multipeerSession.connectedPeers.count < self.maxPeersInSession)
else
{
return
}
browser.invitePeer(peerID, to: multipeerSession, withContext: nil, timeout: 10)
}
...
}
extension CercaViewModel: MCSessionDelegate
{
...
/// 2
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState)
{
DispatchQueue.main.async
{
switch state
{
case .connected:
self.peerName = peerID.displayName
self.peer = peerID
// 3
self.shareTokenWithAllPeers()
self.isConnectionLost = false
case .notConnected:
self.isConnectionLost = true
case .connecting:
self.peerName = "Hola ¿Quién eres? 👋"
@unknown default:
fatalError("Ha aparecido un estado nuevo de la enumeración. Ni idea lo que hacer.")
}
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.