2014-11-06 63 views
1

我有問題讓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服務編碼了一個快速解決方法,讓我的問題得到解決後,該應用至少可以正常工作。上面的代碼示例更新了相關更改。

+1

快速修復可能是使用PubNub.js文件的一個版本,它適用於您:' '也許! :-) – PubNub 2014-11-07 17:32:50

+0

嗨,再次!通過設置PubNub JS SDK的特定版本ID,讓我們知道這是否適合您。 – PubNub 2014-11-07 21:18:30

+0

我以爲我試過3.6.5的API,但我會再給它一次,讓你知道它是如何工作的。 – JasonOffutt 2014-11-07 22:08:01

回答

0

快速更新。在進行了一些其他任務後,在一年左右的時間內回過頭來看,我們決定再次嘗試解決問題。如上所述,基於間隔的輪詢對我們的初始實施工作正常,但我們現在需要更強大的一組功能。

無論如何,我們最終抓住了最新的穩定版本的JS客戶端(3.7.21),到目前爲止它似乎解決了我們的問題。