简体   繁体   English

Swift SimplePing 不起作用

[英]Swift SimplePing doesn't work

I'm using SimplePing to get the latency in my swift application but I don't get any result.我正在使用 SimplePing 来获取我的 swift 应用程序中的延迟,但我没有得到任何结果。

This is the code taken form an example here in SO:这是从 SO 中的示例中获取的代码:

import Foundation

public typealias SimplePingClientCallback = (String?)->()

public class SimplePingClient: NSObject {
    static let singletonPC = SimplePingClient()

    private var resultCallback: SimplePingClientCallback?
    private var pingClinet: SimplePing?
    private var dateReference: NSDate?

    public static func pingHostname(hostname: String, andResultCallback callback: SimplePingClientCallback?) {
        singletonPC.pingHostname(hostname, andResultCallback: callback)
    }

    public func pingHostname(hostname: String, andResultCallback callback: SimplePingClientCallback?) {
        resultCallback = callback
        pingClinet = SimplePing(hostName: hostname)
        pingClinet?.delegate = self
        pingClinet?.start()
    }
}

extension SimplePingClient: SimplePingDelegate {
    public func simplePing(pinger: SimplePing, didStartWithAddress address: NSData) {
        pinger.sendPingWithData(nil)
    }

    public func simplePing(pinger: SimplePing, didFailWithError error: NSError) {
        resultCallback?(nil)
    }

    public func simplePing(pinger: SimplePing!, didSendPacket packet: NSData!) {
        dateReference = NSDate()
    }

    public func simplePing(pinger: SimplePing!, didFailToSendPacket packet: NSData!, error: NSError!) {
        pinger.stop()
        resultCallback?(nil)
    }

    public func simplePing(pinger: SimplePing, didReceiveUnexpectedPacket packet: NSData) {
        pinger.stop()
        resultCallback?(nil)
    }

    public func simplePing(pinger: SimplePing!, didReceivePingResponsePacket packet: NSData!) {
        pinger.stop()

        guard let dateReference = dateReference else { return }

        //timeIntervalSinceDate returns seconds, so we convert to milis
        let latency = NSDate().timeIntervalSinceDate(dateReference) * 1000

        resultCallback?(String(format: "%.f", latency))
    }
}

And to call the SimplePing in my viewController I do that:并在我的 viewController 中调用 SimplePing 我这样做:

let simplePingClient = SimplePingClient()
        simplePingClient.pingHostname("www.apple.com") { latency in
        print("Your latency is \(latency ?? "unknown")")
        self.labelTempsReponse?.text = latency
        }

When I put a breakpoint at this line:当我在这一行放置断点时:

simplePingClient.pingHostname("www.apple.com") { latency in

all the results are nil (the resultCallBack, the pingClient and the dateReference).所有结果都为零(resultCallBack、pingClient 和 dateReference)。 I made a research to solve this issue and I found a question similar to mine but in objective c.我做了一项研究来解决这个问题,我发现了一个与我类似的问题,但在目标 c 中。

Can anyone help me please?有人可以帮我吗?

The signatures in SimplePingClient don't match the SimplePingDelegate protocol it is trying to implement. SimplePingClient的签名SimplePingClient试图实现的SimplePingDelegate协议不匹配。 Perhaps these once matched in some earlier version, but as of Swift 5/XCode 12.2 and using Apple's SimplePing code last updated 2016 they don't.也许这些曾经在某个早期版本中匹配过,但是从 Swift 5/XCode 12.2 开始,并且使用Apple 的 SimplePing 代码最后更新的 2016 年他们没有。 Some datatypes are wrong, some parameter labels mismatched, and some parameters missing.某些数据类型错误,某些参数标签不匹配,某些参数丢失。 This means SimplePingClient never gets its callbacks because SimplePing looks for a matching signature in the delegate object, finds none, and skips the callback.这意味着SimplePingClient永远不会得到它的回调,因为SimplePing在委托对象中寻找匹配的签名,没有找到,并跳过回调。

Further, because SimplePing.h declares these delegate methods as optional you get no compiler errors.此外,因为SimplePing.h这些委托方法声明为可选的,所以不会出现编译器错误。

To see the errors, and to get XCode to offer to write protocol stubs for you, comment out the @optional in the SimplePingDelegate definition in SimplePing.h (line 142).要查看错误并让 XCode 为您编写协议存根,请在SimplePing.h (第 142 行)的SimplePingDelegate定义中SimplePing.h

Here are the changes to the SimplePingClient class extension I needed to make to get SimplePingClient to work:以下是我需要对SimplePingClient类扩展进行的更改,以使 SimplePingClient 正常工作:

extension SimplePingClient: SimplePingDelegate {
    
    public func simplePing(_ pinger: SimplePing, didStartWithAddress address: Data) {
        pinger.send(with: nil)
    }

    public func simplePing(_ pinger: SimplePing, didFailWithError error: Error) {
        resultCallback?(nil)
    }

    public func simplePing(_ pinger: SimplePing, didSendPacket packet: Data, sequenceNumber:UInt16) {
        dateReference = Date()
    }

    public func simplePing(_ pinger: SimplePing, didFailToSendPacket packet: Data, sequenceNumber: UInt16, error: Error) {
        pinger.stop()
        resultCallback?(nil)
    }

    public func simplePing(_ pinger: SimplePing, didReceiveUnexpectedPacket packet: Data) {
        pinger.stop()
        resultCallback?(nil)
    }

    public func simplePing(_ pinger: SimplePing, didReceivePingResponsePacket packet: Data, sequenceNumber:UInt16) {
        pinger.stop()

        guard let dateReference = dateReference else { return }

        //timeIntervalSinceDate returns seconds, so we convert to milis
        let latency = Date().timeIntervalSince(dateReference ) * 1000

        resultCallback?(String(format: "%.f", latency))
    }
}

could it be the case that simplePingClient is going out of scope ?会不会是 simplePingClient 超出范围的情况? In this case, the simplePing will shutdown.在这种情况下,simplePing 将关闭。 Try to declare as a global variable:尝试声明为全局变量:

var simplePingClient : SimplePingClient!

and then this should work:然后这应该有效:

simplePingClient = SimplePingClient()
    simplePingClient.pingHostname("www.apple.com") { latency in
    print("Your latency is \(latency ?? "unknown")")
    self.labelTempsReponse?.text = latency
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM