通过 Sublimation 和 Ngrok,轻松地与您的 Apple 设备共享本地开发服务器。
sequenceDiagram
participant DevServer as Development Server
participant Sub as Sublimation (Server)
participant Ngrok as Ngrok (https://ngrok.com)
participant KVdb as KVdb (https://kvdb.io)
participant SubClient as Sublimation (Client)
participant App as iOS/watchOS App
DevServer->>Sub: Start development server
Sub->>Ngrok: Request public URL
Ngrok-->>Sub: Provide public URL<br/>(https://abc123.ngrok.io)
Sub->>KVdb: Store URL with bucket and key<br/>(bucket: "fdjf9012k20cv", key: "dev-server",<br/>url: https://abc123.ngrok.io)
App->>SubClient: Request server URL<br/>(bucket: "fdjf9012k20cv", key: "dev-server")
SubClient->>KVdb: Request URL<br/>(bucket: "fdjf9012k20cv", key: "dev-server")
KVdb-->>SubClient: Provide stored URL<br/>(https://abc123.ngrok.io)
SubClient-->>App: Return server URL<br/>(https://abc123.ngrok.io)
App->>Ngrok: Connect to development server<br/>(https://abc123.ngrok.io)
Ngrok->>DevServer: Forward request to local server
Ngrok 是一个很棒的服务,可以为外部访问设置本地开发服务器。 假设您需要共享您的本地开发服务器,因为您正在真实的设备上进行测试,该设备无法通过您的本地网络访问您的机器。 您可以运行 ngrok
来设置一个 https 地址,该地址会隧道连接到您的本地开发服务器。
> vapor run serve -p 1337
> ngrok http 1337
现在您将收到一条消息,说明您的 vapor 应用程序正在通过 ngrok 提供服务
Forwarding https://c633-2600-1702-4050-7d30-cc59-3ffb-effa-6719.ngrok.io -> https://:1337
使用 SublimationNgrok,您可以将地址(例如 https://c633-2600-1702-4050-7d30-cc59-3ffb-effa-6719.ngrok.io
)保存到键值存储中,并在开发期间从您的 Apple 设备提取该地址。
Apple 平台
Linux
要使用 SPM 将 SublimationNgrok 集成到您的应用程序中,请在您的 Package.swift 文件中指定它
let package = Package(
...
dependencies: [
.package(url: "https://github.com/brightdigit/SublimationNgrok.git", from: "1.0.0")
],
targets: [
.target(
name: "YourServerApp",
dependencies: [
.product(name: "SublimationNgrok", package: "SublimationNgrok"), ...
]),
...
]
)
对于客户端 API,您只需要包中的 SublimationKVdb
产品。
如果您尚未在 ngrok 注册帐户并通过 homebrew 安装命令行工具。 接下来,让我们使用 kvdb.io 设置一个键值存储,目前支持它。 如果您有其他服务,请在存储库中创建一个 issue。 您的反馈很有帮助。
在 kvdb.io 注册并获取您将使用的 bucket 名称。 您将在设置中使用它。 基本上,您需要三个组件
/opt/homebrew/bin/ngrok
,但您可以在安装后使用:which ngrok
找到它将这些内容保存在您的服务器和客户端都可以访问的共享配置中,例如 enum
public enum SublimationConfiguration {
public static let bucketName = "fdjf9012k20cv"
public static let key = "my-"
}
创建您的 Sublimation
对象时,您将需要使用提供的便捷初始化器 TunnelSublimatory.init(ngrokPath:bucketName:key:application:isConnectionRefused:ngrokClient:)
,以便更容易地将 ngrok 与 TunnelSublimatory
集成
let tunnelSublimatory = TunnelSublimatory(
ngrokPath: "/opt/homebrew/bin/ngrok", // path to ngrok executable
bucketName: SublimationConfiguration.bucketName, // "fdjf9012k20cv"
key: SublimationConfiguration.key, // "dev-server"
application: { myVaporApplication }, // pass your Vapor.Application here
isConnectionRefused: {$.isConnectionRefused}, // supplied by `SublimationVapor`
transport: AsyncHTTPClientTransport() // ClientTransport for Vapor
)
let sublimation = Sublimation(sublimatory: tunnelSublimatory)
对于客户端,您需要导入 SublimationKVdb
模块并通过以下方式检索 url
import SublimationKVdb
let hostURL = try await KVdb.url(
withKey: SublimationConfiguration.key,
atBucket: SublimationConfiguration.bucketName
)
要了解更多信息,请查看完整的 文档。
此代码在 MIT 许可证下分发。 有关更多信息,请参阅 LICENSE 文件。