在Facebook上实时获取用户登录状态 [英] Get user login status on Facebook by real time

查看:808
本文介绍了在Facebook上实时获取用户登录状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我的另一个问题有关:如何知道用户是否退出Facebook,FB.Event.Subscribe不工作

This question is related to another question of mine: How to know if a user log out of Facebook, FB.Event.Subscribe not work

在那个问题,当用户退出Facebook时,我试图收到一个回叫事件。但由于没有适当的答案,我必须再试一次。我尝试通过javascript获取用户的登录状态,如下所示:

In that question, I tried to get a call back event when the users log out of Facebook. But since it has no appropriate answer yet, I must try another way. I try polling to get the login status of a user by javascript, like this:

        function checkLogin()
        {
            alert("in");
            FB.getLoginStatus(function(response) {
              if (response.status == "connected") {
                // logged in and connected user, someone you know
                alert("ok - 5 seconds has passed");
              } else {
                // no user session available, someone you dont know
                alert("not ok");
              }
            });
            t=checkLogin("timedCount()",5000);
        }

这个问题是:该函数只返回正确的第一次被称为。之后,似乎缓存了结果,尽管用户在浏览器中以另一个标签登录了用户 ,但是我不断接收到连接。

The problem with this is that: the function only returns the correct result the first time it gets called. After that, it seems the result is cached, and I continously received "connected" in response, although the user silently logged out by another tab in browser.

这不是很好,因为当时Facebook弹出一个要求用户登录的对话框。但是如果用户取消它,他仍然可以使用我的应用程序(导致他的会话与我的应用程序服务器还没有过期!)。

This is not good, because at that time Facebook pop out a dialog which ask the user to login. But if the user cancel it, he still can work with my application (caused his session with my application server is not expired yet!).

文件,FB dev说:


当您调用FB.getLoginStatus时,可以向
www.facebook.com发出HTTP请求,以检查用户的当前状态。但是,如果在此浏览器会话期间
FB.getLoginStatus已被调用,则
和会话已保留在cookie中,可能不需要向
facebook.com的HTTP请求

When you invoke FB.getLoginStatus, an HTTP request may be made to www.facebook.com to check the current status of the user. However, if FB.getLoginStatus has already been called during this browser session, and a session has been persisted within a cookie, an HTTP request to facebook.com may not be needed

所以我认为这个效果是由缓存引起的。我不知道Facebook为什么这样做。

So I think this effect caused by the cached. I wonder why Facebook do it that way.

有没有人知道这个问题的工作?

Does anyone know a work around for this issue?

谢谢对于任何帮助,

Hoang Long

Hoang Long

推荐答案

很长时间,最终,直到我的一个同事解决了这个问题。

It took me a long time enough, eventually, until one of my co-workers solved the problem.

解决方案是我们需要再次调用 FB.init(),使用在调用 getLoginStatus 之前,cookie:false 。这将删除cookie并获取新的信息。

The solution is that we need to call FB.init() again, with cookie:false before calling getLoginStatus. This will remove the cookie and get new information.

FB.init({ 
        appId:'${appId}', 
            cookie: false, 
        status: true, 
        xfbml: true 
    });

FB.getLoginStatus(function (response) {
        if (response.session) {
                alert("here");
        }
});

这篇关于在Facebook上实时获取用户登录状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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