2

我在我的应用程序中使用 SRWebSocket/SocketRocket 库我无法为其设置响应超时。如果套接字在几秒钟后没有收到任何数据,我想显示一条消息

以下是我的代码

socketRocket = SRWebSocket(url: URL(string: streamUrl)!)
socketRocket?.delegate = self
socketRocket?.open()

也使用以下回调方法

func webSocketDidOpen(_ webSocket: SRWebSocket!) {
    print("webSocketDidOpen")
    showTableView()
}

func webSocket(_ webSocket: SRWebSocket!, didCloseWithCode code: Int, reason: String!, wasClean: Bool) {
    getConversationId()
}

func webSocket(_ webSocket: SRWebSocket!, didFailWithError error: Error!) {
    print("didFailWithError\(error)")
    showLErrorView()
}

func webSocket(_ webSocket: SRWebSocket!, didReceiveMessage message: Any!) {
}

这是图书馆的链接

https://github.com/facebook/SocketRocket

4

1 回答 1

0

如果你想在没有得到回复时显示一些消息,你可以使用 a Timer,它看起来像这样的基础:

var timer: Timer?
var responseTimeout: TimeInterval = 15

func startTimer() {
    timer?.invalidate()
    timer = Timer.scheduledTimer(withTimeInterval: responseTimeout, repeats: false, block: {[weak self] timer in
        self?.onResponseTimeout()
    })
}

func onResponseTimeout() {
    print("Didn't get response")
}

func webSocket(_ webSocket: SRWebSocket!, didReceiveMessage message: Any!) {
    print(message)
    timer?.invalidate() //invalidate timer when you get a message
}

然后您可以startTimer()在需要时调用,即打开套接字时:

func webSocketDidOpen(_ webSocket: SRWebSocket!) {
    print("webSocketDidOpen")
    showTableView()
    startTimer()
}

或在您发送消息后:

socketRocket?.send(message)
startTimer()

如果您还想为连接设置超时(它与 resopone timout 不同),您可以使用URLRequest代替URL

let request = URLRequest(url: url, timeoutInterval: 30) //it is 30 sec connection timeout

socketRocket = SRWebSocket(urlRequest: request)
socketRocket?.delegate = self
socketRocket?.open()

支持连接和响应超时的完整类:

import Foundation
import SocketRocket

class WebSocket: NSObject, SRWebSocketDelegate {

    var socketRocket: SRWebSocket?
    var timer: Timer?

    var responseTimeout: TimeInterval = 15

    func open(url: URL, connectionTimeout: TimeInterval = 10) {
        let request = URLRequest(url: url, timeoutInterval: connectionTimeout)
        socketRocket = SRWebSocket(urlRequest: request)
        socketRocket?.delegate = self
        socketRocket?.open()
    }

    func send(message: String) {
        socketRocket?.send(message)
        startTimer()
    }

    func startTimer() {
        timer?.invalidate()
        timer = Timer.scheduledTimer(withTimeInterval: responseTimeout, repeats: false, block: {[weak self] timer in
            self?.onResponseTimeout()
        })
    }

    func onResponseTimeout() {
        print("Didn't get response")
    }

    func webSocketDidOpen(_ webSocket: SRWebSocket!) {
        print("webSocketDidOpen")
        startTimer()
    }

    func webSocket(_ webSocket: SRWebSocket!, didCloseWithCode code: Int, reason: String!, wasClean: Bool) {
        print("closed with code: \(code), reason: \(reason), wasClean: \(wasClean)")
        timer?.invalidate()
    }

    func webSocket(_ webSocket: SRWebSocket!, didFailWithError error: Error!) {
        print("didFailWithError\(error)")
        timer?.invalidate()
    }

    func webSocket(_ webSocket: SRWebSocket!, didReceiveMessage message: Any!) {
        print(message)
        timer?.invalidate()
    }
}
于 2019-11-15T13:20:05.390 回答