Reputation: 981
In my iOS App I use GCDAsyncSocket
library to communicate with a local server running on another iPad. So multiple iPads communicate with each other locally, and one of them acts as a server.
When a client connects to the server iPad the following GCDAsyncSocketDelegate
triggers:
func socket(_ sock: GCDAsyncSocket, didConnectToHost host: String, port: UInt16) {
sock.readData(toLength: UInt(UInt64.byteWidth), withTimeout: -1, tag: 0)
delay(0.1, queue: socket.delegateQueue, closure: { [weak self] in
sock.write(dataToSend, withTimeout: -1, tag: 0)
})
connected = true
}
Once the client is connected to the server, it writes some data to that socket (server), however, this data doesn't reach the server. But, if I wrap the write call in a delay function then the data will reach its destination.
Here is my delay function:
func delay(_ delay: Double, queue: DispatchQueue?, closure: @escaping () -> ()) {
let delayTime = DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
let q = queue ?? DispatchQueue.global()
q.asyncAfter(deadline: delayTime) {
closure()
}
}
What I am missing here?
Upvotes: 0
Views: 149