1

我正在开发一个简单的Tic Tac Toe游戏。

我正在尝试MultipeerConnectivity将两个设备连接在一起,这是我的代码:

AppDelegate

var mpcHandler = MPCHandler()

MPCHandler

class MPCHandler: NSObject {
    // Variables
    var peerID: MCPeerID!
    var session: MCSession!
    var browser: MCBrowserViewController!
    var advertiser: MCAdvertiserAssistant?
    
    // Functions
    func setupPeerWithDisplayName (displayName: String) {
        self.peerID = MCPeerID(displayName: displayName)
    }
    func setupSession() {
        self.session = MCSession(peer: self.peerID, securityIdentity: nil, encryptionPreference: MCEncryptionPreference.optional)
        self.session.delegate = self
    }
    func setupBrowser() {
        self.browser = MCBrowserViewController(serviceType: "TicTacToe", session: self.session)
    }
    func advertiseSelf(advertise: Bool) {
        if advertise {
            self.advertiser = MCAdvertiserAssistant(serviceType: "TicTacToe", discoveryInfo: nil, session: self.session)
            self.advertiser?.start()
        }
        else {
            self.advertiser?.stop()
            self.advertiser = nil
        }
    }
}

// Extensions
extension MPCHandler: MCSessionDelegate {
    func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
        switch state {
            case MCSessionState.connected:
                print("Connected: \(peerID.displayName)")

            case MCSessionState.connecting:
                print("Connecting: \(peerID.displayName)")

            case MCSessionState.notConnected:
                print("Not Connected: \(peerID.displayName)")
            }
    }
    func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
        let userInfo: [String : Any] = ["data": data, "peerID:": peerID]
        DispatchQueue.main.async {
            NotificationCenter.default.post(name: Notification.Name("deviceDidReceiveDataNotification"), object: nil, userInfo: userInfo)
        }
    }
    func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {
        
    }
    func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
        
    }
    func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {
        
    }
}

MainVC

var appDelegate: AppDelegate?
    
    // Overridden Functions
    override func viewDidLoad() {
        super.viewDidLoad()
        self.appDelegate = UIApplication.shared.delegate as? AppDelegate
        self.appDelegate?.mpcHandler.setupPeerWithDisplayName(displayName: UIDevice.current.name)
        self.appDelegate?.mpcHandler.setupSession()
        self.appDelegate?.mpcHandler.advertiseSelf(advertise: true)
    }

    @IBAction func barButtonPressed(_ sender: UIBarButtonItem) {
        if self.appDelegate?.mpcHandler.session != nil {
            self.appDelegate?.mpcHandler.setupBrowser()
            self.appDelegate?.mpcHandler.browser.delegate = self
            
            self.present(self.appDelegate?.mpcHandler.browser ?? UIViewController(), animated: true, completion: nil)
        }
    }

Info.plist

添加Bonjour Services_TicTacToe._tcp_TicTacToe._udp

调试时,设备相互发现,但尝试连接时,连接的设备卡在Connecting状态,另一台设备没有收到批准连接的通知。

几秒钟后,它说连接被拒绝。

可能是什么问题?

4

0 回答 0