Class
MultipeerTransceiver
public final class MultipeerTransceiver
Handles all aspects related to the multipeer communication.
Initializers
init(configuration:)
public init(configuration: MultipeerConfiguration = .default)
Initializes a new transceiver.
Parameters
Name | Type | Description |
---|---|---|
configuration | MultipeerConfiguration |
|
init(connection:)
init(connection: MultipeerProtocol)
Properties
log
let log
connection
let connection: MultipeerProtocol
availablePeersDidChange
var availablePeersDidChange: ([Peer]) -> Void
Called on the main queue when available peers have changed (new peers discovered or peers removed).
localPeerId
var localPeerId: String?
The current device's peer id
availablePeers
var availablePeers: [Peer]
All peers currently available for invitation, connection and data transmission.
Methods
configure(_:)
private func configure(_ connection: MultipeerProtocol)
receive(_:using:)
public func receive<T: Codable>(_ type: T.Type, using closure: @escaping (_ payload: T, _ sender: Peer) -> Void)
Configures a new handler for a specific Codable
type.
MultipeerKit communicates data between peers as JSON-encoded payloads which originate with
Codable
entities. You register a closure to handle each specific type of entity,
and this closure is automatically called by the framework when a remote peer sends
a message containing an entity that decodes to the specified type.
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
|
closure | @escaping (_ payload: T, _ sender: Peer) -> Void |
|
payload |
|
|
sender |
|
resume()
public func resume()
Resumes the transceiver, allowing this peer to be discovered and to discover remote peers.
stop()
public func stop()
Stops the transceiver, preventing this peer from discovering and being discovered.
broadcast(_:)
public func broadcast<T: Encodable>(_ payload: T)
Sends a message to all connected peers.
Parameters
Name | Type | Description |
---|---|---|
payload | T |
|
send(_:to:)
public func send<T: Encodable>(_ payload: T, to peers: [Peer])
Sends a message to a specific peer.
Parameters
Name | Type | Description |
---|---|---|
payload | T |
|
peers | [Peer] |
|
handleDataReceived(_:from:)
private func handleDataReceived(_ data: Data, from peer: Peer)
invite(_:with:timeout:completion:)
public func invite(_ peer: Peer, with context: Data?, timeout: TimeInterval, completion: InvitationCompletionHandler?)
Manually invite a peer for communicating.
You can call this method to manually invite a peer for communicating if you set the
invitation
parameter to .none
in the transceiver's configuration
.
Parameters
Name | Type | Description |
---|---|---|
peer | Peer |
|
context | Data? |
|
timeout | TimeInterval |
|
completion | InvitationCompletionHandler? |
|
handlePeerAdded(_:)
private func handlePeerAdded(_ peer: Peer)
handlePeerRemoved(_:)
private func handlePeerRemoved(_ peer: Peer)
handlePeerConnected(_:)
private func handlePeerConnected(_ peer: Peer)
handlePeerDisconnected(_:)
private func handlePeerDisconnected(_ peer: Peer)
setConnected(_:on:)
private func setConnected(_ connected: Bool, on peer: Peer)