gpt4 book ai didi

ios - 在 MQTTKit 中使用相同客户端 ID 初始化新客户端时无法接收离线消息

转载 作者:行者123 更新时间:2023-11-29 02:37:25 31 4
gpt4 key购买 nike

目前,当 MQTTClient 对象使用相同的客户端 ID 重新初始化然后连接到代理时,似乎不会保留 session 。

考虑以下代码:

//Just a tiny wrapper around MQTTClient for custom methods
@property (nonatomic, strong) RAMQTTClient *client;

@implementation MQTTViewController

- (void)viewDidLoad
{
[super viewDidLoad];

//The selector gets called whenever a new message is received
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:@"didReceiveMessage" object:nil];

//Initialises an MQTT client with QOS=1 and cleanSession=NO
_client = [[RAMQTTClient alloc]initWithId:@"client-1"];
}

-(void)didReceiveMessage:(NSNotification *)notification {
NSString *data = (NSString *)notification.object;
NSLog(@"Received: %@", data);
}

// Use a switch action to subscribe and disconnect
- (IBAction)switchUpdated:(id)sender {
BOOL on = [sender isOn];
if (on) {
[_client listenToTopic:@"a~1"];
} else {
[_client disconnect];
}
}

@end

只要 _client 对象没有被破坏,断开连接和重新连接确实会检索在该时间间隔内发送的离线消息。但是,如果客户端重新初始化然后开始收听,我就不会收到任何离线消息。

下面是RAMQTTClient的实现:

typedef void (^ConnectionCompletionHandler)(MQTTConnectionReturnCode code);

@interface RAMQTTClient()
@property (strong, nonatomic) MQTTClient *client;
@property (nonatomic) MQTTConnectionReturnCode connectionCode;
@end

@implementation RAMQTTClient

-(instancetype)initWithId:(NSString*)id {
if (self = [super init]) {
_client = [[MQTTClient alloc]initWithClientId:id cleanSession:NO];
}
return self;
}

-(BOOL)isRunning {
return _client.connected;
}

-(NSString *)id {
return _client.clientID;
}

-(void)connectWithCompletionHandler:(ConnectionCompletionHandler)handler {
if (!_client.connected) {
[_client connectToHost:@"localhost" completionHandler:handler];
}
}

-(void)listenToTopic:(NSString *)topic {
ConnectionCompletionHandler completionHandler = ^(MQTTConnectionReturnCode code) {
NSLog(@"RAMQTTClient: %@ connected", _client.clientID);
_connectionCode = code;
if (code == ConnectionAccepted) {
[_client subscribe:topic withQos:AtLeastOnce completionHandler:^(NSArray *grantedQos) {
NSLog(@"RAMQTTClient: Listening to %@", topic);
[_client setMessageHandler:^(MQTTMessage *message) {
NSLog(@"Message => %@",message.payloadString);
[[NSNotificationCenter defaultCenter]postNotificationName:@"didReceiveMessage"
object:message.payloadString];
}];
}];
}
};
[self connectWithCompletionHandler:completionHandler];
}

-(void)disconnect {
if (_client.connected) {
[_client disconnectWithCompletionHandler:^(NSUInteger code) {
NSLog(@"RAMQTTClient: %@ Disconnected with code %u", _client.clientID, code);
}];
}
}

@end

你们以前遇到过这种问题吗?有解决办法吗?

最佳答案

对于那些感兴趣的人,我已经弄清楚了。问题是在订阅主题后设置消息处理程序。由于离线消息在您订阅时立即到达,因此客户端无法在接收消息之前等待或设置消息处理程序。因此,即使消息到达,它们也不会发送到处理程序。在订阅 channel 之前设置消息处理程序可以解决此问题。

[_client setMessageHandler:^(MQTTMessage *message) {
NSLog(@"Message => %@",message.payloadString);
[[NSNotificationCenter defaultCenter]postNotificationName:@"didReceiveMessage"
object:message.payloadString];
}];
[_client subscribe:topic withQos:AtLeastOnce completionHandler:^(NSArray *grantedQos) {
NSLog(@"RAMQTTClient: Listening to %@", topic);
}];

关于ios - 在 MQTTKit 中使用相同客户端 ID 初始化新客户端时无法接收离线消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26186360/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com