Spotify Apps API: Offline Detection

Is there a way to tell if Spotify is offline? I am currently developing an application that clearly depends on a working internet connection. I would like to know if there is an Internet connection at all, and if not, display an error message.

I've found some common javascript solutions that don't seem to work, for example:

var online = navigator.onLine;

      

+3


source to share


4 answers


This is how you do it in the new API (2013+) 1.0:

 models.session.addEventListener('change:online',function({
    console.log('online =',models.session.online);
});

      



See https://developer.spotify.com/docs/apps/api/1.0/api-models-session.html for details .

0


source


You can determine if a client is offline by referring to the state in the session object.

https://developer.spotify.com/technologies/apps/docs/a5a59ca068.html



What you need to do is listen for the state change with an observer and look for the OFFLINE state.

DISCONNECTED: 2
DUMMY_USER: 4
LOGGED_IN: 1
LOGGED_OUT: 0
OFFLINE: 3

var sp = getSpotifyApi(1);
var models = sp.require('sp://import/scripts/api/models');

models.session.observe(models.EVENT.STATECHANGED, function() {
    console.log(models.session.state);
});

      

+5


source


http://developer.spotify.com/en/libspotify/docs/group__session.html

      

Check

sp_connectionstate 

      

for enum value:

SP_CONNECTION_STATE_OFFLINE

      

Gotta get what you want.

0


source


For V1 App APIs, you will do something according to ...

require(['$api/models'], function(models) {

  // add a listener to pick up any changes in on / offline state
  models.session.addEventListener('change', changeOffline);

  function changeOffline(){
    var online = models.session.online; // returns true / false
    if(!online){
      // show offline message etc...
    } else {
      // hide offline message etc...
    }
  }
});

      

https://developer.spotify.com/docs/apps/api/1.0/models-session.html

0


source







All Articles