简体   繁体   English

Multipeer Connectivity如何获得广告同行的名字?

[英]Multipeer Connectivity how to get the name of the advertising peer?

iOS12 Swift 5.x iOS12 Swift 5.x

I using Multipeer Connectivity. 我使用Multipeer Connectivity。 So far it works well, if I connect my devices in the correct order. 到目前为止,如果我以正确的顺序连接我的设备,它运行良好。

Using boiler plate code, tracked down the problem to here. 使用锅炉板代码,将问题追踪到这里。

func setupStream() {
  do {
    outputStream = try session.startStream(withName: "chat", toPeer: session.connectedPeers.first!)
    for debug in session.connectedPeers {
      print("Peer connected \(debug.displayName)")
    }
  } catch {
      print("unable to open stream")
  }
  if let outputStream = outputStream {
    outputStream.delegate = self
    outputStream.schedule(in: RunLoop.main, forMode:RunLoop.Mode.default)
    outputStream.open()
  }
}

Now if I start the advertiser first, and then the browsers... it works well, very well. 现在,如果我首先启动广告客户,然后是浏览器...它运行良好,非常好。 But if I start the browsers BEFORE the advertiser, they seem to see each other... even if I am not advertising from them and the service from them and connect to the wrong client. 但是,如果我在广告客户之前启动浏览器,他们似乎互相看到了......即使我不是从他们那里做广告,也不是来自他们的服务并连接到错误的客户端。

How to say connect to the peer adverting the service and not anybody and everybody you find... 怎么说连接到同行广告服务而不是任何人和你找到的每个人...

iOS 12, Swift 5 iOS 12,Swift 5

Found a solution which I post here, a nice simple one. 找到了我在这里发布的解决方案,一个很简单的解决方案。 When I advertised the service I do so with some discovery info. 当我通过一些发现信息来宣传该服务时。 The key here, the discover variable. 这里的关键是发现变量。

Block looks this. Block看起来如此。

class ColorService : NSObject {

lazy var session : MCSession = {
    let session = MCSession(peer: self.myPeerId, securityIdentity: nil, encryptionPreference: .required)
    session.delegate = self
    return session
}()

 var delegate : ColorServiceDelegate?

// Service type must be a unique string, at most 15 characters long
// and can contain only ASCII lowercase letters, numbers and hyphens.
private let ColorServiceType = "example-color"
private let myPeerId = MCPeerID(displayName: UIDevice.current.name)
private let serviceAdvertiser : MCNearbyServiceAdvertiser

override init() {
    let discover:[String:String] = ["prime":myPeerId.displayName]
    self.serviceAdvertiser = MCNearbyServiceAdvertiser(peer: myPeerId, discoveryInfo: discover, serviceType: ColorServiceType)

    super.init()

    self.serviceAdvertiser.delegate = self
    self.serviceAdvertiser.startAdvertisingPeer()

}

deinit {
    self.serviceAdvertiser.stopAdvertisingPeer()
}

func stopAdvertising() {
  self.serviceAdvertiser.stopAdvertisingPeer()
}

On the other side, when I am looking for the service, I only invite peers who have the discovering set, obviously using said info as the peer to invite. 另一方面,当我正在寻找服务时,我只邀请具有发现集的同伴,显然使用所述信息作为同伴邀请。

extension ColorSearch : MCNearbyServiceBrowserDelegate {

func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error) {
    NSLog("%@", "didNotStartBrowsingForPeers: \(error)")
}

func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
    NSLog("%@", "foundPeer: \(peerID)")
    NSLog("%@", "invitePeer: \(peerID)")
    NSLog("%@", "discoverInfo: \(info)")
    primePeer = info!["prime"]
    if primePeer != nil {
      if peerID.displayName == primePeer {
        browser.invitePeer(peerID, to: self.session, withContext: nil, timeout: 10)
      }
    }

}

func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
    NSLog("%@", "lostPeer: \(peerID)")
}

func disconnect() {
  self.session.disconnect()
}

And I don't get a matrix of connections, I get a single master and a bunch of slaves. 我没有连接矩阵,我得到一个主人和一堆奴隶。 Maybe not what multipeerconnectivity was intended, but a good solution never the less. 也许不是多重连接的意图,但一个好的解决方案永远不会少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将设备名称设置为会话中的对等方Multipeer连接swift3 - set device name to a peer in session Multipeer connectivity swift3 在多点连接中邀请对等方时偶尔会发生崩溃 - Occasional crash when inviting peer in multipeer connectivity 对等(MultiPeer Connectivity)已连接但未存储在connectedPeers中 - Peer (MultiPeer Connectivity) is connected but not stored in connectedPeers 多重连接框架 - 丢失的对等体停留在会话中 - Multipeer Connectivity Framework - Lost Peer stays in Session 具有 Wifi 直连功能的点对点 android 和 iOS(多点连接?) - Peer to peer android and iOS with Wifi direct (multipeer connectivity?) Multipeer Connectivity MCSession在连接导致断开连接时停止播发 - Multipeer Connectivity MCSession Stop Advertising when connected results in disconnect 对等方未找到哪个已连接到其他网络。 多点连接框架 - Peer is not finding which is connected to other network. Multipeer connectivity Framework 多对等连接-如何查看找到的设备 - Multipeer connectivity- how to view the found devices 如何正确拆除多对等连接会话? - How to correctly tear down multipeer connectivity session? iOS 7:通过Wi-Fi或对等Wi-Fi使用Multipeer Connectivity连接设备 - iOS 7 : Connecting devices using Multipeer Connectivity via wi-fi or peer to peer wi-fi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM