我在启动PubNub的订阅消息处理程序时遇到问题。我正在开发一个网络客户端,它可以监听来自移动应用程序的消息。直到最近,此代码还运行良好。我可以通过手机发送消息,看到网络应用程序自动更新。但在过去的几天里,该网络应用程序不再更新。
这是我用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
服务编写了一个快速解决方案,以允许应用程序至少在解决这个问题时正常工作。更新了上面的代码示例,并进行了相关更改。
快速更新。在着手其他一些任务并在一年左右后回到这个问题上之后,我们决定再次尝试这个问题。如上所述,基于间隔的轮询在我们最初的实现中运行良好,但我们现在需要一组更健壮的功能。
无论如何,我们最终获得了JS客户端的最新稳定版本(3.7.21),到目前为止,它似乎已经解决了我们的问题。