diff --git a/lib/account.js b/lib/account.js index af0c00a..da6c1ce 100644 --- a/lib/account.js +++ b/lib/account.js @@ -1,134 +1,135 @@ 'use strict'; import debug from 'debug'; -import transform from 'sdp-transform'; import md5 from 'blueimp-md5'; +import transform from 'sdp-transform'; + import { EventEmitter } from 'events'; import { Call, Identity } from './call'; const DEBUG = debug('sylkrtc:Account'); class Account extends EventEmitter { constructor(options, connection) { if (options.account.indexOf('@') === -1) { throw new Error('Invalid account id specified'); } super(); const id = options.account; const [username, domain] = id.split('@'); this._id = id; this._displayName = options.displayName; this._password = md5(username + ':' + (options.realm || domain)+ ':' + options.password); this._connection = connection; this._registrationState = null; this._calls = new Map(); } get id() { return this._id; } get password() { return this._password; } get displayName() { return this._displayName; } get registrationState() { return this._registrationState; } register() { let req = { sylkrtc: 'account-register', account: this._id }; this._sendRequest(req, (error) => { if (error) { DEBUG('Register error: %s', error); const oldState = this._registrationState; const newState = 'failed'; let data = {reason: error.toString()}; this._registrationState = newState; this.emit('registrationStateChanged', oldState, newState, data); } }); } unregister() { let req = { sylkrtc: 'account-unregister', account: this._id, }; this._sendRequest(req, (error) => { if (error) { DEBUG('Unregister error: %s', error); } const oldState = this._registrationState; const newState = null; this._registrationState = newState; this.emit('registrationStateChanged', oldState, newState, {}); }); } call(uri, options={}) { let callObj = new Call(this); callObj._initOutgoing(uri, options); this._calls.set(callObj.id, callObj); this.emit('outgoingCall', callObj); return callObj; } // Private API _handleEvent(message) { DEBUG('Received account event: %s', message.event); let data = {}; switch (message.event) { case 'registration_state': const oldState = this._registrationState; const newState = message.data.state; this._registrationState = newState; if (newState === 'failed') { data.reason = message.data.reason; } this.emit('registrationStateChanged', oldState, newState, data); break; case 'incoming_session': let call = new Call(this); call._initIncoming(message.session, message.data.originator, message.data.sdp); this._calls.set(call.id, call); // see what media types are offered let mediaTypes = { audio: false, video: false }; const parsedSdp = transform.parse(message.data.sdp); for (let media of parsedSdp.media) { if (media.type === 'audio' && media.port !== 0 && media.direction === 'sendrecv') { mediaTypes.audio = true; } else if (media.type === 'video' && media.port !== 0 && media.direction === 'sendrecv') { mediaTypes.video = true; } } DEBUG('Incoming call from %s with media types: %o', message.data.originator.uri, mediaTypes); this.emit('incomingCall', call, mediaTypes); break; case 'missed_session': data.originator = new Identity(message.data.originator.uri, message.data.originator.display_name); this.emit('missedCall', data); break; default: break; } } _sendRequest(req, cb) { this._connection._sendRequest(req, cb); } } export { Account }; diff --git a/lib/connection.js b/lib/connection.js index 62bdd13..4ef1274 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -1,284 +1,311 @@ 'use strict'; +import bowser from 'bowser'; import debug from 'debug'; import uuid from 'node-uuid'; import { EventEmitter } from 'events'; import { setImmediate } from 'timers'; import { w3cwebsocket as W3CWebSocket } from 'websocket'; import { Account } from './account'; const SYLKRTC_PROTO = 'sylkRTC-1'; const DEBUG = debug('sylkrtc:Connection'); const MSECS = 1000; const INITIAL_DELAY = 0.5 * MSECS; const MAX_DELAY = 64 * MSECS; +// compute a string for our well-known platforms +let platform; +if (bowser.iphone) { + platform = 'iPhone'; +} else if (bowser.ipad) { + platform = 'iPad'; +} else if (bowser.ipod) { + platform = 'iPod'; +} else if (bowser.android) { + platform = 'Android'; +} else if (bowser.mac) { + platform = 'OSX'; +} else if (bowser.linux) { + platform = 'Linux'; +} else if (bowser.windows) { + platform = 'Windows'; +} else { + platform = 'Unknown'; +} +if (bowser.osversion) { + platform = `%{platform} ${bowser.osversion}`; +} + +const USER_AGENT = `SylkRTC (${bowser.name} ${bowser.version} on ${platform})`; + class Connection extends EventEmitter { constructor(options = {}) { if (!options.server) { throw new Error('\"server\" must be specified'); } super(); this._wsUri = options.server; this._sock = null; this._state = null; this._closed = false; this._timer = null; this._delay = INITIAL_DELAY; this._accounts = new Map(); this._requests = new Map(); } get state() { return this._state; } close() { if (this._closed) { return; } this._closed = true; if (this._timer) { clearTimeout(this._timer); this._timer = null; } if (this._sock) { this._sock.close(); this._sock = null; } else { setImmediate(() => { this._setState('closed'); }); } } addAccount(options = {}, cb = null) { if (typeof options.account !== 'string' || typeof options.password !== 'string') { throw new Error('Invalid options, \"account\" and \"password\" must be supplied'); } if (this._accounts.has(options.account)) { throw new Error('Account already added'); } let acc = new Account(options, this); // add it early to the set so we don't add it more than once, ever this._accounts.set(acc.id, acc); let req = { sylkrtc: 'account-add', account: acc.id, password: acc.password, - display_name: acc.displayName + display_name: acc.displayName, + user_agent: USER_AGENT }; this._sendRequest(req, (error) => { if (error) { DEBUG('add_account error: %s', error); this._accounts.delete(acc.id); acc = null; } if (cb) { cb(error, acc); } }); } removeAccount(account, cb=null) { const acc = this._accounts.get(account.id); if (account !== acc) { throw new Error('Unknown account'); } // delete the account from the mapping, regardless of the result this._accounts.delete(account.id); let req = { sylkrtc: 'account-remove', account: acc.id }; this._sendRequest(req, (error) => { if (error) { DEBUG('remove_account error: %s', error); } if (cb) { cb(); } }); } reconnect() { if (this._state === 'disconnected') { clearTimeout(this._timer); this._delay = INITIAL_DELAY; this._timer = setTimeout(() => { this._connect(); }, this._delay); } } // Private API _initialize() { if (this._sock !== null) { throw new Error('WebSocket already initialized'); } if (this._timer !== null) { throw new Error('Initialize is in progress'); } DEBUG('Initializing'); if (process.browser) { window.addEventListener('beforeunload', () => { if (this._sock !== null) { let noop = function() {}; this._sock.onerror = noop; this._sock.onmessage = noop; this._sock.onclose = noop; this._sock.close(); } }); } this._timer = setTimeout(() => { this._connect(); }, this._delay); } _connect() { DEBUG('WebSocket connecting'); this._setState('connecting'); this._sock = new W3CWebSocket(this._wsUri, SYLKRTC_PROTO); this._sock.onopen = () => { DEBUG('WebSocket connection open'); this._onOpen(); }; this._sock.onerror = () => { DEBUG('WebSocket connection got error'); }; this._sock.onclose = (event) => { DEBUG('WebSocket connection closed: %d: (reason=\"%s\", clean=%s)', event.code, event.reason, event.wasClean); this._onClose(); }; this._sock.onmessage = (event) => { DEBUG('WebSocket received message: %o', event); this._onMessage(event); }; } _sendRequest(req, cb) { const transaction = uuid.v4(); req.transaction = transaction; if (this._state !== 'ready') { setImmediate(() => { cb(new Error('Connection is not ready')); }); return; } this._requests.set(transaction, {req: req, cb: cb}); this._sock.send(JSON.stringify(req)); } _setState(newState) { DEBUG('Set state: %s -> %s', this._state, newState); const oldState = this._state; this._state = newState; this.emit('stateChanged', oldState, newState); } // WebSocket callbacks _onOpen() { clearTimeout(this._timer); this._timer = null; this._delay = INITIAL_DELAY; this._setState('connected'); } _onClose() { this._sock = null; if (this._timer) { clearTimeout(this._timer); this._timer = null; } // remove all accounts, the server no longer has them anyway this._accounts.clear(); this._setState('disconnected'); if (!this._closed) { this._delay = this._delay * 2; if (this._delay > MAX_DELAY) { this._delay = INITIAL_DELAY; } DEBUG('Retrying connection in %s seconds', this._delay / MSECS); this._timer = setTimeout(() => { this._connect(); }, this._delay); } else { this._setState('closed'); } } _onMessage(event) { let message = JSON.parse(event.data); if (typeof message.sylkrtc === 'undefined') { DEBUG('Unrecognized message received'); return; } DEBUG('Received \"%s\" message: %o', message.sylkrtc, message); if (message.sylkrtc === 'event') { DEBUG('Received event: \"%s\"', message.event); switch (message.event) { case 'ready': this._setState('ready'); break; default: break; } } else if (message.sylkrtc === 'account_event') { let acc = this._accounts.get(message.account); if (!acc) { DEBUG('Account %s not found', message.account); return; } acc._handleEvent(message); } else if (message.sylkrtc === 'session_event') { const sessionId = message.session; for (let acc of this._accounts.values()) { let call = acc._calls.get(sessionId); if (call) { call._handleEvent(message); break; } } } else if (message.sylkrtc === 'ack' || message.sylkrtc === 'error') { const transaction = message.transaction; const data = this._requests.get(transaction); if (!data) { DEBUG('Could not find transaction %s', transaction); return; } this._requests.delete(transaction); DEBUG('Received \"%s\" for request: %o', message.sylkrtc, data.req); if (data.cb) { if (message.sylkrtc === 'ack') { data.cb(null); } else { data.cb(new Error(message.error)); } } } } } export { Connection }; diff --git a/package.json b/package.json index a0d70bd..90421ea 100644 --- a/package.json +++ b/package.json @@ -1,46 +1,47 @@ { "name": "sylkrtc", "version": "0.6.1", "main": "lib/sylkrtc.js", "description": "SylkServer WebRTC Gateway client library", "repository": { "type": "git", "url": "git://github.com/AGProjects/sylkrtc.git" }, "keywords": [], "author": "AG Projects", "contributors": [ "Tijmen de Mes ", "Saúl Ibarra Corretgé " ], "license": "MIT", "readmeFilename": "README.md", "browserify": { "transform": [ "babelify" ] }, "dependencies": { "blueimp-md5": "^2.3.0", + "bowser": "^1.3.0", "debug": "^2.2.0", "node-uuid": "^1.4.7", "rtcninja": "^0.6.6", "sdp-transform": "^1.6.2", "websocket": "^1.0.23" }, "devDependencies": { "babel-preset-es2015": "^6.9.0", "babelify": "^7.3.0", "browserify": "^13.0.1", "gulp": "git+https://github.com/gulpjs/gulp.git#4.0", "gulp-filelog": "^0.4.1", "gulp-header": "^1.8.2", "gulp-jshint": "^2.0.1", "gulp-sourcemaps": "^1.6.0", "gulp-uglify": "^1.5.3", "gulp-util": "^3.0.7", "jshint-stylish": "^2.2.0", "vinyl-buffer": "^1.0.0", "vinyl-source-stream": "^1.1.0" } }