func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
print("stream event \(eventCode)")
switch eventCode {
case .openCompleted:
uiPresenter?.resetUIWithConnection(status: true)
print("Stream opened")
case .hasBytesAvailable:
if aStream == inputStream {
var dataBuffer = Array<UInt8>(repeating: 0, count: self.buffer)
var len: Int
while (inputStream?.hasBytesAvailable)! {
len = (inputStream?.read(&dataBuffer, maxLength: self.buffer))!
print(String(bytes: dataBuffer, encoding: .utf8)!)
if len > 0 {
let output = String(bytes: dataBuffer, encoding: .utf8)
if let output = output {
//print(output)
self.msg = self.msg + output
let arr = output.components(separatedBy: "\r\n\0\r\n")
if arr.count > 1{
for i in stride(from: 0, to: arr.count - 1, by: 1){
if let tmp = arr[i].between("\n\r\0\0\n\r{", "}\n\r\0\0\n\r"){
self.messageReceived(message: tmp)
}
}
//let tmp = arr[arr.count - 1].components(separatedBy: "}\n\r\0\0\n\r")
if let tmp = arr[arr.count - 1].between("\n\r\0\0\n\r{", "}\n\r\0\0\n\r"){
self.messageReceived(message: tmp)
self.msg = ""
}else{
self.msg = arr[arr.count - 1]
}
}else{
//let tmp = arr[0].components(separatedBy: "}\n\r\0\0\n\r")
if let tmp = arr[0].between("\n\r\0\0\n\r{", "}\n\r\0\0\n\r"){
self.messageReceived(message: tmp)
self.msg = ""
}
}
}
}
}
}
case .hasSpaceAvailable:
print("Stream has space available now")
case .errorOccurred:
print("\(aStream.streamError?.localizedDescription ?? "")")
case .endEncountered:
aStream.close()
aStream.remove(from: RunLoop.current, forMode: RunLoop.Mode.default)
print("close stream")
uiPresenter?.resetUIWithConnection(status: false)
default:
print("Unknown event")
}
}
Код для получения потоковых данных указан выше. При запуске пропадает от 250 до 300 байт данных, остальные данные располагаются в правильном порядке. Я проверил сервер, и он отправляет полные данные, но, как мне кажется, swift не сотрудничает.