(iOS)MQTT连接 遗嘱 双向、单向认证2

2021-01-12  本文已影响0人  小船_d15e

转过的文章代码格式有点乱,具体看的demo就就行,有空再整理,上我自己的demo, 只看这篇就可以了,常见的都能用到了

MQTT

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分。该协议支持所有平台,几乎可以把所有联网物品和外部连接起来,被用来当做传感器和制动器(比如通过Twitter让房屋联网)的通信协议

MQTT特点

MQTT协议是为大量计算能力有限,且工作在低带宽、不可靠的网络的远程传感器和控制设备通讯而设计的协议,它具有以下主要的几项特性:

1. 使用发布/订阅消息模式,提供一对多的消息发布,解除应用程序耦合;

2. 对负载内容屏蔽的消息传输;

3. 使用 TCP/IP 提供网络连接;

4. 有三种消息发布服务质量:

5. 小型传输,开销很小(固定长度的头部是 2 字节),协议交换最小化,以降低网络流量;

6. 使用 Last Will 和 Testament 特性通知有关各方客户端异常中断的机制;

好了废话不多说,直接上干货

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">双向认证方法:让后台生成 ca.crt 和 client.p12文件(client.p12文件由client.crt和client.key合成)
单项认证方法:让后台生成 ca.crt</pre>

1.使用命令行把crt转化为der格式

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">openssl x509 -in ca.crt -out ca.der -outform der</pre>

2.建立连接

[ 复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">if (!self.manager) {
MQTTSSLSecurityPolicyTransport transport = [[MQTTSSLSecurityPolicyTransport alloc]init];
transport.host = @"xxxx.com";
transport.port = 8883;
transport.tls = YES;
NSString
ca = [[NSBundle bundleForClass:[MQTTSession class]] pathForResource:@"ca" ofType:@"der"]; ////TODO:双向认证需加入client证书
// NSString* client = [[NSBundle bundleForClass:[MQTTSession class]] pathForResource:@"certificate" ofType:@"p12"]; // transport.certificates = [MQTTSSLSecurityPolicyTransport clientCertsFromP12:client passphrase:@"password"];
MQTTSSLSecurityPolicy *securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeCertificate];
securityPolicy.allowInvalidCertificates = YES;
securityPolicy.validatesDomainName = NO;
securityPolicy.validatesCertificateChain = NO;
securityPolicy.pinnedCertificates = @[[NSData dataWithContentsOfFile:ca]];
transport.securityPolicy = securityPolicy;

    self.manager = [[MQTTSessionManager alloc] init];
    self.manager.delegate = self; ////不使用证书
// [self.manager connectTo:@"xxxxx.com" // port:1883 // tls:NO // keepalive:60 //心跳间隔不得大于120s // clean:true // auth:true // user:@"username" // pass:@"password" // will:false // willTopic:nil // willMsg:nil // willQos:0 // willRetainFlag:FALSE // withClientId:@"xxxxxxxxxxxxx"];
    ////使用证书(这里采用单项认证,双向认证只需把certificates:参数设置为transport.certificates即可)
    [self.manager connectTo:@"xxxxx.com" port:8883 tls:YES
                  keepalive:60 clean:true auth:YES
                       user:@"username" pass:@"password" will:false willTopic:nil
                    willMsg:nil
                    willQos:0 willRetainFlag:FALSE
               withClientId:@"xxxxxxxxx" securityPolicy:securityPolicy
               certificates:nil];
} else {
    [self.manager connectToLast];
} /* MQTTCLient: observe the MQTTSessionManager's state to display the connection status */ [self.manager addObserver:self
               forKeyPath:@"state" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                  context:nil];</pre>
[ 复制代码

](javascript:void(0); "复制代码")

参数解释:

3.服务器返回数据

状态信息:

[ 复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { switch (self.manager.state) { case MQTTSessionManagerStateClosed:
NSLog(@"MQTTSessionManagerStateClosed"); break; case MQTTSessionManagerStateClosing:
NSLog(@"MQTTSessionManagerStateClosing"); break; case MQTTSessionManagerStateConnected:
NSLog(@"MQTTSessionManagerStateConnected"); //连接成功订阅
for (int i=0; i<3; i++) {
[self.manager subscribeToTopic:[NSString stringWithFormat:@"%d%d%d%d%d%d%d%d",i,i,i,i,i,i,i,i] atLevel:1];
} break; case MQTTSessionManagerStateConnecting:
NSLog(@"MQTTSessionManagerStateConnecting"); break; case MQTTSessionManagerStateError:
NSLog(@"MQTTSessionManagerStateError"); break; case MQTTSessionManagerStateStarting:
NSLog(@"MQTTSessionManagerStateStarting"); default:
NSLog(@"default"); break;
}
}</pre>

[ 复制代码

](javascript:void(0); "复制代码")

数据信息:

[ 复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">- (void)handleMessage:(NSData *)data onTopic:(NSString *)topic retained:(BOOL)retained {
NSLog(@"------------->>%@",topic);

NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",dataString);

}</pre>

[ 复制代码

](javascript:void(0); "复制代码")

4.连接(断开后的再次连接):

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">- (IBAction)connect:(id)sender {
[self.manager connectToLast];
}</pre>

5.订阅

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">- (IBAction)sub:(id)sender {
[self.manager subscribeToTopic:@"55555555555555555" atLevel:1];
}</pre>

6.取消订阅

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">- (IBAction)unsub:(id)sender {
[self.manager unsubscribeTopic:@"55555555555555555"];
}</pre>

7.断开连接

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">- (IBAction)disconnect:(id)sender {
[self.manager disconnect];</pre>

说明:MQTTSessionManager是没有subscribeToTopic:atLevel:和unsubscribeTopic:方法的,只有subscriptions。为了更灵活使用,所以需要自己添加

1. 在.h中添加:

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">//订阅

2. 在.m中添加

[ 复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important; white-space: pre-wrap;">//订阅

[ 复制代码

](javascript:void(0); "复制代码")

项目地址

https://github.com/wangbaolei/Mqtt-Client.git

上一篇 下一篇

猜你喜欢

热点阅读