Peer Connectivity 0.5.4

Peer Connectivity 0.5.4

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最新版本2017年1月
SwiftSwift版本3.0
SPM支持SPM

Reid Chatham维护。



  • 作者:
  • Reid Chatham

PeerConnectivity

对MultipeerConnectivity框架的功能包装。

PeerConnectivity旨在有轻量级、易于使用的语法,具备可扩展性和灵活性,并在后台默默地处理MultipeerConnectivity框架的复杂性和边缘情况。

如果您有任何建议,请打开一个问题或提交一个拉取请求!
查看Playground!

博客文章 https://goo.gl/HJcMbE

阅读文档!

安装

开始使用最简单的方法是使用CocoaPods。只需将以下行添加到您的Podfile中:

pod 'PeerConnectivity', '~> 0.5.4'

创建/停止/启动

var pcm = PeerConnectionManager(serviceType: "local")

// Start
pcm.start()

// Stop
//  - You should always stop the connection manager 
//    before attempting to create a new one
pcm.stop()

// Can join chatrooms using PeerConnectionType.Automatic, .InviteOnly, and .Custom
//  - .Automatic : automatically searches and joins other devices with the same service type
//  - .InviteOnly : provides a browserViewController and invite alert controllers
//  - .Custom : no default behavior is implemented

// The manager can be initialized with a contructed peer representing the local user
// with a custom displayName

pcm = PeerConnectionManager(serviceType: "local", connectionType: .Automatic, displayName: "I_AM_KING")

// Start again at any time
pcm.start() {
    // Do something when finished starting the session
}

向对等点发送事件

let event: [String: Any] = [
    "EventKey" : Date()
]

// Sends to all connected peers
pcm.sendEvent(event)


// Use this to access the connectedPeers
let connectedPeers: [Peer] = pcm.connectedPeers

// Events can be sent to specific peers
if let somePeerThatIAmConnectedTo = connectedPeers.first {
   pcm.sendEvent(event, toPeers: [somePeerThatIAmConnectedTo])
}

监听事件

// Listen to an event
pcm.observeEventListenerForKey("someEvent") { (eventInfo, peer) in

    print("Received some event \(eventInfo) from \(peer.displayName)")
    guard let date = eventInfo["eventKey"] as? Date else { return }
    print(date)

}

// Stop listening to an event
pcm.removeListenerForKey("SomeEvent")

感谢

图标来自名词项目