我有一个 iOS 应用程序,用户可以随时登录和注销。在服务器端,我想知道哪些用户在线。
我可以做一些事情,比如在调用登录 web 服务时将用户设置为在线,并在调用注销 web 服务时将其设置为离线。但是,我不能确定是否总是注销,可能是手机突然关机了,所以服务器会让用户保持在线,当他们再次尝试登录时,他会得到错误“对不起,你已经登录了在”
也许我可以对反馈服务器做点什么(我的应用程序使用推送通知),但我不知道它检测到用户离线的速度有多快......
In these cases I think the best think to do is that your App should ping your server every X seconds and update a kind of "last_online" field.
Then in your logic, you can consider for example that a user last seen online more than 3 minutes ago is to be considered offline.
Like you said, you can't trust the user to click "logout" so you can't assume that's the time he logged out.
Of course you can still let the user click logout manually.