Skip to content

Commit

Permalink
Fix #26. Fire 'unregistered' event correctly.
Browse files Browse the repository at this point in the history
Fire the event in any of the following cases:

- SIP response to un-REGISTER request
- un-REGISTER request transaction timeout
- Transport error during un-REGISTER request
- re-REGISTER failure
  • Loading branch information
jmillan committed Feb 1, 2013
1 parent 5616837 commit 3a6971d
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/Registrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ JsSIP.Registrator.prototype = {
extraHeaders = extraHeaders || [];

this.registered = false;
this.ua.emit('unregistered', this.ua);

// Clear the registration timer.
window.clearTimeout(this.registrationTimer);
Expand Down Expand Up @@ -216,21 +215,39 @@ JsSIP.Registrator.prototype = {
* @private
*/
this.receiveResponse = function(response) {
console.log(JsSIP.C.LOG_REGISTRATOR +response.status_code + ' ' + response.reason_phrase + ' received to unregister request');
var cause;

switch(true) {
case /^1[0-9]{2}$/.test(response.status_code):
// Ignore provisional responses.
break;
case /^2[0-9]{2}$/.test(response.status_code):
this.unregistered(response);
break;
default:
cause = JsSIP.Utils.sipErrorCause(response.status_code);

if (cause) {
cause = JsSIP.C.causes[cause];
} else {
cause = JsSIP.C.causes.SIP_FAILURE_CODE;
}
this.unregistered(response, cause);
}
};

/**
* @private
*/
this.onRequestTimeout = function() {
console.log(JsSIP.C.LOG_REGISTRATOR +'Request Timeout received for unregister request');
this.unregistered(null, JsSIP.C.causes.REQUEST_TIMEOUT);
};

/**
* @private
*/
this.onTransportError = function() {
console.log(JsSIP.C.LOG_REGISTRATOR +'Transport Error received for unregister request');
this.unregistered(null, JsSIP.C.causes.CONNECTION_ERROR);
};

request_sender.send();
Expand All @@ -242,14 +259,28 @@ JsSIP.Registrator.prototype = {
registrationFailure: function(response, cause) {
if (this.registered) {
this.registered = false;
this.ua.emit('unregistered', this.ua);
this.ua.emit('unregistered', this.ua, {
response: response || null,
cause: cause
});
}
this.ua.emit('registrationFailed', this.ua, {
response: response || null,
cause: cause
});
},

/**
* @private
*/
unregistered: function(response, cause) {
this.registered = false;
this.ua.emit('unregistered', this.ua, {
response: response || null,
cause: cause || null
});
},

/**
* @private
*/
Expand Down

0 comments on commit 3a6971d

Please sign in to comment.