1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
module.exports = Transport;
var C = {
// Transport status codes
STATUS_READY: 0,
STATUS_DISCONNECTED: 1,
STATUS_ERROR: 2
};
/**
* Expose C object.
*/
Transport.C = C;
/**
* Dependencies.
*/
var debug = require('debug')('JsSIP:Transport');
var JsSIP_C = require('./Constants');
var Parser = require('./Parser');
var UA = require('./UA');
var SIPMessage = require('./SIPMessage');
var sanityCheck = require('./sanityCheck');
// 'websocket' module uses the native WebSocket interface when bundled to run in a browser.
var W3CWebSocket = require('websocket').w3cwebsocket;
function Transport(ua, server) {
this.ua = ua;
this.ws = null;
this.server = server;
this.reconnection_attempts = 0;
this.closed = false;
this.connected = false;
this.reconnectTimer = null;
this.lastTransportError = {};
/**
* Options for the Node "websocket" library.
*/
this.node_websocket_options = this.ua.configuration.node_websocket_options || {};
// Add our User-Agent header.
this.node_websocket_options.headers = this.node_websocket_options.headers || {};
this.node_websocket_options.headers['User-Agent'] = JsSIP_C.USER_AGENT;
}
Transport.prototype = {
/**
* Connect socket.
*/
connect: function() {
var transport = this;
if(this.ws && (this.ws.readyState === this.ws.OPEN || this.ws.readyState === this.ws.CONNECTING)) {
debug('WebSocket ' + this.server.ws_uri + ' is already connected');
return false;
}
if(this.ws) {
this.ws.close();
}
debug('connecting to WebSocket ' + this.server.ws_uri);
this.ua.onTransportConnecting(this,
(this.reconnection_attempts === 0)?1:this.reconnection_attempts);
try {
this.ws = new W3CWebSocket(this.server.ws_uri, 'sip', this.node_websocket_options.origin, this.node_websocket_options.headers, this.node_websocket_options.requestOptions, this.node_websocket_options.clientConfig);
this.ws.binaryType = 'arraybuffer';
this.ws.onopen = function() {
transport.onOpen();
};
this.ws.onclose = function(e) {
transport.onClose(e);
};
this.ws.onmessage = function(e) {
transport.onMessage(e);
};
this.ws.onerror = function(e) {
transport.onError(e);
};
} catch(e) {
debug('error connecting to WebSocket ' + this.server.ws_uri + ': ' + e);
this.lastTransportError.code = null;
this.lastTransportError.reason = e.message;
this.ua.onTransportError(this);
}
},
/**
* Disconnect socket.
*/
disconnect: function() {
if(this.ws) {
// Clear reconnectTimer
clearTimeout(this.reconnectTimer);
// TODO: should make this.reconnectTimer = null here?
this.closed = true;
debug('closing WebSocket ' + this.server.ws_uri);
this.ws.close();
}
// TODO: Why this??
if (this.reconnectTimer !== null) {
clearTimeout(this.reconnectTimer);
this.reconnectTimer = null;
this.ua.onTransportDisconnected({
transport: this,
code: this.lastTransportError.code,
reason: this.lastTransportError.reason
});
}
},
/**
* Send a message.
*/
send: function(msg) {
var message = msg.toString();
if(this.ws && this.ws.readyState === this.ws.OPEN) {
debug('sending WebSocket message:\n\n' + message + '\n');
this.ws.send(message);
return true;
} else {
debug('unable to send message, WebSocket is not open');
return false;
}
},
// Transport Event Handlers
onOpen: function() {
this.connected = true;
debug('WebSocket ' + this.server.ws_uri + ' connected');
// Clear reconnectTimer since we are not disconnected
if (this.reconnectTimer !== null) {
clearTimeout(this.reconnectTimer);
this.reconnectTimer = null;
}
// Reset reconnection_attempts
this.reconnection_attempts = 0;
// Disable closed
this.closed = false;
// Trigger onTransportConnected callback
this.ua.onTransportConnected(this);
},
onClose: function(e) {
var connected_before = this.connected;
this.connected = false;
this.lastTransportError.code = e.code;
this.lastTransportError.reason = e.reason;
debug('WebSocket disconnected (code: ' + e.code + (e.reason? '| reason: ' + e.reason : '') +')');
if(e.wasClean === false) {
debug('WebSocket abrupt disconnection');
}
// Transport was connected
if (connected_before === true) {
this.ua.onTransportClosed(this);
// Check whether the user requested to close.
if(!this.closed) {
this.reConnect();
}
} else {
// This is the first connection attempt
// May be a network error (or may be UA.stop() was called)
this.ua.onTransportError(this);
}
},
onMessage: function(e) {
var message, transaction,
data = e.data;
// CRLF Keep Alive response from server. Ignore it.
if(data === '\r\n') {
debug('received WebSocket message with CRLF Keep Alive response');
return;
}
// WebSocket binary message.
else if (typeof data !== 'string') {
try {
data = String.fromCharCode.apply(null, new Uint8Array(data));
} catch(evt) {
debug('received WebSocket binary message failed to be converted into string, message discarded');
return;
}
debug('received WebSocket binary message:\n\n' + data + '\n');
}
// WebSocket text message.
else {
debug('received WebSocket text message:\n\n' + data + '\n');
}
message = Parser.parseMessage(data, this.ua);
if (! message) {
return;
}
if(this.ua.status === UA.C.STATUS_USER_CLOSED && message instanceof SIPMessage.IncomingRequest) {
return;
}
// Do some sanity check
if(! sanityCheck(message, this.ua, this)) {
return;
}
if(message instanceof SIPMessage.IncomingRequest) {
message.transport = this;
this.ua.receiveRequest(message);
} else if(message instanceof SIPMessage.IncomingResponse) {
/* Unike stated in 18.1.2, if a response does not match
* any transaction, it is discarded here and no passed to the core
* in order to be discarded there.
*/
switch(message.method) {
case JsSIP_C.INVITE:
transaction = this.ua.transactions.ict[message.via_branch];
if(transaction) {
transaction.receiveResponse(message);
}
break;
case JsSIP_C.ACK:
// Just in case ;-)
break;
default:
transaction = this.ua.transactions.nict[message.via_branch];
if(transaction) {
transaction.receiveResponse(message);
}
break;
}
}
},
onError: function(e) {
debug('WebSocket connection error: %o', e);
},
/**
* Reconnection attempt logic.
*/
reConnect: function() {
var transport = this;
this.reconnection_attempts += 1;
if(this.reconnection_attempts > this.ua.configuration.ws_server_max_reconnection) {
debug('maximum reconnection attempts for WebSocket ' + this.server.ws_uri);
this.ua.onTransportError(this);
} else {
debug('trying to reconnect to WebSocket ' + this.server.ws_uri + ' (reconnection attempt ' + this.reconnection_attempts + ')');
this.reconnectTimer = setTimeout(function() {
transport.connect();
transport.reconnectTimer = null;
}, this.ua.configuration.ws_server_reconnection_timeout * 1000);
}
}
};
|