Slide 17
Slide 17 text
final class SessionContainer: NSObject, ObservableObject {
private let session = MCSession(peer: peerId)
private let nearbyServiceBrowser = MCNearbyServiceBrowser(peer: peerId, serviceType: serviceType)
private var selectedPeer: MCPeerID?
override init() {
super.init()
session.delegate = self
nearbyServiceBrowser.delegate = self
PersonaProvider.shared.updateFrame = { [weak self] image in
if let self, let selectedPeer, let data = image.jpegData(compressionQuality: quality) {
try? session.send(data, toPeers: [selectedPeer], with: .reliable)
}
}
}
}
extension SessionContainer: MCNearbyServiceBrowserDelegate {
func browser(_ browser: MCNearbyServiceBrowser, foundPeer: MCPeerID, withDiscoveryInfo: [String: String]?) {
browser.invitePeer(foundPeer, to: session, withContext: nil, timeout: 30)
}
}
extension SessionContainer: MCSessionDelegate {
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
selectedPeer = session.connectedPeers.first
}
}