Exchange data

  • After establishing connections between devices, data exchange can commence, encompassing various formats like byte arrays, files, or streams.

  • ConnectionManager offers methods such as send(_:to:) , startStream(_:to:) , and sendResource(at:withName:to:) for initiating data transmission.

  • To receive data, implement the ConnectionManagerDelegate protocol, providing methods to handle byte payloads, streams, file transfers, and transfer updates.

  • While payloads of the same type maintain their sending order upon arrival, there's no guarantee of order preservation across different payload types.

Once connections are established between devices, you can start exchanging data. The exchanged data can take the form of a simple byte array, such as a short text message; a file, such as a photo or video; or a stream, such as the audio stream from the device’s microphone.

Data can be sent using the following connection manager instance methods:

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

The following connection manager delegate methods can be utilized when receiving data.

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. 
  
 } 
 } 
 
Create a Mobile Website
View Site in Mobile | Classic
Share by: