您的位置:首页 > 编程语言 > Qt开发

MQTT连接服务器发送和接收数据

2015-10-14 11:41 375 查看
这里使用MQTTClient.framework这个第三方框架。

在工程中导入MQTTClient.framework这个框架,见附件。

.h文件

#import <UIKit/UIKit.h>

#import <MQTTClient/MQTTClient.h>

#import <MQTTClient/MQTTSessionManager.h>

@interface ViewController : UIViewController <MQTTSessionManagerDelegate, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@end

.m文件

#import "ViewController.h"

#import "ChatCell.h"

@interface ViewController ()

/*

 * MQTTClient: keep a strong reference to your MQTTSessionManager here

 */

@property (strong, nonatomic) MQTTSessionManager *manager;

@property (strong, nonatomic) NSDictionary *mqttSettings;

@property (strong, nonatomic) NSMutableArray *chat;

@property (weak, nonatomic) IBOutlet UILabel *status;

@property (weak, nonatomic) IBOutlet UITextField *message;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSString *base;

@property (weak, nonatomic) IBOutlet UIButton *connect;

@property (weak, nonatomic) IBOutlet UIButton *disconnect;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];

    NSURL *mqttPlistUrl = [bundleURL URLByAppendingPathComponent:@"mqtt.plist"];

    self.mqttSettings = [NSDictionary dictionaryWithContentsOfURL:mqttPlistUrl];

    self.base = self.mqttSettings[@"base"];

    

    self.chat = [[NSMutableArray alloc] init];

    

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    self.tableView.estimatedRowHeight = 150;

    self.tableView.rowHeight = UITableViewAutomaticDimension;

    

    self.message.delegate = self;

    

    

    /*

     * MQTTClient: create an instance of MQTTSessionManager once and connect

     * will is set to let the broker indicate to other subscribers if the connection is lost

     */

    if (!self.manager) {

        self.manager = [[MQTTSessionManager alloc] init];

        self.manager.delegate = self;

        self.manager.subscriptions = [[NSMutableDictionary alloc] init];

//        [self.manager.subscriptions setObject:[NSNumber numberWithInt:MQTTQosLevelExactlyOnce]

//                                       forKey:[NSString stringWithFormat:@"%@/#", self.base]];

        [self.manager.subscriptions setValue:[NSNumber numberWithInt:MQTTQosLevelExactlyOnce] forKey:[NSString stringWithFormat:@"%@/#", self.base]];

        [self.manager connectTo:self.mqttSettings[@"host"] //主机号

                           port:[self.mqttSettings[@"port"] intValue]//端口号

                            tls:[self.mqttSettings[@"tls"] boolValue]

                      keepalive:60

                          clean:true

                           auth:false

                           user:nil

                           pass:nil

                      willTopic:[NSString stringWithFormat:@"%@/%@",//topic

                                 self.base,

                                 [UIDevice currentDevice].name]

                           will:[@"offline" dataUsingEncoding:NSUTF8StringEncoding]

                        willQos:MQTTQosLevelExactlyOnce

                 willRetainFlag:FALSE

                   withClientId: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];

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

}

- (void)viewWillDisappear:(BOOL)animated {

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    switch (self.manager.state) {

        case MQTTSessionManagerStateClosed:

            self.status.text = @"closed";

            self.disconnect.enabled = false;

            self.connect.enabled = false;

            break;

        case MQTTSessionManagerStateClosing:

            self.status.text = @"closing";

            self.disconnect.enabled = false;

            self.connect.enabled = false;

            break;

        case MQTTSessionManagerStateConnected:

            self.status.text = [NSString stringWithFormat:@"connected as %@-%@",

                                [UIDevice currentDevice].name,

                                self.tabBarItem.title];

            self.disconnect.enabled = true;

            self.connect.enabled = false;

//            [self.manager sendData:[@"joins chat" dataUsingEncoding:NSUTF8StringEncoding]

//                             topic:[NSString stringWithFormat:@"%@/%@",

//                                    self.base,

//                                    [UIDevice currentDevice].name]

//                               qos:MQTTQosLevelExactlyOnce

//                            retain:FALSE];

            break;

        case MQTTSessionManagerStateConnecting:

            self.status.text = @"connecting";

            self.disconnect.enabled = false;

            self.connect.enabled = false;

            break;

        case MQTTSessionManagerStateError:

            self.status.text = @"error";

            self.disconnect.enabled = false;

            self.connect.enabled = false;

            break;

        case MQTTSessionManagerStateStarting:

        default:

            self.status.text = @"not connected";

            self.disconnect.enabled = false;

            self.connect.enabled = true;

            break;

    }

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;

}

- (IBAction)clear:(id)sender {

    [s
9e05
elf.chat removeAllObjects];

    [self.tableView reloadData];

}

- (IBAction)connect:(id)sender {

    /*

     * MQTTClient: connect to same broker again

     */

    

    [self.manager connectToLast];

}

- (IBAction)disconnect:(id)sender {

    /*

     * MQTTClient: send goodby message and gracefully disconnect

     */

//    [self.manager sendData:[@"leaves chat" dataUsingEncoding:NSUTF8StringEncoding]

//                     topic:[NSString stringWithFormat:@"%@/%@",

//                            self.base,

//                            [UIDevice currentDevice].name]

//                       qos:MQTTQosLevelExactlyOnce

//                    retain:FALSE];

//    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];

    [self.manager disconnect];

}

- (IBAction)send:(id)sender {

    /*

     * MQTTClient: send data to broker

     */

    

    [self.manager sendData:[self.message.text dataUsingEncoding:NSUTF8StringEncoding]

                     topic:[NSString stringWithFormat:@"%@/%@",

                            self.base,

                            [UIDevice currentDevice].name]

                       qos:MQTTQosLevelExactlyOnce

                    retain:FALSE];

    

}

/*

 * MQTTSessionManagerDelegate

 */

- (void)handleMessage:(NSData *)data onTopic:(NSString *)topic retained:(BOOL)retained {

    /*

     * MQTTClient: process received message

     */

    //接收和发送数据

    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSString *senderString = [topic substringFromIndex:self.base.length + 1];

    

    [self.chat insertObject:[NSString stringWithFormat:@"%@:\n%@", senderString, dataString] atIndex:0];

    [self.tableView reloadData];

}

/*

 * UITableViewDelegate

 */

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    return NO;

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    return NO;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ChatCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"line"];

    cell.textView.text = self.chat[indexPath.row];

    return cell;

}

/*

 * UITableViewDataSource

 */

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.chat.count;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MQTT 框架 服务器 数据