문제

I'm developing a iOS application and i'd like to check if a specific user is online or not. My first idea was to send cyclically at a distance of five seconds asynchronous connections from the application to a php page that updates a field in the database by inserting the current date and time. Then, to see if a particular user is online, the application fetch this data from the database every 5 seconds and compare it with the current time: if current time is greater than data collected (greater than 5 seconds), it means that the user is offline , otherwise it is online. This works but surely the instant messaging applications do not use this method because it is too expensive. Which approach should be used?

I hope i explained myself, thank you!

도움이 되었습니까?

해결책

Here's how I would attack the problem:

  1. Have the app assign a unique ID to the IOS device. (I'd save it in NSUserDefaults)
  2. When the app starts check a url to get a response (ie: check if the user is online)
  3. Connect to a server and if it hasn't already, compare the unique key with other entries
  4. If there is no other entries in the entire entry history set another NSUserDefault (BOOL) to tell the app it never needs to check again.
  5. This unique key now becomes a 'log-in' of sorts and can be transmitted to the server
  6. (optional) have the user fill out information so the server can relate the key to a person/stats/whatever information, rather than just a lonely key and upload this information (if this is done, AFAIK, apple requires you to warn users that their information is being taken.)
  7. Have the device send a message every so often to the server to tell it 'hey, I'm still alive'
  8. Have the server cycle through every so often (I'd say ~ double the time the IOS device sends the 'I'm alive' message. ie: if the device tells the server every 5 seconds it's still online, have the server check the list and responses every 7.5 - 10 seconds) and remove the list of online keys using information gathered in a response table. If the IOS device hasn't sent the message when the server cycles through, it mustn't be online.

I can see where you are going with time stamps, however with multiple users around the world with multiple connections, you would have to be lenient with your date & time entries and checks (however you seem to already have that in mind) because it takes longer times for some users' internet than others to respond.

EDIT: just saw your lat bit about instant messaging. From my experience if you are on a mobile device it isn't always instant (it seems to me that when I open up a chat conversation to someone on my phone over Facebook, their online status is not updated until I send the message/every set amount of time). Sometimes I will receive messages 30 seconds to a minute later on my phone after it has reached my computer. But this seems to be prioritised (ie: if I'm having a big conversation with someone with lots of messages going back and forth, communication is very instant)-just thought I'd share.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top