2016-10-21 13:29:04 +00:00
|
|
|
const uuid = require('node-uuid')
|
2016-10-19 00:29:21 +00:00
|
|
|
|
|
|
|
// based off of https://github.com/andyet/signalmaster
|
|
|
|
// since it was updated to socket.io 1.3.7
|
|
|
|
|
|
|
|
function safeCb(cb) {
|
|
|
|
if (typeof cb === 'function') {
|
2016-11-07 20:25:08 +00:00
|
|
|
return cb
|
2016-10-19 00:29:21 +00:00
|
|
|
} else {
|
2016-11-07 20:25:08 +00:00
|
|
|
return function() {}
|
2016-10-19 00:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function(io, stunservers, state) {
|
2016-11-07 20:25:08 +00:00
|
|
|
io.on('connection', function(socket) {
|
2016-10-19 00:29:21 +00:00
|
|
|
socket.resources = {
|
|
|
|
screen: false,
|
|
|
|
video: true,
|
|
|
|
audio: false
|
2015-09-19 16:02:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 00:29:21 +00:00
|
|
|
// pass a message to another id
|
2016-11-07 20:25:08 +00:00
|
|
|
socket.on('message', function(details) {
|
2016-10-19 00:29:21 +00:00
|
|
|
if (!details) return
|
|
|
|
|
|
|
|
var otherClient = io.to(details.to)
|
|
|
|
if (!otherClient) return
|
|
|
|
|
|
|
|
details.from = socket.id
|
|
|
|
otherClient.emit('message', details)
|
|
|
|
})
|
|
|
|
|
2016-11-07 20:25:08 +00:00
|
|
|
socket.on('shareScreen', function() {
|
2016-10-19 00:29:21 +00:00
|
|
|
socket.resources.screen = true
|
|
|
|
})
|
|
|
|
|
2016-11-07 20:25:08 +00:00
|
|
|
socket.on('unshareScreen', function(type) {
|
2016-10-19 00:29:21 +00:00
|
|
|
socket.resources.screen = false
|
|
|
|
removeFeed('screen')
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('join', join)
|
|
|
|
|
|
|
|
function removeFeed(type) {
|
|
|
|
if (socket.room) {
|
|
|
|
io.sockets.in(socket.room).emit('remove', {
|
|
|
|
id: socket.id,
|
|
|
|
type: type
|
|
|
|
})
|
|
|
|
if (!type) {
|
|
|
|
socket.leave(socket.room)
|
|
|
|
socket.room = undefined
|
2015-09-19 16:02:05 +00:00
|
|
|
}
|
2016-10-19 00:29:21 +00:00
|
|
|
}
|
2015-09-19 16:02:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 00:29:21 +00:00
|
|
|
function join(name, cb) {
|
|
|
|
// sanity check
|
|
|
|
if (typeof name !== 'string') return
|
|
|
|
// leave any existing rooms
|
|
|
|
removeFeed()
|
|
|
|
safeCb(cb)(null, describeRoom(name))
|
|
|
|
socket.join(name)
|
|
|
|
socket.room = name
|
|
|
|
}
|
2015-09-19 16:02:05 +00:00
|
|
|
|
2016-10-19 00:29:21 +00:00
|
|
|
// we don't want to pass "leave" directly because the
|
|
|
|
// event type string of "socket end" gets passed too.
|
2016-11-07 20:25:08 +00:00
|
|
|
socket.on('disconnect', function() {
|
2016-10-19 00:29:21 +00:00
|
|
|
removeFeed()
|
|
|
|
})
|
2016-11-07 20:25:08 +00:00
|
|
|
socket.on('leave', function() {
|
2016-10-19 00:29:21 +00:00
|
|
|
removeFeed()
|
|
|
|
})
|
|
|
|
|
2016-11-07 20:25:08 +00:00
|
|
|
socket.on('create', function(name, cb) {
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
cb = (typeof cb === 'function') ? cb : function() {}
|
2016-10-19 00:29:21 +00:00
|
|
|
name = name || uuid()
|
|
|
|
} else {
|
|
|
|
cb = name
|
|
|
|
name = uuid()
|
|
|
|
}
|
|
|
|
// check if exists
|
|
|
|
var room = io.nsps['/'].adapter.rooms[name]
|
|
|
|
if (room && room.length) {
|
|
|
|
safeCb(cb)('taken')
|
|
|
|
} else {
|
|
|
|
join(name)
|
|
|
|
safeCb(cb)(null, name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// support for logging full webrtc traces to stdout
|
|
|
|
// useful for large-scale error monitoring
|
2016-11-07 20:25:08 +00:00
|
|
|
socket.on('trace', function(data) {
|
2016-10-19 00:29:21 +00:00
|
|
|
console.log('trace', JSON.stringify(
|
|
|
|
[data.type, data.session, data.prefix, data.peer, data.time, data.value]
|
|
|
|
))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function describeRoom(name) {
|
|
|
|
var adapter = io.nsps['/'].adapter
|
|
|
|
var sockets = adapter.rooms[name] || {}
|
|
|
|
var result = {
|
|
|
|
clients: {}
|
|
|
|
}
|
2016-11-07 20:25:08 +00:00
|
|
|
Object.keys(sockets).forEach(function(id) {
|
2016-10-19 00:29:21 +00:00
|
|
|
result.clients[id] = adapter.nsp.connected[id].resources
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|