![]() |
帮助列表 |
libMQTTKit-dms.a
Build Settings 中的 Build Options 里的 Debug Information Format 改为 DWARF
#import "DMS.h"
DMS * _client; NSString* clientId = @"IOSTester"; _client = [DMS newWithClientId:clientId];
//接受消息的处理代码块
[_client setMessageHandler:^(MQTTMessage *message) {
NSLog(@"%@",message.payloadString);
}];
//断开连接的处理代码块
[_client setDisconnectionHandler:^(NSUInteger code) {
NSLog(@"disconnected");
}];NSString* address = @"mqttdms.aodianyun.com";
NSString* pubkey = @demo";
NSString* subkey = @"demo";
int result = [_client connectToHost:address
withPubKey:pubkey
subKey:subkey
completionHandler:^(MQTTConnectionReturnCode code) {
if (code == ConnectionAccepted) {
NSLog(@"connected");
} else {
dispatch_async(dispatch_get_main_queue(), ^{
//连接失败后需要断开
[_client disconnectWithCompletionHandler:nil];
NSLog(@"connection refused");
});
}
}];
if(result != DMS_SUCCESS) {
NSLog(@"connection failed");
}NSString* topic = @"test";
[_client subscribe:topic
withCompletionHandler:^(NSArray *grantedQos) {
NSLog(@"%@",[[NSString alloc] initWithFormat:@"%@%@", @"subscribed to topic ",topic]);
}];NSString* payload = @"test message";
NSString* topic = @"test";
[_client publishString:payload
toTopic:topic
completionHandler:^(int mid) {
NSLog(@"published");
}];NSString* topic = @"test";
[_client unsubscribe:topic
withCompletionHandler:^{
NSLog(@"%@",[[NSString alloc] initWithFormat:@"%@%@", @"unsubscribed to topic ",topic]);
}];//当不再需要DMS对象时,务必要断开连接
[_client disconnectWithCompletionHandler:^(NSUInteger code) {
NSLog(@"disconnected");
}];//释放对象前,如果存在连接,请先断开连接,并等待操作完成 //当DMS对象的引用计数为0时,对象将被释放 //以上为开启ARC情况下的示例代码
在线QQ
3076938759
售前咨询400-663-6063
售后服务400-663-6063