Skip to content

Commit

Permalink
make disconnectAll public, add static variables so discovery keys wil…
Browse files Browse the repository at this point in the history
…l not clash, also add platform and version keys by default
  • Loading branch information
maxxfrazer committed Sep 16, 2020
1 parent 243d6d0 commit 3a2ad40
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
25 changes: 22 additions & 3 deletions Sources/MultipeerHelper/MultipeerHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import MultipeerConnectivity
import Foundation
#if canImport(RealityKit)
import RealityKit
#endif
Expand All @@ -22,6 +23,10 @@ public class MultipeerHelper: NSObject {
case both = 3
}

public static let compTokenKey = "MPH_CompToken"
public static let osVersionKey = "MPH_OSVersion"
public static let platformKey = "MPH_Platform"

/// Detemines whether your service is advertising, browsing, or both.
public let sessionType: SessionType
public let serviceName: String
Expand Down Expand Up @@ -86,7 +91,10 @@ public class MultipeerHelper: NSObject {
encryptionPreference: encryptionPreference
)
session.delegate = self
self.setupSession()
}

private func setupSession() {
if (self.sessionType.rawValue & SessionType.host.rawValue) != 0 {
var discoveryInfo = self.delegate?.setDiscoveryInfo?()
?? [String: String]()
Expand All @@ -96,10 +104,21 @@ public class MultipeerHelper: NSObject {
let networkLoc = NetworkCompatibilityToken.local
let jsonData = try? JSONEncoder().encode(networkLoc)
if let encodedToken = String(data: jsonData!, encoding: .utf8) {
discoveryInfo["compatibility_token"] = encodedToken
discoveryInfo[MultipeerHelper.compTokenKey] = encodedToken
}
}
#endif
#if os(iOS) || os(tvOS)
discoveryInfo[MultipeerHelper.osVersionKey] = UIDevice.current.systemVersion
#if os(iOS)
discoveryInfo[MultipeerHelper.platformKey] = "iOS"
#else
discoveryInfo[MultipeerHelper.platformKey] = "tvOS"
#endif
#elseif os(macOS)
discoveryInfo[MultipeerHelper.osVersionKey] = ProcessInfo.processInfo.operatingSystemVersionString
discoveryInfo[MultipeerHelper.platformKey] = "macOS"
#endif
serviceAdvertiser = MCNearbyServiceAdvertiser(
peer: myPeerID,
discoveryInfo: discoveryInfo,
Expand Down Expand Up @@ -176,12 +195,12 @@ public class MultipeerHelper: NSObject {

/// Method used for disconnecting all services. Once completed,
/// create a new MultipeerHelper if you want to connect to sessions again.
func disconnectAll() {
public func disconnectAll() {
self.serviceAdvertiser?.stopAdvertisingPeer()
self.serviceBrowser?.stopBrowsingForPeers()
self.serviceAdvertiser = nil
self.serviceBrowser = nil
self.session.disconnect()
self.session?.disconnect()
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/MultipeerHelper/MultipeerHelperDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import MultipeerConnectivity
@objc optional func shouldAcceptJoinRequest(peerID: MCPeerID, context: Data?) -> Bool

/// This will be set as the base for the discoveryInfo, which is sent out by the advertiser (host).
/// The key "compatibility_token" is in use by MultipeerHelper, for checking the
/// The key "MultipeerHelper.compTokenKey" is in use by MultipeerHelper, for checking the
/// compatibility of RealityKit versions.
@objc optional func setDiscoveryInfo() -> [String: String]

Expand All @@ -60,7 +60,7 @@ extension MultipeerHelperDelegate {
/// - Returns: Boolean representing whether or not the two devices
/// have compatible versions of RealityKit.
public static func checkPeerToken(with discoveryInfo: [String: String]?) -> Bool {
guard let compTokenStr = discoveryInfo?["compatibility_token"]
guard let compTokenStr = discoveryInfo?[MultipeerHelper.compTokenKey]
else {
return false
}
Expand Down

0 comments on commit 3a2ad40

Please sign in to comment.