我有问题让PubNub的订阅消息处理程序触发。我正在开发一个Web客户端,用于监听来自移动应用的消息。直到最近,这段代码运行良好。我可以通过手机发送消息并查看网络应用程序自动更新。但在过去的几天里,网络应用程序不再更新。PubNub订阅消息回调not firing
这是一个我在CoffeeScript中编写的Angular应用程序。我有一个MessageService
可以处理PubNub的所有引导。我服务的subscribe
方法通过实体id
arg设置为要监听的频道名称,并通过messageHandler
参数传递函数引用。
angular.module('exampleApp').service 'MessageService', ($http, $interval) ->
pubnub = null
subscribePromise = null
config =
subscribe_key: 'demo'
# Sanity check. This gets triggered upon connection with the correct
# channel name/entity id.
connectionHandler = ->
_.forOwn arguments, (arg) -> console.log arg
return {
getChats: (id) ->
# Calls an API to fetch all of the chat messages. These aren't transmitted over
# PubNub because we do other fun things to adhere to HIPAA compliance.
return $http.get 'path/to/api/endpoint/' + id
subscribe: (id, messageHandler) ->
pubnub = pubnub or PUBNUB.init config
pubnub.subscribe({
channel: id
message: (data) ->
if not not subscribePromise
$interval.cancel subscribePromise
subscribePromise = null
messageHandler data
connect: connectionHandler
})
# Interval-based workaround to function in spite of PubNub issue
subscribePromise = $interval messageHandler, 10000
}
下面是我的一个控制器中的messageHandler
实现示例。
angular.module('exampleApp').controller 'MessageCtrl', (MessageService) ->
$scope.messageId = 'some entity id'
# This message handler never gets fired, despite passing it to pubnub.subscribe
onMessageUpdated = (data) ->
console.log data
MessageService.getChats($scope.messageId).then (messages) -> $scope.messages = messages
MessageService.subscribe $scope.messageId, onMessageUpdated
就像我前面提到的,这段代码在不久之前就开始工作了,但是出乎意料的是,消息处理程序完全停止了开火。一个多月没有触及它。推动我坚果的事情是,我可以在PubNub中打开开发控制台并观看来自电话的消息,但由于某种原因,该消息处理程序似乎从未被调用。
我使用的是“边缘”版本的pubnub.js,所以我想知道是否有最近的更新,打破了我的实现。你们其他人可以看到我可能会失踪或做错了吗?任何帮助表示赞赏。
//编辑
只是一个快速更新。我已经尝试回滚到3.5.47,并且行为仍然没有改变。我使用Angular的$interval
服务编码了一个快速解决方法,让我的问题得到解决后,该应用至少可以正常工作。上面的代码示例更新了相关更改。
快速修复可能是使用PubNub.js文件的一个版本,它适用于您:' '也许! :-) – PubNub 2014-11-07 17:32:50
嗨,再次!通过设置PubNub JS SDK的特定版本ID,让我们知道这是否适合您。 – PubNub 2014-11-07 21:18:30
我以为我试过3.6.5的API,但我会再给它一次,让你知道它是如何工作的。 – JasonOffutt 2014-11-07 22:08:01