エクスチェンジ データ

デバイス間の接続が確立されると、データの交換を開始できます。 交換されるデータは、短いテキスト メッセージなどの単純なバイト配列、写真や動画などのファイル、またはデバイスのマイクからの音声ストリームなどのストリームの形式をとることができます。

データは、次の接続マネージャー インスタンス メソッドを使用して送信できます。

  • send(_:to:)
  • startStream(_:to:)
  • sendResource(at:withName:to:)

データを受信するときは、次の接続マネージャー デリゲート メソッドを使用できます。

Swift

extension Example: ConnectionManagerDelegate {
  func connectionManager(
    _ connectionManager: ConnectionManager, didReceive data: Data,
    withID payloadID: PayloadID, from endpointID: EndpointID) {
    // A simple byte payload has been received. This will always include the full data.
  }

  func connectionManager(
    _ connectionManager: ConnectionManager, didReceive stream: InputStream,
    withID payloadID: PayloadID, from endpointID: EndpointID,
    cancellationToken token: CancellationToken) {
    // We have received a readable stream.
  }

  func connectionManager(
    _ connectionManager: ConnectionManager,
    didStartReceivingResourceWithID payloadID: PayloadID,
    from endpointID: EndpointID, at localURL: URL,
    withName name: String, cancellationToken token: CancellationToken) {
    // We have started receiving a file. We will receive a separate transfer update
    // event when complete.
  }

  func connectionManager(
    _ connectionManager: ConnectionManager,
    didReceiveTransferUpdate update: TransferUpdate,
    from endpointID: EndpointID, forPayload payloadID: PayloadID) {
    // A success, failure, cancelation or progress update.
  }
}