TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✓ |
Maintained by Reid Chatham.
The easiest way to get started is to use CocoaPods. Just add the following line to your 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")
Icon from the Noun Project.