objective-c – 在Swift for PubNub 4.0中建立回调以接收消息
发布时间:2020-12-16 09:58:31 所属栏目:百科 来源:网络整理
导读:在我看来,PubNub在 Swift中开始使用的文档不适用于早于PubNub 4.0的版本.我无法成功建立回调以注册PubNub. 我的代码: class Communicator: NSObject,PNObjectEventListener { var pubNubClient: PubNub override init(){ let config = PNConfiguration( pub
在我看来,PubNub在
Swift中开始使用的文档不适用于早于PubNub 4.0的版本.我无法成功建立回调以注册PubNub.
我的代码: class Communicator: NSObject,PNObjectEventListener { var pubNubClient: PubNub override init(){ let config = PNConfiguration( publishKey: "my_publish_key",subscribeKey: "my_subscribe_key" ) pubNubClient = PubNub.clientWithConfiguration(config); super.init() pubNubClient.addListener(self) pubNubClient.subscribeToChannels(["my_channel"],withPresence: false) } func didReceiveMessage(client: PubNub!,message: PNMessageResult!){ /* THIS METHOD NEVER GETS REACHED */ } } 深入研究一下PubNub源代码,这个区域似乎有问题: - (void)addListener:(id <PNObjectEventListener>)listener { dispatch_async(self.resourceAccessQueue,^{ if ([listener respondsToSelector:@selector(client:didReceiveMessage:)]) { /* this block is never reached!!! */ [self.messageListeners addObject:listener]; } /* Remaining Lines Stripped Away */ }); } 我还是相对较新的Swift并与Objective C集成.我很好奇是否有respondsToSelector的问题,因为Objective C代码引用了Swift代码. 消息肯定会过去; PubNub库中有另一个较低级别的函数,它记录了收到的所有消息. 任何帮助将非常感激. 解决方法
4.0之前的版本已被弃用,并且不会完全按照以前的方式工作.
我建议完全迁移到最新的(4.0)SDK,新的iOS SDK已经消除了很多膨胀并且编译速度更快.要开始查看this tutorial. 总而言之,实例化PubNub客户端看起来如下: let config = PNConfiguration( publishKey: "Your_Pub_Key",subscribeKey: "Your_Sub_Key") client = PubNub.clientWithConfiguration(config) client?.addListener(self) client?.subscribeToChannels(["Your_Channel"],withPresence: false) 新的didReceiveMessage函数如下所示: func client(client: PubNub!,didReceiveMessage message: PNMessageResult!,withStatus status: PNErrorStatus!) { //Do Something like //println(message) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |