iOS MQTT 设置遗嘱

Will Flag 作用

简而言之,就是客户端预先定义好,在自己异常断开的情况下,所留下的最后遗愿(Last Will),也称之为遗嘱(Testament)。这个遗嘱就是一个由客户端预先定义好的主题和对应消息,附加在CONNECT的可变报文头部中,在客户端连接出现异常的情况下,由服务器主动发布此消息。

设置遗嘱注意点:

当Will Flag位为1时,Will QoS和Will Retain才会被读取,此时消息体中要出现Will Topic和Will Message具体内容,否则Will QoS和Will Retain值会被忽略掉。

当Will Flag位为0时,则Will Qos和Will Retain无效。

- (void)setSessionWillTopic:(NSString *)onlineStatus{
    
    NSMutableDictionary *willDic = [[NSMutableDictionary alloc] init];
    NSString *user_id = [UserInfo restore].userid;
    NSString *phone_type =[UIDevice getIdentifer];
    NSString *phone_sys =[[UIDevice currentDevice] systemVersion];
    NSString *app_version =[STEnvironmentManager sharedInstance].currentEnvirontmentVersion;
    [willDic setObject:onlineStatus forKey:@"status"];
    [willDic setObject:user_id forKey:@"userId"];
    [willDic setObject:@"”iOS" forKey:@"channel"];
    [willDic setObject:phone_type forKey:@"phoneType"];//手机型号
    [willDic setObject:phone_sys forKey:@"phoneSys"];//手机系统
    [willDic setObject:app_version forKey:@"appVersion"];//当前版本号
    NSData *willData = [NSJSONSerialization dataWithJSONObject:willDic options:NSJSONWritingPrettyPrinted error:nil];
    //   1. 当Will Flag位为1时,Will QoS和Will Retain才会被读取,此时消息体中要出现Will Topic和Will Message具体内容,否则Will QoS和Will Retain值会被忽略掉。
    //   2. 当Will Flag位为0时,则Will Qos和Will Retain无效。
    _session.willFlag = 1;
    _session.willRetainFlag = true;
    _session.willQoS = 1;
    _session.willTopic = [NSString  stringWithFormat:@"%@90000/%@",kFlycoPublishTopicPath,[[[UIDevice currentDevice] identifierForVendor] UUIDString]];
    
//    79F89CCB-6FDF-41EB-A2A3-AF210B5D1D34
    _session.willMsg =willData;
            
}

你可能感兴趣的:(iOS MQTT 设置遗嘱)