Swift 的 ZetaPush 客户端
入门指南
这些说明将在您的本地机器上设置并运行项目的副本,以进行开发和测试。有关将项目部署到实际系统的说明,请参阅部署部分。
先决条件
您必须已安装并运行 Cocoapods
sudo gem install cocoapods
安装
首先使用 XCode 创建一个新项目。在项目目录中启动命令
pod init
创建一个 PodFile,编辑它并添加 ZetaPushSwift pod
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'ZetaPushTestPod' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for ZetaPushTestPod
pod "ZetaPushSwift"
end
最后,运行pod install安装库
pod install
现在您可以在Xcode中打开新创建的工作空间(.xcworkspace)
前置条件
您必须在ZetaPush上注册账户,并拥有有效的沙箱ID。请注意,在ZetaPush中,沙箱ID用于标识ZetaPush云上托管的应用程序。
对于上面的示例,您必须部署了至少是(弱认证)和一个Echo服务。
要获取更多信息,请阅读完整文档。
任何问题?
编写您的第一个客户端
连接到ZetaPush
您必须将库导入您的swift文件
import ZetaPushSwift
创建一个ZetaPushWeakClient对象。此对象将完成所有繁重工作,以弱认证连接到ZetaPush
import UIKit
import ZetaPushSwift
class ViewController: UIViewController {
let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: "YOUR_SANDBOX_ID", weakDeploymentId: "YOUR_WEAK_DEPLOYMENT_ID")
override func viewDidLoad() {
super.viewDidLoad()
// Connect to ZetaPush
zetaPushWeakClient.connect()
}
}
这就对了,您已连接!
为了验证您是否真的连接了,我们可以添加一个回调来通知连接建立。
import UIKit
import ZetaPushSwift
class ViewController: UIViewController, ClientHelperDelegate {
let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: "YOUR_SANDBOX_ID", weakDeploymentId: "YOUR_WEAK_DEPLOYMENT_ID")
override func viewDidLoad() {
super.viewDidLoad()
// Handle lifecycle events
zetaPushWeakClient.delegate = self
// Connect to ZetaPush
zetaPushWeakClient.connect()
}
// Callback fired when a successful handshake occurs
func onSuccessfulHandshake(_ client:ClientHelper){
// The connection to ZetaPush is a success
print("ViewController SuccessfulHandshake")
}
}
调用第一个服务
现在我们可以调用一个无用的Echo服务,它将“echo”,即为发送给它的任何内容都会发回来。
如果您不熟悉发布-订阅范式,您可以在我们的文档网站上获取更多信息。
总结一下,当你向ZetaPush发送(发布)一条消息时,只有在你之前已请求响应的情况下(订阅)才会接收到响应。
对于Echo服务,你在“echo”动词上发布消息,并在“echo”动词上接收响应。有关所有可以发布的动词及其对应的订阅动词的详细说明,请参阅参考网站。
回到代码上!
import UIKit
import ZetaPushSwift
class ViewController: UIViewController, ClientHelperDelegate {
let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: "YOUR_SANDBOX_ID", weakDeploymentId: "YOUR_WEAK_DEPLOYMENT_ID")
// Declare a new service
var zetaPushServiceEcho : ZetaPushService?
override func viewDidLoad() {
super.viewDidLoad()
// Handle lifecycle events
zetaPushWeakClient.delegate = self
// Create an echo service with the corresponding DeploymentId
zetaPushServiceEcho = ZetaPushService(zetaPushWeakClient, deploymentId: "YOUR_ECHO_DEPLOYMENT_ID")
// Subscribe to the echo verb
_ = zetaPushServiceEcho?.subscribe(verb: "echo", block: { (messageDict) in
print("ViewController zetaPushServiceEcho?.subscribe", messageDict)
})
// Connect to ZetaPush
zetaPushWeakClient.connect()
}
// Callback fired when a successful handshake occurs
func onSuccessfulHandshake(_ client:ClientHelper){
// The connection to ZetaPush is a success
print("ViewController SuccessfulHandshake")
}
// Just call the echo service when we click on a button
@IBAction func OnMyFirstTestDown(_ sender: Any) {
zetaPushServiceEcho?.publish(verb: "echo", parameters: ["hello": "world" as NSObject])
}
}
这就完成了,你已经调用了你的第一个ZetaPush服务。
请注意,ZetaPush提供了大量服务,您可以在其中直接使用。您可以在我们的参考网站上查看完整说明。
使用宏获得更多功能
ZMS概述
您可以在我们的文档网站上阅读ZMS(ZetaPush宏脚本)的概述。
您还可以通过快速入门安装创建第一个宏所需的最小软件。
我们的第一个宏
让我们用Eclipse创建一个简单的宏。这个宏是新项目创建时的默认宏(返回值为__selfName通道)。
/**
* Takes a message as input, and returns it, with a server message
*/
macroscript welcome(/** message from the client */ string message = "Hello") {
// ...
} return {clientMessage : message, serverMessage : WELCOME_MESSAGE} on channel __selfName
现在,在我们的iOS项目中调用它。调用宏有两种方式:使用发布-订阅方式或使用承诺。
使用发布-订阅调用宏
回到代码!
import UIKit
import ZetaPushSwift
class ViewController: UIViewController, ClientHelperDelegate {
let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: "YOUR_SANDBOX_ID", weakDeploymentId: "YOUR_WEAK_DEPLOYMENT_ID")
// Declare a new Macro Service
var zetaPushMacroService: ZetaPushMacroService?
override func viewDidLoad() {
super.viewDidLoad()
// Handle lifecycle events
zetaPushWeakClient.delegate = self
// Create a new Macro Service (with default deployementId: "macro_0")
zetaPushMacroService = ZetaPushMacroService(zetaPushWeakClient)
// Subscribe to the welcome verb
_ = zetaPushMacroService?.subscribe(verb: "welcome", block: { (messageDic) in
print("ViewController zetaPushMacroService?.subscribe", messageDic)
})
// Connect to ZetaPush
zetaPushWeakClient.connect()
}
// Callback fired when a successful handshake occurs
func onSuccessfulHandshake(_ client:ClientHelper){
// The connection to ZetaPush is a success
print("ViewController SuccessfulHandshake")
}
// Just call the macro service when we click on a button
@IBAction func OnMyFirstTestDown(_ sender: Any) {
zetaPushMacroService?.call(verb: "welcome", parameters: ["message": "hello world" as NSObject])
}
}
使用承诺调用宏
Promise 在异步宏调用中非常有用。有关 Promise 的更多信息,可以了解相关的维基百科页面 (链接到中文维基百科)
借助 ZetaPush iOS SDK,您可以借助 Promise 通过“几乎”同步的方式调用宏。
import UIKit
import ZetaPushSwift
class ViewController: UIViewController {
let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: "YOUR_SANDBOX_ID", weakDeploymentId: "YOUR_WEAK_DEPLOYMENT_ID")
// Declare a new Macro Service
var zetaPushMacroService: ZetaPushMacroService?
override func viewDidLoad() {
super.viewDidLoad()
// Handle lifecycle events
zetaPushWeakClient.delegate = self
// Create a new Macro Service (with default deployementId: "macro_0")
zetaPushMacroService = ZetaPushMacroService(zetaPushWeakClient)
// Connect to ZetaPush
zetaPushWeakClient.connect()
}
// Callback fired when a successful handshake occurs
func onSuccessfulHandshake(_ client:ClientHelper){
// The connection to ZetaPush is a success
print("ViewController SuccessfulHandshake")
}
// Just call the macro service when we click on a button
@IBAction func OnMyFirstTestDown(_ sender: Any) {
zetaPushMacroService?.call(verb: "welcome", parameters: ["message": "hello world" as NSObject])
.then{ result in
print ("call result", result)
}
.catch { error in
print ("call error", error)
}
}
}
就这样,您现在可以从 ZetaPush 调用服务和宏。
使用 ZetaPush CLI 自动生成代码
使用 ZetaPush 命令行,您可以根据 ZMS 语言生成 Swift 代码。这段生成的代码将为您提供使用宏所需的所有内容。
这段生成的代码将创建 4 个文件
结构文件
在此文件中,您将获得调用宏所需的所有结构(类):输入类、输出类和完成类。
异步 API
在此文件中,生成了一个类,其中包含调用宏所需的所有方法。
Promise API
在此文件中,生成了一个类,其中包含调用宏所需的所有方法。与异步 API 的主要区别是,返回值是一个 Promise。
AsyncAPIListener
在此文件中,生成了一个类,其中包含接收宏结果所需的所有方法。
如何使用自动生成的代码
创建一个继承自AsyncAPIListener的类
在这个类中,你可以重写适合你需求的方法。
open class MyAPIListener: MacrosAsyncApiListener {
// Only override the method i'm interesting in
open override func sendMessage(_ parameter: SendMessageCompletion){
print("sendMessage")
}
}
当你创建MyAPIListener对象时,每次调用相应的宏时都会调用sendMessage函数。
使用AsyncApi或PromiseApi对象
// Create the objects with a ZetaPushClient parameter
webRTCAsyncApi = WebRTCAsyncApi(zetaPushClient)
webRTCPromiseApi = WebRTCPromiseApi(zetaPushClient)
//To call a promise API
webRTCPromiseApi?.createPublicRoom(parameters: _createPublicRoomInput)
.then { result -> Void in
print ("createPublicRoom", result.result.room!)
}
.catch { error in
print ("createPublicRoom error", error)
}
// To call the same api with the asyncAPI
webRTCAsyncApi?.createPublicRoom(parameters: _createPublicRoomInput)
// The result will be returned to the AsyncAPIListener