LCLPing 是一个用 Swift 编写的跨平台 Ping 库,专为 Swift 而生。它旨在帮助简化客户端和服务器端应用程序及服务的网络可达性和延迟的测试、测量和监控。
将以下内容添加到您的 Package.swift
文件中
.package(url: "https://github.com/Local-Connectivity-Lab/lcl-ping.git", from: "1.0.0")
然后将模块导入到您的项目中
.target(
name: "YourAppName",
.dependencies: [
.product(name: "LCLPing", package: "lcl-ping")
]
)
// Reachability Test
let isReachable = LCLPing.reachable(via: .icmp, strategy: .multiple, host: "google.com")
print("is reachable: \(isReachable)")
// Run Ping Test
// create ping configuration for each run
let icmpConfig = ICMPPingClient.Configuration(endpoint: .ipv4("127.0.0.1", 0), count: 1)
let httpConfig = try HTTPPingClient.Configuration(url: "http://127.0.0.1:8080", count: 1)
// initialize test client
let icmpClient = ICMPPingClient(configuration: icmpConfig)
let httpClient = HTTPPingClient(configuration: httpConfig)
do {
// run the test using SwiftNIO EventLoopFuture
let result = try icmpClient.start().wait()
print(result)
} catch {
print("received: \(error)")
}
do {
let result = try httpClient.start().wait()
print(result)
} catch {
print("received: \(error)")
}
您还可以使用 make demo
或 swift run Demo
运行演示,如果您没有安装 make。
欢迎任何贡献和 Pull Request!但是,在您计划实现某些功能或尝试修复不确定的问题之前,建议先发起讨论。您也可以加入我们的 Discord 频道,或访问我们的 网站。
LCLPing 基于 Apache License 许可发布。有关更多详细信息,请参阅 LICENSE。