Git Product home page Git Product logo

cocoaredis's Introduction

Supports ios and macosx

This code is RedisKit without cocoa version uploaded to the cocoa library.

🎉 Congrats

🚀 CocoaRedis (1.0.1) successfully published 📅 July 10th, 19:48 🌎 https://cocoapods.org/pods/CocoaRedis 👍 Tell your friends!

use

target 'MyApp' do
  pod 'CocoaRedis', '~> 1.0.2'
end

More use please see: https://github.com/dizzus/RedisKit

swift的使用如下:

import UIKit
import CocoaRedis
import HandyJSON
import SwiftEventBus

class RedisClient: NSObject {
    
    // 初始化Redis
    private var redis = CocoaRedis()
    // 外部访问的设备id, 用于所有数据的订阅功能
    var deviceId = "0000"
    //  监听数据
    var observer: NSObjectProtocol?
    
    /**
     配置Config文件
     */
    func config(){
        /// 判断代码必须不能为空,否则会造成代码错误
        if redis != nil {
            redis!.connect(withHost: REDIS_HOST, port: Int32(REDIS_PORT))
            // 连接的代码
                .then { [self] _ in
                    return redis!.auth(REDIS_USERNAME+":"+REDIS_PASSWORD)
                }
            // 在此订阅
                .then{ [self] _ in
                    RedisLogger.log("===Redis connected===")
                    let channels1 = deviceId+"-telemetry"
                    let channels2 = deviceId+"-status"
                    let channels3 = deviceId+"-version"
                    return redis!.subscribeChannels([channels1, channels2, channels3])
                }
            // 订阅接收到通知数据
                .then { [self] _ in
                    let center = NotificationCenter.default
                    observer = center.addObserver(forName: NSNotification.Name.CocoaRedisMessage,
                                                  object: nil,
                                                  queue: nil) { notification in
                        // Get published message from the notification object.
                        if let obj = notification.userInfo as? [String: Any] {
                            let channel = obj["channel"] as! String
                            let message = obj["message"] as! String
                            //print(obj["channel"] as Any)
                            //print(obj["message"] as Any)
                            
                            if channel == "\(self.deviceId)-telemetry" {
                                if let telemetry = JSONDeserializer<TelemetryModel>.deserializeFrom(json: (message)) {
                                    SwiftEventBus.post(REDIS_TELEMETRY, sender: telemetry)
                                }
                            }else if channel == "\(self.deviceId)-status" {
                                if let status = JSONDeserializer<StatusNModel>.deserializeFrom(json: (message)) {
                                    SwiftEventBus.post(REDIS_STATUS, sender: status)
                                }
                            }else if channel == "\(self.deviceId)-version" {
                                if let version = JSONDeserializer<VersionModel>.deserializeFrom(json: (message)) {
                                    SwiftEventBus.post(REDIS_VERSION, sender: version)
                                }
                                
                                
                            }
                        }
                    }
                    return nil
                }
        }
    }
    
    /**
     取消通知订阅 此代码作用与Redis的代码取消订阅
     @author ardawang
     date 2024/7/9
     */
    func unsub(){
        if redis?.isConnected != nil {
            let channels1 = deviceId+"-telemetry"
            let channels2 = deviceId+"-status"
            let channels3 = deviceId+"-version"
            redis!.unsubscribe([channels1, channels2, channels3])
                .then { [self] _ in
                    NotificationCenter.default.removeObserver(observer as Any)
                    return redis!.quit()
                }
        }
    }
}


cocoaredis's People

Contributors

ardwang avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.