检测对Facebook会话的更改 [英] Detect changes to the Facebook session

查看:73
本文介绍了检测对Facebook会话的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我网站的外观在很大程度上取决于用户是否通过Facebook登录,因此,对于我来说,尽快检测出Facebook Session中的任何更改对我来说非常重要,以便我适应该页面反映用户的登录状态.我讨厌对此类代码使用间隔和超时,因为事情可能会变得非常混乱,但据我所知,它似乎是唯一的选择.

The look of my site depends heavily on whether the user is logged in via Facebook and resultantly, it is very important for me to detect any changes in the Facebook Session as soon as they occur in order for me to adapt the look of the page to reflect the users login status. I hate using intervals and timeouts for this sort of code as things can get really messy, but it appears to be the only option (to the best of my knowledge).

我已经尝试过使用FB.subscribe(auth.statusChange),但这似乎并不能正常正常地触发.也许我用错了吗?

I have tried using the FB.subscribe(auth.statusChange), but this does not seem to fire regularly enough and necessarily correctly. Maybe I am using it wrong?

我测试FB.subscribe(auth.statusChange)的方法是每次启动时都记录一次日志,并有意在另一个选项卡上登录和注销Facebook.实际触发该事件大约需要30分钟(而且并不总是触发).这不仅很难测试,因为我每次必须等待30分钟,看来不一定能正确触发.我使用不正确吗?

The way I have tested the FB.subscribe(auth.statusChange), is simply logging each time it is fired and intentionally logging in and out of Facebook on another tab. It seems that it takes around 30 minutes for the event to actually fire (and it does not always fire). Not only is this very hard to test as I have to wait 30 mins each time, it seems it does not necessarily fire correctly. Am I using it incorrectly?

我正在考虑使用的代码是这样的,但是我不满意它,因为它使用了超时/间隔:

The code I am thinking about using is something like this but I am not to happy with it as it uses timeouts/intervals:

window.fbAsyncInit=function(){
    // Initialize the Facebook object
    FB.init({
        appId:"123",
        status:true,
        cookie:true,
        xfbml:true,
        oauth:true
    });
    // Check the Facebook session status every 30 seconds
    setInterval(function(){
        FB.getLoginStatus(function(a){
            // Change the page look based on the facebook status
            fb_welcome(a.status)
        },true)}
    ,30000);
};

或者,我宁愿使用超时而不是间隔,但这需要我将Facebook对象传递给这样的另一个函数:

Alternatively, I would rather use a timeout instead of an interval, but this requires me passing the Facebook object to another function like this:

// Check the Facebook session status every 30 seconds
setTimeout(function(){
    // Pass the Facebook object to the welcome function
    fb_welcome(FB);
,30000);

将FB对象传递给fbAsyncInit函数之外的另一个函数不是一个好主意吗?

Is it a bad idea to pass the FB object to another function outside of the fbAsyncInit function?

推荐答案

实际上,最好订阅auth.authResponseChange事件.最佳做法在 Event.subscribe 文档中列出.

Actually it is better to subscribe to auth.authResponseChange event. This is listed on Event.subscribe documentation as best practices.

在大多数情况下,您将想订阅auth.authResponseChange而不是auth.statusChange.响应以JavaScript数组的形式返回,而不是以JSON编码.

For most cases, you will want to subscribe to auth.authResponseChange rather than auth.statusChange. The response is returned as a javascript array, not encoded as JSON.

更新:
似乎这样对您也不起作用,因为如果您确实调用了Facebook的API,并且在其他情况下(例如登录/注销),它会不时被调用.

Update:
Seems like this will not work for you as well, since it's called from time to time if you do calls to Facebook's API and probably in other cases like login/logout.

Facebook JS-SDK中并不真正存在您需要的内容,并且间隔似乎是一种选择.您可以设置一个仅调用FB.getLoginStatus并保留事件订阅的时间间隔,因为如果检测到状态/authResponse更改,则将触发事件.

What you demand is not really exists in Facebook JS-SDK, and intervals seems to be an option. You can set an interval that will only call to FB.getLoginStatus and leave the event subscriptions as is since if the status/authResponse changes are detected events will be fired.

FB.Event.subscribe('auth.authResponseChange', function(authResponse){
  // authResponse changed
});

window.setInterval(FB.getLoginStatus, 30000);

这篇关于检测对Facebook会话的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆