|
| 1 | +# MultipeerTransceiver |
| 2 | + |
| 3 | +Handles all aspects related to the multipeer communication. |
| 4 | + |
| 5 | +``` swift |
| 6 | +public final class MultipeerTransceiver |
| 7 | +``` |
| 8 | + |
| 9 | +## Initializers |
| 10 | + |
| 11 | +### `init(configuration:)` |
| 12 | + |
| 13 | +Initializes a new transceiver. |
| 14 | + |
| 15 | +``` swift |
| 16 | +public init(configuration: MultipeerConfiguration = .default) |
| 17 | +``` |
| 18 | + |
| 19 | +#### Parameters |
| 20 | + |
| 21 | + - configuration: The configuration, uses the default configuration if none specified. |
| 22 | + |
| 23 | +## Properties |
| 24 | + |
| 25 | +### `availablePeersDidChange` |
| 26 | + |
| 27 | +Called on the main queue when available peers have changed (new peers discovered or peers removed). |
| 28 | + |
| 29 | +``` swift |
| 30 | +var availablePeersDidChange: ([Peer]) -> Void |
| 31 | +``` |
| 32 | + |
| 33 | +### `peerAdded` |
| 34 | + |
| 35 | +Called on the main queue when a new peer discovered. |
| 36 | + |
| 37 | +``` swift |
| 38 | +var peerAdded: (Peer) -> Void |
| 39 | +``` |
| 40 | + |
| 41 | +### `peerRemoved` |
| 42 | + |
| 43 | +Called on the main queue when a peer removed. |
| 44 | + |
| 45 | +``` swift |
| 46 | +var peerRemoved: (Peer) -> Void |
| 47 | +``` |
| 48 | + |
| 49 | +### `availablePeers` |
| 50 | + |
| 51 | +All peers currently available for invitation, connection and data transmission. |
| 52 | + |
| 53 | +``` swift |
| 54 | +var availablePeers: [Peer] |
| 55 | +``` |
| 56 | + |
| 57 | +## Methods |
| 58 | + |
| 59 | +### `receive(_:using:)` |
| 60 | + |
| 61 | +Configures a new handler for a specific `Codable` type. |
| 62 | + |
| 63 | +``` swift |
| 64 | +public func receive<T: Codable>(_ type: T.Type, using closure: @escaping (_ payload: T) -> Void) |
| 65 | +``` |
| 66 | + |
| 67 | +MultipeerKit communicates data between peers as JSON-encoded payloads which originate with |
| 68 | +`Codable` entities. You register a closure to handle each specific type of entity, |
| 69 | +and this closure is automatically called by the framework when a remote peer sends |
| 70 | +a message containing an entity that decodes to the specified type. |
| 71 | + |
| 72 | +#### Parameters |
| 73 | + |
| 74 | + - type: The `Codable` type to receive. |
| 75 | + - closure: The closure that will be called whenever a payload of the specified type is received. |
| 76 | + - payload: The payload decoded from the remote message. |
| 77 | + |
| 78 | +### `resume()` |
| 79 | + |
| 80 | +Resumes the transceiver, allowing this peer to be discovered and to discover remote peers. |
| 81 | + |
| 82 | +``` swift |
| 83 | +public func resume() |
| 84 | +``` |
| 85 | + |
| 86 | +### `stop()` |
| 87 | + |
| 88 | +Stops the transceiver, preventing this peer from discovering and being discovered. |
| 89 | + |
| 90 | +``` swift |
| 91 | +public func stop() |
| 92 | +``` |
| 93 | + |
| 94 | +### `broadcast(_:)` |
| 95 | + |
| 96 | +Sends a message to all connected peers. |
| 97 | + |
| 98 | +``` swift |
| 99 | +public func broadcast<T: Encodable>(_ payload: T) |
| 100 | +``` |
| 101 | + |
| 102 | +#### Parameters |
| 103 | + |
| 104 | + - payload: The payload to be sent. |
| 105 | + |
| 106 | +### `send(_:to:)` |
| 107 | + |
| 108 | +Sends a message to a specific peer. |
| 109 | + |
| 110 | +``` swift |
| 111 | +public func send<T: Encodable>(_ payload: T, to peers: [Peer]) |
| 112 | +``` |
| 113 | + |
| 114 | +#### Parameters |
| 115 | + |
| 116 | + - payload: The payload to be sent. |
| 117 | + - peers: An array of peers to send the message to. |
| 118 | + |
| 119 | +### `invite(_:with:timeout:completion:)` |
| 120 | + |
| 121 | +Manually invite a peer for communicating. |
| 122 | + |
| 123 | +``` swift |
| 124 | +public func invite(_ peer: Peer, with context: Data?, timeout: TimeInterval, completion: InvitationCompletionHandler?) |
| 125 | +``` |
| 126 | + |
| 127 | +You can call this method to manually invite a peer for communicating if you set the |
| 128 | +`invitation` parameter to `.none` in the transceiver's `configuration`. |
| 129 | + |
| 130 | +> Warning: If the invitation parameter is not set to `.none`, you shouldn't call this method, since the transceiver does the inviting automatically. |
| 131 | + |
| 132 | +#### Parameters |
| 133 | + |
| 134 | + - peer: The peer to be invited. |
| 135 | + - context: Custom data to be sent alongside the invitation. |
| 136 | + - timeout: How long to wait for the remote peer to accept the invitation. |
| 137 | + - completion: Called when the invitation succeeds or fails. |
0 commit comments