1、使用cocoapods集成PingUtil
2、在导入、新增、修改的时候刷新ping值
This commit is contained in:
@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import XYPingUtil
|
||||
|
||||
|
||||
class ServerProfile: NSObject, NSCopying {
|
||||
@ -23,6 +24,8 @@ class ServerProfile: NSObject, NSCopying {
|
||||
@objc var enabledKcptun: Bool = false
|
||||
@objc var kcptunProfile = KcptunProfile()
|
||||
|
||||
@objc var ping:Int = 0
|
||||
|
||||
override init() {
|
||||
uuid = UUID().uuidString
|
||||
}
|
||||
@ -126,6 +129,7 @@ class ServerProfile: NSObject, NSCopying {
|
||||
|
||||
copy.enabledKcptun = self.enabledKcptun
|
||||
copy.kcptunProfile = self.kcptunProfile.copy() as! KcptunProfile
|
||||
copy.ping = self.ping
|
||||
return copy;
|
||||
}
|
||||
|
||||
@ -148,6 +152,9 @@ class ServerProfile: NSObject, NSCopying {
|
||||
if let kcptunData = data["KcptunProfile"] {
|
||||
profile.kcptunProfile = KcptunProfile.fromDictionary(kcptunData as! [String:Any?])
|
||||
}
|
||||
if let ping = data["Ping"] as? NSNumber {
|
||||
profile.ping = ping.intValue
|
||||
}
|
||||
}
|
||||
|
||||
if let id = data["Id"] as? String {
|
||||
@ -172,6 +179,7 @@ class ServerProfile: NSObject, NSCopying {
|
||||
d["OTA"] = ota as AnyObject?
|
||||
d["EnabledKcptun"] = NSNumber(value: enabledKcptun)
|
||||
d["KcptunProfile"] = kcptunProfile.toDictionary() as AnyObject
|
||||
d["Ping"] = NSNumber(value: ping)
|
||||
return d
|
||||
}
|
||||
|
||||
@ -315,10 +323,22 @@ class ServerProfile: NSObject, NSCopying {
|
||||
}
|
||||
|
||||
func title() -> String {
|
||||
var ping = self.ping == 0 ? "" : "(\(self.ping)ms)"
|
||||
if self.ping == -1 {
|
||||
ping = "(\("Timeout".localized))"
|
||||
}
|
||||
if remark.isEmpty {
|
||||
return "\(serverHost):\(serverPort)"
|
||||
return "\(serverHost):\(serverPort)\(ping)"
|
||||
} else {
|
||||
return "\(remark) (\(serverHost):\(serverPort))"
|
||||
return "\(remark) (\(serverHost):\(serverPort))\(ping)"
|
||||
}
|
||||
}
|
||||
|
||||
func refreshPing() {
|
||||
PingUtil.pingHost(serverHost, success: { (ping) in
|
||||
self.ping = ping
|
||||
}, failure: {
|
||||
print("ping failure")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user