iOS / Swift:设置打开Streams的超时时间

时间:2018-03-29 14:38:52

标签: ios swift stream timeout

我正在开发一个通过

连接到服务器的iOS应用程序(Swift 4)
Stream.getStreamsToHost(withName: self.host!, port: self.port!, inputStream: &self.inputStream, outputStream: &self.outputStream)
if let instream = self.inputStream, let outstream = self.outputStream {
    instream.delegate = self
    outstream.delegate = self
    instream.schedule(in: .current, forMode: .commonModes)
    outstream.schedule(in: .current, forMode: .commonModes)
    instream.open()
    outstream.open()
}

如果我的互联网连接不良/没有连接/服务器由于某种原因无法访问,我想向用户显示错误消息。这已经有效,但需要差不多一分钟,直到错误“操作无法完成。操作超时”发生。

我可以以某种方式减少我的应用程序中的超时,还是这是一个无法更改的系统范围超时?

Stream.setProperty的常量不会包含任何与超时相关的内容:https://developer.apple.com/documentation/foundation/stream#1666775

我能想象的一种解决方法是在X秒后手动调度一个函数,如果没有建立连接,则取消它。这是实现自定义超时的最佳做法吗?

2 个答案:

答案 0 :(得分:0)

在阅读了几个关于这个问题的线程之后,我决定使用一个在n秒后检查连接是否成功的Timer:

Stream.getStreamsToHost(withName: self.host!, port: self.port!, inputStream: &self.inputStream, outputStream: &self.outputStream)

if let instream = self.inputStream, let outstream = self.outputStream {
    instream.delegate = self
    outstream.delegate = self
    instream.schedule(in: .current, forMode: .commonModes)
    outstream.schedule(in: .current, forMode: .commonModes)
    instream.open()
    outstream.open()

    self.connectionTimer = Timer.scheduledTimer(withTimeInterval: self.CONNECTION_TIMEOUT, repeats: false) { _ in
    if !self.instreamReady || !self.outstreamReady {
        self.errorHandler(NetworkingError.Timeout("Timeout \(self.CONNECTION_TIMEOUT)s exeeded"))
            self.disconnect()
         }
         self.connectionTimer?.invalidate()
         self.connectionTimer = nil
    }
}

作品:)

答案 1 :(得分:0)

var continue = true 变量计数 = 0

            var inp :InputStream?
            var out :OutputStream?
            let testData = mesaj
            Stream.getStreamsToHost(withName: addr, port: port, inputStream: &inp, outputStream: &out)
            let stream = out!
            print( stream.streamStatus.rawValue)
            
            Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
                guard continue == true else{
                    timer.invalidate()
                    return
                }
                if (counting == 0){
                    DispatchQueue.global(qos: .utility).async() {
                        stream.open()
                        var position = 0
                        print( stream.streamStatus.rawValue)
                        while position < testData.count {
                            let length = min(4096, testData.count - position)
                            let amount =  testData[position...].withUnsafeBytes {
                                stream.write($0.bindMemory(to: UInt8.self).baseAddress!, maxLength: length)
                            }
                            if amount <= 0 {
                                // Error or EOF
                                break
                            }
                            position += amount
                        }
                        stream.close()
                        DispatchQueue.main.sync {
                            self.durumTF.text = "Send to PC"
                            self.durumTF.textColor = .blue
                        }
                        continue=false
                    }
                    
                }
                if (counting == 3){
                    print(stream.streamStatus.rawValue)
                    if stream.streamStatus.rawValue == 7 {
                        stream.close()
                        
                        self.durumTF.text = "Couldn't connect to PC"
                        self.durumTF.textColor = .red
                    }
                    if stream.streamStatus.rawValue == 1 {
                        stream.close()
                        
                        self.durumTF.text = "Run server on pc"
                        self.durumTF.textColor = .red
                    }
                    continue = false
                }
                
                counting += 1
                
            }