【问题标题】:How do I invite users to my session using Multipeer Connectivity framework?如何使用 Multipeer Connectivity 框架邀请用户加入我的会话?
【发布时间】: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?

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

【问题讨论】:

    标签: ios swift multipeer-connectivity


    【解决方案1】:

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

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

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

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

    3. 一个会话仅包含 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)
      } }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2013-10-01
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      相关资源
      最近更新 更多