简体   繁体   English

实时获取Facebook上的用户登录状态

[英]Get user login status on Facebook by real time

This question is related to another question of mine: How to know if a user log out of Facebook, FB.Event.Subscribe not work这个问题与我的另一个问题有关: How to know if a user log out of Facebook, FB.Event.Subscribe not work

In that question, I tried to get a call back event when the users log out of Facebook.在那个问题中,当用户退出 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:我尝试通过 javascript 轮询获取用户的登录状态,如下所示:

        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 .这样做的问题是: function 仅在第一次被调用时才返回正确的结果 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.之后,结果似乎被缓存了,我不断收到“已连接”的响应,尽管用户在浏览器中的另一个选项卡上默默地注销了。

This is not good, because at that time Facebook pop out a dialog which ask the user to login.这不好,因为当时 Facebook 会弹出一个对话框,要求用户登录。 But if the user cancel it, he still can work with my application (caused his session with my application server is not expired yet.).但是如果用户取消它,他仍然可以使用我的应用程序(导致他的 session 与我的应用程序服务器尚未过期。)。

In the document , FB dev says that:文档中,FB 开发人员说:

When you invoke FB.getLoginStatus, an HTTP request may be made to www.facebook.com to check the current status of the user.当您调用 FB.getLoginStatus 时,可能会向 www.facebook.com 发出 HTTP 请求以检查用户的当前状态。 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 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

So I think this effect caused by the cached.所以我认为这种影响是缓存造成的。 I wonder why Facebook do it that way.我想知道为什么 Facebook 会那样做。

Does anyone know a work around for this issue?有谁知道解决这个问题的方法?

Thanks for any help,谢谢你的帮助,

Hoang Long黄龙

It took me a long time enough, eventually, until one of my co-workers solved the problem.最终,我花了很长时间,直到我的一位同事解决了这个问题。

The solution is that we need to call FB.init() again, with cookie:false before calling getLoginStatus .解决方案是我们需要在调用getLoginStatus之前使用cookie:false再次调用FB.init() This will remove the cookie and get new information.这将删除 cookie 并获取新信息。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM