MQTT相关

MQTTClient相关

可通过CocoaPod MQTTClient

创建一个新客户端并连接到服务器

    MQTTSession *session = [[MQTTSession alloc] initWithClientId:@"clientId"];
    session.delegate = self;
    [session connectToHost:@"" port:1883 usingSSL:NO];  

订阅主题并且设置消息等级

[session subscribeToTopic:@"topic" atLevel:MQTTQosLevelExactlyOnce];
    

发布一条消息到对应主题

//[NSData data]为对应的消息数据,topic为对应的主题
[session publishAndWaitData:[NSData data] onTopic:@"topic" retain:NO qos:MQTTQosLevelExactlyOnce];

MQTTSession

属性

id< MQTTSessionDelegate >   delegate
 
MQTTPersistence *   persistence
 
void(^  connectionHandler )(MQTTSessionEvent event)
 
void(^  messageHandler )(NSData *message, NSString *topic)
 
MQTTSessionStatus   status
 
NSString *  clientId
 
NSString *  userName
 
NSString *  password
//保持活动的以秒为单位的时间间隔,
//客户端确保控制发布的数据包之间的时间间隔不超过存活时间
UInt16  keepAliveInterval
//指定服务器是否清楚之前的会话,false是持久对话true则相反。
BOOL    cleanSessionFlag
//遗嘱,如果是YES的话,消息会被发送到服务器 
BOOL    willFlag
//如果willFlag为YES,则为字符串,否则为nil
NSString *  willTopic
//同上
NSData *    willMsg
//指定消息质量级别,0暂时还没用到,从1和2中选择。 
MQTTQosLevel    willQoS
//表明如果服务器应该发布willmsg的状态。如果willflag为NO
//该处也为NO,如果willFlag为YES,该处为NO的话,服务器发布willMsg必须为非保留状态。
BOOL    willRetainFlag
//协议等级 
UInt8   protocolLevel
//如果为nil,默认为[NSRunLoop currentRunLoop] 
NSRunLoop *     runLoop
//默认为NSRunLoopCommonModes 
NSString *  runLoopMode
//评估使用的安全策略服务器信任安全连接。 
MQTTSSLSecurityPolicy *     securityPolicy
//证书
NSArray *   certificates
 
MQTTMessage *   connectMessage

实例方法

初始化方法

(MQTTSession *)     - init
 
(MQTTSession *)     - initWithClientId:userName:password:keepAlive:cleanSession:will:willTopic:willMsg:willQoS:willRetainFlag:protocolLevel:runLoop:forMode:
 //增安全策略即SSL
(MQTTSession *)     - initWithClientId:userName:password:keepAlive:cleanSession:will:willTopic:willMsg:willQoS:willRetainFlag:protocolLevel:runLoop:forMode:securityPolicy:
//在安全策略基础上加证书
(MQTTSession *)     - initWithClientId:userName:password:keepAlive:cleanSession:will:willTopic:willMsg:willQoS:willRetainFlag:protocolLevel:runLoop:forMode:securityPolicy:certificates:
 
(id)    - initWithClientId:
 
(id)    - initWithClientId:runLoop:forMode:
 
(id)    - initWithClientId:userName:password:
 
(id)    - initWithClientId:userName:password:runLoop:forMode:
 
(id)    - initWithClientId:userName:password:keepAlive:cleanSession:
 
(id)    - initWithClientId:userName:password:keepAlive:cleanSession:runLoop:forMode:
 
(id)    - initWithClientId:userName:password:keepAlive:cleanSession:willTopic:willMsg:willQoS:willRetainFlag:
 
(id)    - initWithClientId:userName:password:keepAlive:cleanSession:willTopic:willMsg:willQoS:willRetainFlag:runLoop:forMode:
 
(id)    - initWithClientId:keepAlive:connectMessage:runLoop:forMode:

连接服务器

(void)  - connectToHost:port:usingSSL:
 
(void)  - connectToHost:port:
 
(void)  - connectToHost:port:withConnectionHandler:messageHandler:
 
(void)  - connectToHost:port:usingSSL:withConnectionHandler:messageHandler:
 
(BOOL)  - connectAndWaitToHost:port:usingSSL:

订阅主题

(UInt16)    - subscribeToTopic:atLevel:
 
(void)  - subscribeTopic:
 //是否成功订阅
(BOOL)  - subscribeAndWaitToTopic:atLevel:
 
(UInt16)    - subscribeToTopics:
 
(BOOL)  - subscribeAndWaitToTopics:

退订主题

(UInt16)    - unsubscribeTopic:
 
(BOOL)  - unsubscribeAndWaitTopic:
 
(UInt16)    - unsubscribeTopics:
 
(BOOL)  - unsubscribeAndWaitTopics:

发布消息

//发布的数据在指定的QoS级别给定主题和retain Flag
(UInt16)    - publishData:onTopic:retain:qos:
 
(void)  - publishData:onTopic:
 
(void)  - publishDataAtLeastOnce:onTopic:
 
(void)  - publishDataAtLeastOnce:onTopic:retain:
 
(void)  - publishDataAtMostOnce:onTopic:
 
(void)  - publishDataAtMostOnce:onTopic:retain:
 
(void)  - publishDataExactlyOnce:onTopic:
 
(void)  - publishDataExactlyOnce:onTopic:retain:
 //JSON数据转化为NSData然后发送。
(void)  - publishJson:onTopic:
 //在特定的QoS级别上发布特定主题同步数据并保留标志
(BOOL)  - publishAndWaitData:onTopic:retain:qos:

关闭连接

(void)  - close
//关闭,并且同步。
(void)  - closeAndWait

类方法

//读取一个PKCS12证书,并且将其转换为证书列阵的初始化方法
(NSArray *)     + clientCertsFromP12:passphrase:

//示例
NSString *path = [[NSBundle bundleForClass:[MQTTClientTests class]] pathForResource:@"filename"
ofType:@"p12"];
NSArray *myCerts = [MQTTSession clientCertsFromP12:path passphrase:@"passphrase"];
if (myCerts) {
self.session = [[MQTTSession alloc] initWithClientId:nil
   userName:nil
   password:nil
   keepAlive:60
   cleanSession:YES
   will:NO
   willTopic:nil
   willMsg:nil
   willQoS:0//0暂时没有用到的
   willRetainFlag:NO
   protocolLevel:4
   runLoop:[NSRunLoop currentRunLoop]
   forMode:NSRunLoopCommonModes
   securityPolicy:nil
   certificates:myCerts];
[self.session connectToHost:@"localhost" port:8884 usingSSL:YES];
}

MQTTSessionDelegate

实例方法

(void)  - newMessage:data:onTopic:qos:retained:mid:
 
(BOOL)  - newMessageWithFeedback:data:onTopic:qos:retained:mid:
 
(void)  - session:newMessage:onTopic:
 
(void)  - handleEvent:event:error:
 
(void)  - session:handleEvent:
 //建立连接胡调用
(void)  - connected:
 
(void)  - connected:sessionPresent:
 
(void)  - connectionRefused:error:
 
(void)  - connectionClosed:
 
(void)  - connectionError:error:
 
(void)  - protocolError:error:
 
(void)  - messageDelivered:msgID:
 
(void)  - subAckReceived:msgID:grantedQoss:
 
(void)  - unsubAckReceived:msgID:
 
(void)  - sending:type:qos:retained:duped:mid:data:
 
(void)  - received:type:qos:retained:duped:mid:data:
 
(BOOL)  - ignoreReceived:type:qos:retained:duped:mid:data:
 //客户端内部缓冲区的内容变化的时候调用用于监控完成发送和接收消息
(void)  - buffered:queued:flowingIn:flowingOut:
 //客户端内部缓冲区的内容变化的时候调用用于监控完成发送和接收消息
(void)  - buffered:flowingIn:flowingOut:

MQTTSessionManager

属性

id< MQTTSessionManagerDelegate >    delegate
 
NSDictionary *  subscriptions
 
MQTTSessionManagerState     state
 
NSError *   lastErrorCode

实例方法


(MQTTSessionManager *)  - initWithPersistence:maxWindowSize:maxMessages:maxSize:
 //重新连接
(void)  - connectToLast
 //发送数据
(UInt16)    - sendData:topic:qos:retain:
 //断开
(void)  - disconnect

(void)  - connectTo:port:tls:keepalive:clean:auth:user:pass:will:willTopic:willMsg:willQos:willRetainFlag:withClientId:securityPolicy:certificates:
 
(void)  - connectTo:port:tls:keepalive:clean:auth:user:pass:will:willTopic:willMsg:willQos:willRetainFlag:withClientId:
 
(void)  - connectTo:port:tls:keepalive:clean:auth:user:pass:willTopic:will:willQos:willRetainFlag:withClientId:
//host:指定主机名或IP地址来连接。默认为“localhost”。
//port:端口号
//TLS:是否使用SSL
//keepalive:保持活动时间,秒为单位
//clean:是否清楚之前会话
//auth:指定用户应该用于authenthication并传递参数
//will:遗嘱
//clientId:客户端Id
//securityPolicy:自定义的安全策略
//certificates:证书

MQTTSessionMannagerDelegate

实例方法

//收到新消息的时候
(void)  - handleMessage:onTopic:retained:
//消息递送
(void)  - messageDelivered:

你可能感兴趣的:(MQTT相关)