gpt4 book ai didi

ios - iOS Swift Web Socket SRWebSocket设置响应超时

转载 作者:行者123 更新时间:2023-12-01 16:21:26 79 4
gpt4 key购买 nike

我在我的应用程序中使用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

最佳答案

如果您想在没有响应时显示一些消息,可以使用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()
}
}

关于ios - iOS Swift Web Socket SRWebSocket设置响应超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58724044/

79 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com