Skip to content

Instantly share code, notes, and snippets.

@keithnorm
Created August 3, 2013 19:24
Show Gist options
  • Save keithnorm/6147655 to your computer and use it in GitHub Desktop.
Save keithnorm/6147655 to your computer and use it in GitHub Desktop.
diff --git a/public/js/client.js b/public/js/client.js
index feb581f..95bcd9b 100644
--- a/public/js/client.js
+++ b/public/js/client.js
@@ -8,6 +8,7 @@ var stream;
var room_count = 0;
var pc = new webkitRTCPeerConnection(configuration);
var connected = false;
+var shouldInitConnection = false;
var mediaConstraints = {
'mandatory': {
'OfferToReceiveAudio':true,
@@ -34,8 +35,8 @@ socket.on('received_offer', function(data) {
});
socket.on('received_answer', function(data) {
- data = JSON.parse(data);
- console.log('received answer');
+ // data is coming through as a JS object already
+ console.log('received answer', data);
if(!connected) {
pc.setRemoteDescription(new RTCSessionDescription(data));
connected = true;
@@ -43,8 +44,8 @@ socket.on('received_answer', function(data) {
});
socket.on('received_candidate', function(data) {
+ console.log('received candidate', data);
data = JSON.parse(data);
- console.log('received candidate');
var candidate = new RTCIceCandidate({
sdpMLineIndex: data.label,
candidate: data.candidate
@@ -54,8 +55,9 @@ socket.on('received_candidate', function(data) {
socket.on('room_count', function(data) {
room_count = data;
+ console.log('ROOM COUNT', data);
if (room_count > 1)
- start();
+ shouldInitConnection = true;
});
pc.onicecandidate = function(e) {
@@ -79,13 +81,16 @@ function broadcast() {
navigator.webkitGetUserMedia({audio: true, video: true}, function(s) {
stream = s;
pc.addStream(s);
+ console.log("GOT MEDIA");
vid1.src = webkitURL.createObjectURL(s);
vid1.play();
+ if (shouldInitConnection) start();
// initCall is set in views/index and is based on if there is another person in the room to connect to
});
};
function start() {
+ console.log('STARTING');
// this initializes the peer connection
pc.createOffer(function(description) {
console.log(description);
diff --git a/server.js b/server.js
index 4465b85..8d75cd9 100644
--- a/server.js
+++ b/server.js
@@ -14,6 +14,7 @@ app.get('/:id?', function (req, res) {
io.sockets.on('connection', function(socket) {
var room = socket.handshake.headers.referer;
+ console.log('JOINED', room);
socket.join(room);
socket.id = uuid.v1();
@@ -22,27 +23,27 @@ io.sockets.on('connection', function(socket) {
socket.on('debug_clients', function(data) {
socket.emit('room_count', io.sockets.manager.rooms['/' + room].length);
});
-
- socket.in('/' + room).emit('room_count', io.sockets.manager.rooms['/' + room].length);
+
+ io.sockets.in(room).emit('room_count', io.sockets.manager.rooms['/' + room].length);
socket.on('received_offer', function(data) {
console.log(" received_offer %j", data);
- socket.emit('received_offer', data);
+ io.sockets.in(room).emit('received_offer', data);
});
socket.on('received_candidate', function(data) {
console.log(" received_candidate %j", data);
- socket.emit('received_candidate', data);
+ io.sockets.in(room).emit('received_candidate', data);
});
socket.on('received_answer', function(data) {
console.log(" received_answer %j", data);
- socket.emit('received_answer', data);
+ io.sockets.in(room).emit('received_answer', data);
});
socket.on('close', function(data) {
console.log("closed %j", data);
- socket.emit('closed', data);
+ io.sockets.in(room).emit('closed', data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment