Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement roulette

  • Loading branch information...
commit aa112e7973c31256853abf6b63790d11a7b5eceb 1 parent 096d274
@jeromeetienne authored
Showing with 62 additions and 8 deletions.
  1. +3 −0  Makefile
  2. +31 −4 index.html
  3. +28 −4 lib/server.js
View
3  Makefile
@@ -1,6 +1,9 @@
# makefile to automatize simple operations
server:
+ forever -w --watchDirectory lib lib/server.js
+
+serverProd:
npm start
deploy:
View
35 index.html
@@ -6,9 +6,12 @@
<body>
<div style="position: absolute; font-size: 200%; font-weight: bolder; right: 1em; bottom: 0.5em;" id='uiContainer'>
<form action="javascript:void(0)" id='roomNameForm' style='display: inline-block'>
+ <div style='float: right'>
+ <input type='button' class='butRoulette' value='roulette'/>
+ <input type='button' class='butRandom' value='random' />
+ </div>
<span>ROOM :</span>
<input type="text" class='roomName' size='10' style='font-size: inherit;'/>
- <input type='button' class='butRandom' value='random'/>
</form>
</div><div id="info" style='color: white'>
WebGL Video Chat
@@ -98,7 +101,11 @@
// set castShadow
tQuery('mesh', tvSetModel).castShadow(true)
// notify everybody
- onModelLoaded();
+ if( location.hash === '#roulette'){
+ onModelLoadedRoulette();
+ }else{
+ onModelLoaded();
+ }
});
@@ -157,19 +164,39 @@
location.hash = '#' + roomName;
location.reload();
});
+ document.querySelector('#roomNameForm .butRoulette').addEventListener('click', function(){
+ location.hash = '#roulette';
+ location.reload();
+ });
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
+ function onModelLoadedRoulette(){
+ var request = new XMLHttpRequest();
+ request.onload = function(){
+ var response = JSON.parse(this.responseText)
+ roomName = response.roomName;
+ // set the input value
+ document.querySelector('#roomNameForm .roomName').value = roomName;
+ onModelLoaded();
+ }
+ var url = '/rouletteRoomName?'+Math.floor((Math.random()) * 0x10000).toString(16)
+ request.open("GET", url, true);
+ request.send();
+ }
+
function onModelLoaded(){
// connect the server
var serverUrl = 'ws://' + location.host + location.pathname;
rtc.connect(serverUrl, roomName);
+ // log to debug
+ console.log('connecting to server', serverUrl, 'in room', roomName)
// honor nostream
if( nostream ) return;
// create a local stream
- rtc.createStream({'video': true, 'audio': true}, function(mediaStream){
+ rtc.createStream({'video': true, 'audio': false}, function(mediaStream){
console.log('created stream', arguments)
createScreen(mediaStream)
reflowScreens();
@@ -177,7 +204,7 @@
console.log('createStream failed', arguments)
});
}
-
+
/**
* adapt screens layout depending on their numbers
*/
View
32 lib/server.js
@@ -1,15 +1,39 @@
-var express = require('express');
-var app = express()
-var server = require('http').createServer(app);
-var webRTC = require('webrtc.io').listen(server);
+var express = require('express');
+var app = express()
+var server = require('http').createServer(app);
+var webRTC = require('webrtc.io').listen(server);
var port = process.env.PORT || 8000;
server.listen(port);
+console.log('listen on http://0.0.0.0:8000')
// export static files
app.use('/', express.static(__dirname + '/../'));
+app.get('/rooms', function(req, res) {
+ var rooms = webRTC.rtc.rooms
+ res.send(rooms)
+})
+
+app.get('/rouletteRoomName', function(req, res) {
+ var rooms = webRTC.rtc.rooms
+ var roomNames = Object.keys(rooms).filter(function(roomName){
+ return roomName.match(/^lette-/);
+ });
+ // try to find a room with only one participant
+ for(var i = 0; i < roomNames.length; i++){
+ var roomName = roomNames[i]
+ var room = rooms[roomName]
+ if( room.length === 1 ){
+ res.send({roomName:roomName})
+ return;
+ }
+ }
+ // pick a random name
+ var roomName = 'lette-' + Math.floor((Math.random()) * 0x10000).toString(16);
+ res.send({roomName:roomName})
+});
webRTC.rtc.on('connect', function(rtc) {
//Client connected
Please sign in to comment.
Something went wrong with that request. Please try again.