@ -3,7 +3,9 @@ var config = require('getconfig'),
uuid = require ( 'node-uuid' ) ,
uuid = require ( 'node-uuid' ) ,
mysql = require ( 'mysql' ) ,
mysql = require ( 'mysql' ) ,
cookie _reader = require ( 'cookie' ) ,
cookie _reader = require ( 'cookie' ) ,
io = require ( 'socket.io' ) . listen ( config . server . port ) ;
crypto = require ( 'crypto' ) ,
port = parseInt ( process . env . PORT || config . server . port , 10 ) ,
io = require ( 'socket.io' ) . listen ( port ) ;
var sql = mysql . createConnection ( {
var sql = mysql . createConnection ( {
host : config . mysql . server ,
host : config . mysql . server ,
@ -11,6 +13,11 @@ var sql = mysql.createConnection({
user : config . mysql . user ,
user : config . mysql . user ,
password : config . mysql . password } ) ;
password : config . mysql . password } ) ;
if ( config . logLevel ) {
// https://github.com/Automattic/socket.io/wiki/Configuring-Socket.IO
io . set ( 'log level' , config . logLevel ) ;
}
function describeRoom ( name ) {
function describeRoom ( name ) {
var clients = io . sockets . clients ( name ) ;
var clients = io . sockets . clients ( name ) ;
var result = {
var result = {
@ -94,24 +101,30 @@ io.sockets.on('connection', function (client) {
client . on ( 'unshareScreen' , function ( type ) {
client . on ( 'unshareScreen' , function ( type ) {
client . resources . screen = false ;
client . resources . screen = false ;
if ( client . room ) removeFeed ( 'screen' ) ;
removeFeed ( 'screen' ) ;
} ) ;
} ) ;
client . on ( 'join' , join ) ;
client . on ( 'join' , join ) ;
function removeFeed ( type ) {
function removeFeed ( type ) {
io . sockets . in ( client . room ) . emit ( 'remove' , {
if ( client . room ) {
id : client . id ,
io . sockets . in ( client . room ) . emit ( 'remove' , {
type : type
id : client . id ,
} ) ;
type : type
} ) ;
if ( ! type ) {
client . leave ( client . room ) ;
client . room = undefined ;
}
}
}
}
function join ( name , cb ) {
function join ( name , cb ) {
// sanity check
// sanity check
if ( typeof name !== 'string' ) return ;
if ( typeof name !== 'string' ) return ;
// leave any existing rooms
// leave any existing rooms
if ( client . room ) removeFeed ( ) ;
removeFeed ( ) ;
safeCb ( cb ) ( null , describeRoom ( name ) )
safeCb ( cb ) ( null , describeRoom ( name ) ) ;
client . join ( name ) ;
client . join ( name ) ;
client . room = name ;
client . room = name ;
}
}
@ -121,7 +134,9 @@ io.sockets.on('connection', function (client) {
client . on ( 'disconnect' , function ( ) {
client . on ( 'disconnect' , function ( ) {
removeFeed ( ) ;
removeFeed ( ) ;
} ) ;
} ) ;
client . on ( 'leave' , removeFeed ) ;
client . on ( 'leave' , function ( ) {
removeFeed ( ) ;
} ) ;
client . on ( 'create' , function ( name , cb ) {
client . on ( 'create' , function ( name , cb ) {
if ( arguments . length == 2 ) {
if ( arguments . length == 2 ) {
@ -139,7 +154,29 @@ io.sockets.on('connection', function (client) {
safeCb ( cb ) ( null , name ) ;
safeCb ( cb ) ( null , name ) ;
}
}
} ) ;
} ) ;
// tell client about stun and turn servers and generate nonces
if ( config . stunservers ) {
client . emit ( 'stunservers' , config . stunservers ) ;
}
if ( config . turnservers ) {
// create shared secret nonces for TURN authentication
// the process is described in draft-uberti-behave-turn-rest
var credentials = [ ] ;
config . turnservers . forEach ( function ( server ) {
var hmac = crypto . createHmac ( 'sha1' , server . secret ) ;
// default to 86400 seconds timeout unless specified
var username = Math . floor ( new Date ( ) . getTime ( ) / 1000 ) + ( server . expiry || 86400 ) + "" ;
hmac . update ( username ) ;
credentials . push ( {
username : username ,
credential : hmac . digest ( 'base64' ) ,
url : server . url
} ) ;
} ) ;
client . emit ( 'turnservers' , credentials ) ;
}
} ) ;
} ) ;
if ( config . uid ) process . setuid ( config . uid ) ;
if ( config . uid ) process . setuid ( config . uid ) ;
console . log ( 'signal master is running at: http://localhost:' + config . server . port ) ;
console . log ( 'signal master is running at: http://localhost:' + port ) ;