Dữ liệu của sàn giao dịch

Sau khi thiết lập kết nối giữa các thiết bị, bạn có thể bắt đầu trao đổi dữ liệu. Dữ liệu trao đổi có thể ở dạng một mảng byte đơn giản, chẳng hạn như một tin nhắn văn bản ngắn; một tệp, chẳng hạn như ảnh hoặc video; hoặc một luồng, chẳng hạn như luồng âm thanh từ micrô của thiết bị.

Bạn có thể gửi dữ liệu bằng các phương thức thực thể trình quản lý kết nối sau:

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

Bạn có thể sử dụng các phương thức uỷ quyền trình quản lý kết nối sau khi nhận dữ liệu.

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.
  }
}