-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
824b6fb
commit 6bb273b
Showing
4 changed files
with
195 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
### Example user template template | ||
### Example user template | ||
|
||
# IntelliJ project files | ||
.idea | ||
*.iml | ||
out | ||
gen | ||
# Created by .ignore support plugin (hsz.mobi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Created by ghostmac on 3/3/16. | ||
*/ | ||
|
||
module.exports = exports = IoServer; | ||
|
||
function IoServer(http) { | ||
var io = require('socket.io').listen(http, { | ||
log: false, | ||
origins: '*:*' | ||
}); | ||
|
||
io.set('transports', [ | ||
'websocket', // 'disconnect' EVENT will work only with 'websocket' | ||
'xhr-polling', | ||
'jsonp-polling' | ||
]); | ||
|
||
var listOfBroadcasts = {}; | ||
|
||
io.on('connection', function(socket) { | ||
var currentUser; | ||
var sessionId = uuid.v4() | ||
|
||
socket.on('join-broadcast', function(user) { | ||
currentUser = user; | ||
|
||
user.numberOfViewers = 0; | ||
if (!listOfBroadcasts[user.broadcastid]) { | ||
listOfBroadcasts[user.broadcastid] = { | ||
broadcasters: {}, | ||
allusers: {}, | ||
typeOfStreams: user.typeOfStreams // object-booleans: audio, video, screen | ||
}; | ||
} | ||
|
||
var firstAvailableBroadcaster = getFirstAvailableBraodcater(user); | ||
if (firstAvailableBroadcaster) { | ||
listOfBroadcasts[user.broadcastid].broadcasters[firstAvailableBroadcaster.userid].numberOfViewers++; | ||
socket.emit('join-broadcaster', firstAvailableBroadcaster, listOfBroadcasts[user.broadcastid].typeOfStreams); | ||
|
||
console.log('User <', user.userid, '> is trying to get stream from user <', firstAvailableBroadcaster.userid, '>'); | ||
} else { | ||
currentUser.isInitiator = true; | ||
socket.emit('start-broadcasting', listOfBroadcasts[user.broadcastid].typeOfStreams); | ||
|
||
console.log('User <', user.userid, '> will be next to serve broadcast.'); | ||
} | ||
|
||
listOfBroadcasts[user.broadcastid].broadcasters[user.userid] = user; | ||
listOfBroadcasts[user.broadcastid].allusers[user.userid] = user; | ||
}); | ||
|
||
socket.on('message', function(message) { | ||
socket.broadcast.emit('message', message); | ||
}); | ||
|
||
socket.on('disconnect', function() { | ||
if (!currentUser) return; | ||
if (!listOfBroadcasts[currentUser.broadcastid]) return; | ||
if (!listOfBroadcasts[currentUser.broadcastid].broadcasters[currentUser.userid]) return; | ||
|
||
delete listOfBroadcasts[currentUser.broadcastid].broadcasters[currentUser.userid]; | ||
if (currentUser.isInitiator) { | ||
delete listOfBroadcasts[currentUser.broadcastid]; | ||
} | ||
}); | ||
//console.log(util.inspect(socket)) | ||
console.log('connected to ws server - session: %s', sessionId); | ||
socket.on('error', function (error) { | ||
console.log('Error: ' + error); | ||
}); | ||
|
||
socket.on('close', function () { | ||
console.log('Connection closing: ' + sessionId + ' closed'); | ||
}); | ||
|
||
socket.on('message', function (_message) { | ||
var message = JSON.parse(_message); | ||
console.log('Connection ' + sessionId + ' received message ', message); | ||
var jsonRpc = message | ||
|
||
//roomServer.roomHandler.handleRequest() | ||
|
||
}) | ||
}); | ||
|
||
function getFirstAvailableBraodcater(user) { | ||
var broadcasters = listOfBroadcasts[user.broadcastid].broadcasters; | ||
var firstResult; | ||
for (var userid in broadcasters) { | ||
if (broadcasters[userid].numberOfViewers <= 3) { | ||
firstResult = broadcasters[userid]; | ||
continue; | ||
} else delete listOfBroadcasts[user.broadcastid].broadcasters[userid]; | ||
} | ||
return firstResult; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters