如何使用 Multipeer Connectivity 框架邀请用户加入我的会话?

Posted

技术标签:

【中文标题】如何使用 Multipeer Connectivity 框架邀请用户加入我的会话?【英文标题】:How do I invite users to my session using Multipeer Connectivity framework? 【发布时间】:2020-08-10 10:45:05 【问题描述】:

viewDidLoad 我设置了我的会话:

    peerID = MCPeerID(displayName: UIDevice.current.name)
    mcSession = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .required)
    mcSession.delegate = self
    mcAdvertiserAssistant = MCAdvertiserAssistant(serviceType: "hws-kb", discoveryInfo: nil, session: mcSession)

并有四个动作:

func startHosting() 
    mcAdvertiserAssistant.start() //run the session

func endHosting() 
    mcAdvertiserAssistant.stop() //stop the session

func joinSession() 
    let mcBrowser = MCBrowserViewController(serviceType: "hws-kb", session: mcSession)
    mcBrowser.delegate = self
    present(mcBrowser, animated: true) //join me to active session

func leaveSession() 
    mcSession.disconnect() //leave me from active session


//how do Invite others to my session?

以上方式我所能控制的就是当有人想连接时接受或拒绝。但是我想亲自向用户发送邀请,并且不允许其他人在没有我邀请的情况下加入。有可能吗?

【问题讨论】:

【参考方案1】:

据我所知不可能。邀请发送给所有附近的同行。在这个框架中,连接建立是在广告开始时自动发生的。在发送数据时,您将拥有很多控制权,因为会话对象包含所有已连接的对等点。

而且,下面的注释可能会对您有所帮助。

    在游戏应用中,使用浏览器对象创建一个管理员,使用广告客户对象创建多个参与者。

    使用 uuid 创建显示名称,例如 let displayName = UIDevice.current.identifierForVendor?.uuidString

    一个会话仅包含 8 个对等点。如果您的参与者超过 8 人,请按照以下说明操作。

    func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) 
    let filteredSessions = adminSessions.filter  (session) -> Bool in
        return session.connectedPeers.count != kMCSessionMaximumNumberOfPeers
    
    if filteredSessions.count == 0 
        let session =  getNewSession()
        adminSessions.append(session)
        adminBrowser.invitePeer(peerID, to: session, withContext: nil, timeout: 60)
     else 
        let activeSession = filteredSessions[filteredSessions.count-1]
        adminBrowser.invitePeer(peerID, to: activeSession, withContext: nil, timeout: 60)
     
    

【讨论】:

以上是关于如何使用 Multipeer Connectivity 框架邀请用户加入我的会话?的主要内容,如果未能解决你的问题,请参考以下文章