如何从facebook API连续轮询数据?

时间:2016-11-17 18:26:06

标签: javascript facebook facebook-graph-api facebook-javascript-sdk facebook-graph-api-v2.4

我正在创建一个简单的网站,使用facebook javascript sdk和facebook API从facebook页面读取消息。我需要在Facebook页面到达时不断阅读这些消息吗?我该怎么做呢?。如果我在while(true){FB.api()}之类的循环中连续运行它,网站会挂起!我需要不断轮询聊天中的消息,然后在网站上显示。这样做的任何线索? 这就是我正在做的事情。或者我应该能够连续轮询未读消息,然后将其显示给用户!。

        FB.api(
            "/<pageId>?fields=access_token",
            function (response) {
            if (response && !response.error) {
                console.log(response);
                /* handle the result */
                document.getElementById('status').innerHTML = response.access_token;
                FB.api(
                    "/<pageid>?fields=conversations{message_count,unread_count}",
                    {access_token : response.access_token},
                    function(response2){
                        if(response2 && !response2.error){

                            var conversationId = response2.conversations.data[0].id;
                            var unreadCounter = response2.conversations.data[0].unread_count;
                            if(!unreadCounter){
                                console.log("There are no unread msgs currently":unreadCounter);    

                            }


                            //console.log(conversationId);
                            FB.api(
                             "/"+conversationId+"?fields=snippet",
                             {access_token : response.access_token},
                            function (response) {
                            if (response && !response.error) {
                            /* handle the result */
                            console.log(response);
                            }
                            else{
                                console.log(response);
                            }
                        }   
                    );


                            //handling result
                            console.log(response2);
                            document.getElementById('status').innerHTML = response2.conversations;
                        }
                        else{
                            console.log(response2);
                        }

                    }
                );
            }
            else{
                console.log(response);
            }
        },{scope:"read_page_mailboxes"});   

0 个答案:

没有答案