1、使用cocoapods集成PingUtil
2、在导入、新增、修改的时候刷新ping值
This commit is contained in:
@ -109,7 +109,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
|
||||
image.isTemplate = true
|
||||
statusItem.image = image
|
||||
statusItem.menu = statusMenu
|
||||
|
||||
ServerProfileManager.instance.refreshPing()
|
||||
|
||||
let notifyCenter = NotificationCenter.default
|
||||
|
||||
@ -555,6 +555,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
|
||||
|
||||
if isChanged {
|
||||
mgr.save()
|
||||
ServerProfileManager.instance.refreshPing()
|
||||
self.updateServersMenu()
|
||||
} else {
|
||||
sendNotify("Not found valid qrcode of shadowsocks profile.", "", "")
|
||||
|
@ -55,3 +55,5 @@
|
||||
"Manual" = "Manual";
|
||||
|
||||
"Show Running Mode On Status Bar" = "Show Running Mode On Status Bar";
|
||||
|
||||
"Timeout" = "Timeout";
|
||||
|
@ -160,6 +160,7 @@ class PreferencesWindowController: NSWindowController
|
||||
}
|
||||
}
|
||||
profileMgr.save()
|
||||
ServerProfileManager.instance.refreshPing()
|
||||
window?.performClose(nil)
|
||||
|
||||
|
||||
|
@ -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")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -58,6 +58,12 @@ class ServerProfileManager: NSObject {
|
||||
}
|
||||
}
|
||||
|
||||
func refreshPing() {
|
||||
for profile in profiles {
|
||||
profile.refreshPing()
|
||||
}
|
||||
}
|
||||
|
||||
func getActiveProfile() -> ServerProfile? {
|
||||
if let id = activeProfileId {
|
||||
for p in profiles {
|
||||
@ -106,6 +112,7 @@ class ServerProfileManager: NSObject {
|
||||
|
||||
self.profiles.append(profile)
|
||||
self.save()
|
||||
self.refreshPing()
|
||||
}
|
||||
NotificationCenter.default.post(name: NOTIFY_SERVER_PROFILES_CHANGED, object: nil)
|
||||
let configsCount = (jsonArr1.object(forKey: "configs") as! [[String: AnyObject]]).count
|
||||
|
@ -65,3 +65,5 @@
|
||||
*/
|
||||
|
||||
"Export Command Copied." = "Export 命令已复制至剪贴板";
|
||||
|
||||
"Timeout" = "超时";
|
||||
|
Reference in New Issue
Block a user