BlueSocket
BlueSocket copied to clipboard
Unable to detect closed socket
I'm not reciving zero bytes when the socket loses connection. Could something have changed regarding this?
It used to return bytesRead = 0 when the connection was closed.
socketQueue.async { [weak self] in
var readData = Data()
var shouldKeepRunning = true
do {
repeat {
let bytesRead = try socket.read(into: &readData)
if bytesRead > 0 {
let readString = String(data: readData, encoding: .utf8) ?? ""
self?.gotInput(string: readString)
readData = Data()
} else {
shouldKeepRunning = false
break
}
} while shouldKeepRunning
// close socket when bytesRead is zero
socket.close()
self?.connectionClosed()
} catch {
self?.status = .error(message: error.localizedDescription)
log.error("Read error: \(error). Read data: \(readData)")
}
}
Nothing should have changed here. What is bytesRead returning instead?
@dannys42 bytesRead isn't getting returned anything. The socket is on Wi-Fi and for a test, I turn off Wi-Fi on my computer (to force a disconnect), but nothing happens.
@Joebayld what do you mean? Is it blocking? Or is it returning with a 0 or -1? Can you setup an example program to demonstrate this?