🇵🇰 🇯🇵
MeteorDDPSwift 5编写的 Meteor 服务器客户端!
以下 WebSocket 方法用于将 MeteorDDP 连接到 Meteor 服务器。
- Starscream Min iOS 8
- URLSessionWebSocketTask Min iOS 13
目录
MeteorDDP
描述
灵感来自 SwiftDDP,用于 Meteor 服务器,使用 Swift 5 编写!
MeteorDDP 对于使用原生 Swift 在 iOS 上集成为 JavaScript 编写的 Meteor 服务器非常有帮助。
Meteor 是一个基于 Node 的全栈框架,允许创建响应式 Web 应用,这些应用可以轻松移植到 Android 和 iOS 平台。响应式 Web 应用意味着实时行为:客户端和服务器之间存在持续连接,因此,通过任何方式(直接在数据库中输入、从服务器那边,甚至是客户端)对应用程序所做的更改会反映在应用程序的每个实例中,无需重新加载页面。
演示
MeteorDDP
是 iOS 平台上为 Meteor 服务器提供的客户端,它提供通过 Meteor 服务器绑定的所有事件的观察者。
要运行示例项目,首先克隆仓库,然后从 Example 目录中运行 pod install
。
要求
- iOS 8.0+
- Xcode 11.0+
- Swift 5+
安装
可以使用 CocoaPods、Carthage 或手动方式安装 MeteorDDP
。
CocoaPods
MeteorDDP
通过 CocoaPods 提供。要安装 CocoaPods,运行:
$ gem install cocoapods
然后创建一个包含以下内容的 Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'MeteorDDP'
end
最后,运行以下命令来安装它
$ pod install
Carthage
要安装 Carthage,运行(使用 Homebrew):
$ brew update
$ brew install carthage
然后将以下行添加到您的 Cartfile 中
github "EngrAhsanAli/MeteorDDP" "master"
然后将库导入到您使用它的所有文件和全局实例中
import MeteorDDP
let meteor: MeteorClient = {
MeteorClient(url: url, webSocket: .webSocketTask, requestTimeout: 15)
}()
// By default the preferred websocket method is starscream
## 手动安装
如果您不希望使用上述任何一种依赖管理器,可以通过将“Classes”文件夹中的文件添加到您的项目并将全局实例创建如下方式,手动整合 MeteorDDP
到您的项目中。
入门指南
以下是如何在 Swift 中配置 meteor 的示例。
let meteor: MeteorClient = {
MeteorClient(url: url, webSocket: .starscream)
}()
// Global meteor subscriptions manager (optional to manage through this way)
let meteorCollection: MeteorCollections = {
MeteorCollections(client: meteor)
}()
public static var loggingLevel: Level = .normal
连接到 Meteor 服务器
meteor.connect {
// do something after the client connects
}
使用电子邮件和密码登录。
meteor.loginWithPassword("[email protected]", password: "********") { (result, error) in
if error != nil {
//Login Failed
}
else {
// do something after login
}
}
使用用户名和密码登录。
meteor.loginWithUsername("Ali", password: "********") { (result, error) in
if error != nil {
//Login Failed
}
else {
// do something after login
}
}
无论如何登录(无论有效期内的电子邮件还是用户名)。
meteor.login("Ali", password: "********") { (result, error) in
if error != nil {
//Login Failed
}
else {
// do something after login
}
}
登出。
meteor.logout { (result, error) in
if error != nil {
//Logout Failed
}
else {
// do something after logout
}
}
断开 MeteorDDP 连接。
meteor.disconnect()
订阅
订阅和取消订阅集合的子集
meteor.subscribe("todos", [1,2,3,4]) {
// Do something when the todos subscription is ready
}
meteor.unsubscribe("SubID") {
// Unsubscribed of SubID
}
meteor.unsubscribe(withName: "todos") {
// Unsubscribed from todos
}
meteor.unsubscribeAll {
// Unsubscribed to all
}
通过订阅管理器进行管理
// First subscribe with name
meteorCollection.subscribe("todos", params: nil) { events, document in
// Do something when the todos subscription is ready
}
// Listen to collection changes
meteorCollection.collectionDidChange = { collection in
// collection.documents new or changed dataset
}
// Add observer to some event, like dataAdded, dataChange or dataRemove
meteor.addEventObserver("todos", event: .dataAdded) {
guard let value = $0 as? MeteorDocument else {
return
}
// value.id is the unique ID of MeteorDocument
self.documents[value.id] = value.fields
}
// Optionally remove events registered against subscription
meteor.removeEventObservers("todos", event: [.dataAdded, .dataChange, .dataRemove])
// Unsubscribe
meteorCollection.unsubscribe("todos")
从客户端更新集合
// operation could be insert, remove or update against given meteor document
// keyValue is the update request to meteor server against that collection
meteor.updateColection("todos", type: .update, documents: [["_id": "MeteorDocumentID"],["$set": keyValue]]) { (res, error) in
if error == nil {
// Successfully updated
}
}
调用一个方法
meteor.call("tasks.insert", params: [textField.text!]) { (res, error) in
if error == nil && res != nil {
// do something with the response
}
}
观察者
委托
MeteorDelegate 能够通过以下描述的单个方法接收更新
meteor.delegate = self
func didReceive(name: MeteorEvents, event: Any) {
switch name {
case .method:
// event -> MeteorMethod object
case .websocket:
// event -> WebSocketEvent, could be connected, disconnected, text, error
case .dataAdded:
// event -> MeteorDocument object
case .dataChange:
// event -> MeteorDocument object
case .dataRemove:
// event -> MeteorDocument object
}
}
回调
在您的代码中任何地方简单地添加指定的观察者!
// Event could be any as mentioned in MeteorDelegate
meteor.addEventObserver("todos", event: .dataAdded) {
// $0 is the data against that event
}
MeteorEncodable
以下帮助方法用于编码或解码对象
MeteorEncodable.encode
MeteorEncodable.decode
MeteorOAuth
MeteorOAuthViewController
// MeteorLoginService are twitter, github, google, facebook
// MeteorOAuthViewController is to present MeteorOAuth
#贡献 & 许可协议
MeteorDDP
遵循 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。
欢迎提交补丁请求!最好的贡献将包括针对在典型应用程序生命周期中阻塞主线程的类/方法的替换或配置。
请关注并支持我们 Meteor Pakistan 在 facebook
如果您在应用程序中使用 MeteorDDP
,请通过电子邮件联系 Engr. Ahsan Ali