How to differ sessions in browser-tabs?

后端 未结 21 1625
情深已故
情深已故 2020-11-22 08:48

In a web-application implemented in java using JSP and Servlets; if I store information in the user session, this information is shared from all the tabs from the same brows

21条回答
  •  情深已故
    2020-11-22 08:57

    I resolved this of following way:

    • I've assigned a name to window this name is the same of connection resource.
    • plus 1 to rid stored in cookie for attach connection.
    • I've created a function to capture all xmloutput response and assign sid and rid to cookie in json format. I do this for each window.name.

    here the code:

    var deferred = $q.defer(),
            self = this,
            onConnect = function(status){
              if (status === Strophe.Status.CONNECTING) {
                deferred.notify({status: 'connecting'});
              } else if (status === Strophe.Status.CONNFAIL) {
                self.connected = false;
                deferred.notify({status: 'fail'});
              } else if (status === Strophe.Status.DISCONNECTING) {
                deferred.notify({status: 'disconnecting'});
              } else if (status === Strophe.Status.DISCONNECTED) {
                self.connected = false;
                deferred.notify({status: 'disconnected'});
              } else if (status === Strophe.Status.CONNECTED) {
                self.connection.send($pres().tree());
                self.connected = true;
                deferred.resolve({status: 'connected'});
              } else if (status === Strophe.Status.ATTACHED) {
                deferred.resolve({status: 'attached'});
                self.connected = true;
              }
            },
            output = function(data){
              if (self.connected){
                var rid = $(data).attr('rid'),
                    sid = $(data).attr('sid'),
                    storage = {};
    
                if (localStorageService.cookie.get('day_bind')){
                  storage = localStorageService.cookie.get('day_bind');
                }else{
                  storage = {};
                }
                storage[$window.name] = sid + '-' + rid;
                localStorageService.cookie.set('day_bind', angular.toJson(storage));
              }
            };
        if ($window.name){
          var storage = localStorageService.cookie.get('day_bind'),
              value = storage[$window.name].split('-')
              sid = value[0],
              rid = value[1];
          self.connection = new Strophe.Connection(BoshService);
          self.connection.xmlOutput = output;
          self.connection.attach('bosh@' + BoshDomain + '/' + $window.name, sid, parseInt(rid, 10) + 1, onConnect);
        }else{
          $window.name = 'web_' + (new Date()).getTime();
          self.connection = new Strophe.Connection(BoshService);
          self.connection.xmlOutput = output;
          self.connection.connect('bosh@' + BoshDomain + '/' + $window.name, '123456', onConnect);
        }
    

    I hope help you

提交回复
热议问题