我正在使用SwiftSocket作为TCP客户端。当发送例如10个单独的数据包时,会有很长的延迟。我该如何加速呢?
示例代码:
@IBAction func slider(_ sender: UISlider) {
guard let client = client else { return }
let posval = Int16(sender.value)
let posvalstring = String(posval)
if (posval != lastpos) {
if let response = sendRequest(string: "\(posvalstring)\n", using: client) {
textView.text = textView.text+"Response: \(response)\n"}
lastpos = posval
}
}
private func ConnectTCP() {
guard let client = client else { return }
switch client.connect(timeout: 10) {
case .success:
textView.text = textView.text+"Connected to host \(client.address)\n"
if let response = sendRequest(string: "HELLO !\n", using: client) {
textView.text = textView.text+"Response: \(response)\n"
}
case .failure(let error):
textView.text = textView.text+String(describing: error)+"\n"
}
}
private func sendRequest(string: String, using client: TCPClient) -> String? {
textView.text = textView.text+"Sending data ...\n"
switch client.send(string: string) {
case .success:
return readResponse(from: client)
case .failure(let error):
textView.text = textView.text+String(describing: error)+"\n"
return nil
}
}