Skip to content

Commit

Permalink
Enhance transport error handling
Browse files Browse the repository at this point in the history
- Add disconnection 'cause' and 'code' properties to UA 'disconnection' event handler
- Emit UA 'disconnected' event every time UA is noticed about a transport disconnection regardles there are other servers to reconnect.
  • Loading branch information
jmillan committed Jan 28, 2013
1 parent eb1aa63 commit aec55a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ JsSIP.Transport = function(ua, server) {
this.closed = false;
this.connected = false;
this.reconnectTimer = null;
this.lastTransportError = {};

this.ua.transport = this;

Expand Down Expand Up @@ -127,6 +128,8 @@ JsSIP.Transport.prototype = {
var connected_before = this.connected;

this.connected = false;
this.lastTransportError.code = e.code;
this.lastTransportError.reason = e.reason;
console.warn(JsSIP.c.LOG_TRANSPORT +'WebSocket disconnected: code=' + e.code + (e.reason? ', reason=' + e.reason : ''));

if(e.wasClean === false) {
Expand All @@ -140,6 +143,12 @@ JsSIP.Transport.prototype = {
// Reset reconnection_attempts
this.reconnection_attempts = 0;
this.reConnect();
} else {
this.ua.emit('disconnected', this.ua, {
transport: this,
code: this.lastTransportError.code,
reason: this.lastTransportError.reason
});
}
} else {
// This is the first connection attempt
Expand Down
12 changes: 9 additions & 3 deletions src/UA.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ JsSIP.UA.prototype.onTransportError = function(transport) {
transport.server.status = JsSIP.c.WS_SERVER_ERROR;
console.log(JsSIP.c.LOG_UA +'connection status set to: '+ JsSIP.c.WS_SERVER_ERROR);

this.emit('disconnected', this, {
transport: transport,
code: transport.lastTransportError.code,
reason: transport.lastTransportError.reason
});

server = this.getNextWsServer();

if(server) {
Expand All @@ -310,9 +316,7 @@ JsSIP.UA.prototype.onTransportError = function(transport) {
if (!this.error || this.error !== JsSIP.c.UA_NETWORK_ERROR) {
this.status = JsSIP.c.UA_STATUS_NOT_READY;
this.error = JsSIP.c.UA_NETWORK_ERROR;
this.emit('disconnected');
}

// Transport Recovery process
this.recoverTransport();
}
Expand All @@ -339,7 +343,9 @@ JsSIP.UA.prototype.onTransportConnected = function(transport) {

this.status = JsSIP.c.UA_STATUS_READY;
this.error = null;
this.emit('connected', this);
this.emit('connected', this, {
transport: transport
});

if(this.configuration.register) {
if(this.registrator) {
Expand Down

0 comments on commit aec55a2

Please sign in to comment.