2017-01-28 4 views
0

2日間の拷問の後、私はpubnubストレージアカウントに保存されている歴史的なメッセージを実際にどのように公開できるのか理解できないままこの質問を投稿しました。それを最も基本的なものとして理解するために、私はチャットアプリを作って、SDKで説明されているように履歴機能を使用しましたが、メッセージが失われたページを更新するたびに、私はバックフィルを試み、不運に加入して属性を復元しました。私がしたいのは、クロムのリフレッシュをクリックしてそこに残っているメッセージを見ることだけです。チャット履歴のメッセージをメッセンジャーPubnubに表示する

<div><input id=input placeholder=you-chat-here /></div> 

Chat Output 
<div id=box></div> 

<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js"></script> 

<script>(function(){ 
    var pubnub = new PubNub({ publishKey : 'demo', subscribeKey : 'demo' }); 
    function $(id) { return document.getElementById(id); } 
    var box = $('box'), input = $('input'), channel = 'chat'; 
    pubnub.addListener({ 

     message: function(obj) { 
      box.innerHTML = (''+obj.message).replace(/[<>]/g, '') + '<br>' + box.innerHTML 
     }}); 
     pubnub.history({ 
      channel: 'chat', 
      reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first. 
      count: 100, // how many items to fetch 
      callback : function(msgs) { 
       pubnub.each(msgs[0], chat); 
      } 
     }, 
     function (status, response) { 
      // handle status, response 
      console.log("messages successfully retreived") 
     }); 

     pubnub.subscribe({channels:[channel], 
          restore: true, 
          backfill: true, 
          ssl: true}); 

     input.addEventListener('keyup', function(e) { 
      if ((e.keyCode || e.charCode) === 13) { 
       pubnub.publish({channel : channel, message : input.value,x : (input.value='')}); 
      } 
     }); 
    })(); 
</script> 

</body> 
+0

、それは実際にあなたのメッセージを返していますが、コールバックが歴史メソッドから動作していない理由、それはだと、オブジェクトではなく、必ず、あなたの 'response.messages'をチェック – Roljhon

答えて

0

あなたのhistoryコードが正しくないと思います。あなたのコード応答がfunction引数になるので、callbackの必要はありません。 This example is from the JavaScript SDK docs

pubnub.history(
    { 
     channel: 'chat', 
    }, 
    function (status, response) { 
     var msgs = response.messages; 

     if (msgs != undefined && msgs.length > 0) { 
      // if msgs were retrieved, do something useful 
      console.log(msgs); 
     } 
    } 
); 
関連する問題