µ¿ÀÛ ¿Ϸá

This commit is contained in:
2016-02-09 20:08:31 +09:00
parent 04cda46062
commit 1f132c53a8
3 changed files with 36 additions and 105 deletions

View File

@@ -28,20 +28,22 @@ function pageReady() {
} }
function getUserMediaSuccess(stream) { function getUserMediaSuccess(stream) {
console.log('['+(new Date().toLocaleString())+'] ' + '[getUserMediaSuccess] ' + stream);
localStream = stream; localStream = stream;
localVideo.src = window.URL.createObjectURL(stream); localVideo.src = window.URL.createObjectURL(stream);
} }
function start(isCaller) { function start(isCaller) {
console.log('['+(new Date().toLocaleString())+'] ' + "[start] caller? " + isCaller);
peerConnection = new RTCPeerConnection(peerConnectionConfig); peerConnection = new RTCPeerConnection(peerConnectionConfig);
peerConnection.onicecandidate = gotIceCandidate; peerConnection.onicecandidate = gotIceCandidate;
peerConnection.onaddstream = gotRemoteStream; peerConnection.onaddstream = gotRemoteStream;
peerConnection.addStream(localStream); peerConnection.addStream(localStream);
if(isCaller) { if(isCaller)
peerConnection.createOffer(gotDescription, errorHandler); peerConnection.createOffer(gotDescription, errorHandler);
} }
}
function gotMessageFromServer(message) { function gotMessageFromServer(message) {
if(!peerConnection) start(false); if(!peerConnection) start(false);
@@ -57,23 +59,25 @@ function gotMessageFromServer(message) {
} }
function gotIceCandidate(event) { function gotIceCandidate(event) {
console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event);
if(event.candidate != null) { if(event.candidate != null) {
console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event.candidate);
serverConnection.send(JSON.stringify({'ice': event.candidate})); serverConnection.send(JSON.stringify({'ice': event.candidate}));
} }
} }
function gotDescription(description) { function gotDescription(description) {
console.log('got description'); console.log('['+(new Date().toLocaleString())+'] ' + '[gotDescription] got description');
peerConnection.setLocalDescription(description, function () { peerConnection.setLocalDescription(description, function () {
serverConnection.send(JSON.stringify({'sdp': description})); serverConnection.send(JSON.stringify({'sdp': description}));
}, function() {console.log('set description error')}); }, function() {console.log('['+(new Date().toLocaleString())+'] ' + '[gotDescription] set description error')});
} }
function gotRemoteStream(event) { function gotRemoteStream(event) {
console.log('got remote stream'); console.log('['+(new Date().toLocaleString())+'] ' + '[gotRemoteStream] got remote stream');
remoteVideo.src = window.URL.createObjectURL(event.stream); remoteVideo.src = window.URL.createObjectURL(event.stream);
} }
function errorHandler(error) { function errorHandler(error) {
console.log(error); console.log('['+(new Date().toLocaleString())+'] ' + "[errorHandler] " + error);
} }

View File

@@ -1,62 +1,3 @@
var max_print_r_depth = 2
function print_r(obj, t, d) {
// define tab spacing
var tab = t || '';
// define depth
var depth = d || 1;
// check if it's array
var isArr = Object.prototype.toString.call(obj) === '[object Array]';
// use {} for object, [] for array
var str = isArr ? ('Array\n' + tab + '[ \n') : ('Object\n' + tab + '{ \n');
// walk through it's properties
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
var val1 = obj[prop];
var val2 = '';
var type = Object.prototype.toString.call(val1);
switch (type) {
// recursive if object/array
case '[object Array]':
case '[object Object]':
if(depth <= max_print_r_depth)
val2 = print_r(val1, (tab + ' '), depth+1);
else
val2 = type;
break;
case '[object String]':
if(val1.length > 40)
val2 = '\'' + val1.substring(0, 40) + '\'';
else
val2 = '\'' + val1 + '\'';
break;
case '[object Function]':
case '[object Uint8Array]':
val2 = type;
break;
default:
val2 = val1;
}
str += tab + ' ' + prop + ' => ' + val2 + ',\n';
}
}
// remove extra comma for last property
str = str.substring(0, str.length - 2) + '\n' + tab;
return isArr ? (str + ']') : (str + '}');
}
var localVideo; var localVideo;
var remoteVideo; var remoteVideo;
var peerConnection; var peerConnection;
@@ -90,19 +31,19 @@ function pageReady(ch) {
} }
function onConnection(data) { function onConnection(data) {
console.log('['+(new Date().toLocaleString())+'] ' + '[onConnect]' + data.toString()); // console.log('['+(new Date().toLocaleString())+'] ' + '[onConnect]' + data.toString());
serverConnection.send( JSON.stringify({ channel: channel }) ); serverConnection.send( JSON.stringify({ 'channel': channel }) );
} }
function getUserMediaSuccess(stream) { function getUserMediaSuccess(stream) {
console.log('['+(new Date().toLocaleString())+'] ' + '[getUserMediaSuccess] ' + stream); // console.log('['+(new Date().toLocaleString())+'] ' + '[getUserMediaSuccess] ' + stream);
localStream = stream; localStream = stream;
localVideo.src = window.URL.createObjectURL(stream); localVideo.src = window.URL.createObjectURL(stream);
} }
function start(isCaller) { function start(isCaller) {
console.log('['+(new Date().toLocaleString())+'] ' + "[start] caller? " + isCaller); // console.log('['+(new Date().toLocaleString())+'] ' + "[start] caller? " + isCaller);
peerConnection = new RTCPeerConnection(peerConnectionConfig); peerConnection = new RTCPeerConnection(peerConnectionConfig);
peerConnection.onicecandidate = gotIceCandidate; peerConnection.onicecandidate = gotIceCandidate;
peerConnection.onaddstream = gotRemoteStream; peerConnection.onaddstream = gotRemoteStream;
@@ -111,8 +52,6 @@ function start(isCaller) {
if(isCaller) if(isCaller)
peerConnection.createOffer(gotDescription, errorHandler); peerConnection.createOffer(gotDescription, errorHandler);
else
serverConnection.send( JSON.stringify({ channel: channel }) );
} }
function gotMessageFromServer(message) { function gotMessageFromServer(message) {
@@ -127,18 +66,14 @@ function gotMessageFromServer(message) {
peerConnection.createAnswer(gotDescription, errorHandler); peerConnection.createAnswer(gotDescription, errorHandler);
}, errorHandler); }, errorHandler);
} else if(signal.ice) { } else if(signal.ice) {
console.log(signal.ice.candidate) peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice));
var iceCandidate = new RTCIceCandidate(signal.ice);
console.log('test2');
peerConnection.addIceCandidate(iceCandidate);
console.log('test3')
} }
} }
function gotIceCandidate(event) { function gotIceCandidate(event) {
console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event); // console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event);
if(event.candidate != null) { if(event.candidate != null) {
console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event.candidate); // console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event.candidate);
serverConnection.send(JSON.stringify({'ice': event.candidate})); serverConnection.send(JSON.stringify({'ice': event.candidate}));
} }
} }

View File

@@ -1,22 +1,21 @@
var utils = require('./utils'); var utils = require('./utils');
var WebSocketServer = require('ws').Server; var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({port: 3434}); var wss = new WebSocketServer({port: 3434});
wss.broadcast = function(data, channel) { wss.broadcast = function(data, channel) {
// if(!channel) if(!channel)
// { {
// console.log('empty channel. skipped.'); console.log('empty channel. skipped.');
// return; return;
// } }
for(var i in this.clients) { for(var i in this.clients) {
// if(this.clients[i].channel == channel)
{
console.log('channel1 : ' + this.clients[i].channel);
console.log('channel2 : ' + channel);
console.log('channel : ' + channel + 'client\'s channel : ' + this.clients[i].channel);
if(this.clients[i].channel == channel)
{
console.log('['+(new Date().toLocaleString())+'] ' + 'send : [' + channel +'] '+ data); console.log('['+(new Date().toLocaleString())+'] ' + 'send : [' + channel +'] '+ data);
this.clients[i].send(data); this.clients[i].send(data);
} }
@@ -25,24 +24,17 @@ wss.broadcast = function(data, channel) {
wss.on('connection', function(ws) { wss.on('connection', function(ws) {
ws.on('message', function(message) { ws.on('message', function(message) {
console.log('['+(new Date().toLocaleString())+'] ' + 'received: %s\n', message); // console.log('['+(new Date().toLocaleString())+'] ' + 'received: %s\n', message);
try {
var obj = JSON.parse(message);
utils.ObjDump(obj);
if(obj.channel != null)
{
var channel = obj.channel;
// if(wss.channel == null) if(typeof message == "string") {
// wss.channel = new Object(); // console.log('string message : ' + message);
// if(wss.channel[channel] == null)
// wss.channel[channel] = new Array(); var obj = JSON.parse(message);
// wss.channel[channel].push(ws); // utils.ObjDump(obj);
ws.channel = channel; if(obj.channel) {
console.log('new channel : ' + ws.channel); ws.channel = obj.channel;
return;
} }
} catch (ex) {
console.log('['+(new Date().toLocaleString())+'] ' + ex);
} }
wss.broadcast(message, ws.channel); wss.broadcast(message, ws.channel);