MQTT
MQTT是一个基于客户端服务器的消息发布/订阅传输协议。MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛。在很多情况下,包括受限的环境中,如:机器与机器(M2M)通信和物联网(IoT)。其在,通过卫星链路通信传感器、偶尔拨号的医疗设备、智能家居、及一些小型化设备中已广泛使用。
证书配置
双向认证方法:让后台生成 ca.crt 和 client.p12文件(client.p12文件由client.crt和client.key合成) 我使用的是自签证书,在本地安装好openssl环境后。
1.使用命令行把crt转化为der格式
openssl x509 -in ca.crt -out ca.der -outform der
一般情况下后台都装好openssl环境 直接转换就可以了。
2.创建MQTT
MQTTSSLSecurityPolicyTransport *transport = [[MQTTSSLSecurityPolicyTransport alloc]init];
transport.host = d[@"192.168.1.ip"];
transport.port = [d[@"port"] intValue];
transport.tls = YES;
NSString* ca = [[NSBundle bundleForClass:[MQTTSession class]] pathForResource:@"ca" ofType:@"der"];
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.sessionManager = [[MQTTSessionManager alloc]init];
NSDictionary *dic = @{@"subject":@"disconnect"};
NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
[self.sessionManager connectTo:d[@"192.168.1.ip"]
port: [d[@"port"] intValue]
tls:YES
keepalive:60//设置心跳间隔
clean:NO //clean标志位向代理服务器表明客户端是否要建立持续的会话
auth:YES //MQTT允许发送用户名和密码以便验证客户端的身份和进行授权
user:@"username"
pass:@"password"
will:YES //是否设遗嘱 是的话需要填写willTopic和willMsg
willTopic:@"/topic/lastwill" //遗嘱消息主题
willMsg:data //遗嘱消息设置
willQos:0 //MQTT设计了一套保证消息稳定传输的机制,包括消息应答、存储和重传。在这套机制下,提供了三种不同层次QoS(Quality of Service):QoS0,At most once,至多一次;QoS1,At least once,至少一次;QoS2,Exactly once,确保只有一次。
willRetainFlag:NO
withClientId: @"clientid" //clientId 客户端标识符
securityPolicy:securityPolicy
certificates:
[MQTTSSLSecurityPolicyTransport clientCertsFromP12:client passphrase:@"password"]];//配置证书的时候设置的密码
// 添加监听状态观察者
self.sessionManager.delegate = self;
[self.sessionManager addObserver:self
forKeyPath:@"state"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:nil];
//订阅topic 可设置多个主题@{@"topic": @2, @"topic2":@2}
self.sessionManager.subscriptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:MQTTQosLevelExactlyOnce] forKey:@"/topic/mqtt"];
3.发送和接收数据
发送消息
[self. sessionManager sendData:[self.LocationStr dataUsingEncoding:NSUTF8StringEncoding]
topic:@"topic"
qos:2
retain:true];
收到消息
- (void)handleMessage:(NSData *)data onTopic:(NSString *)topic retained:(BOOL)retained {
}
MQTT状态监控
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
switch (self.manager.state) {
case MQTTSessionManagerStateClosed:
self.NetStatus = @"closed";
NSLog(@"MQTT已关闭");
break;
case MQTTSessionManagerStateClosing:
self.NetStatus = @"closing";
NSLog(@"MQTT关闭中");
break;
case MQTTSessionManagerStateConnected:
self.NetStatus = @"已连接";
NSLog(@"MQTT已链接");
break;
case MQTTSessionManagerStateConnecting:
self.NetStatus = @"connecting";
NSLog(@"MQTT正在连接");
break;
case MQTTSessionManagerStateError:
self.NetStatus = @"error";
NSLog(@"MQTT错误");
break;
case MQTTSessionManagerStateStarting:
default:
self.NetStatus = @"not connected";
NSLog(@"MQTT未链接");
break;
}
}