From 1f132c53a89c52839bdebce973a0881d3830977a Mon Sep 17 00:00:00 2001 From: mjjo Date: Tue, 9 Feb 2016 20:08:31 +0900 Subject: [PATCH] =?UTF-8?q?=C2=B5=C2=BF=C3=80=C3=9B=20=C2=BF=CF=B7=C3=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/webrtc-org.js | 16 +++++---- client/webrtc.js | 79 ++++---------------------------------------- server/server.js | 46 +++++++++++--------------- 3 files changed, 36 insertions(+), 105 deletions(-) diff --git a/client/webrtc-org.js b/client/webrtc-org.js index 6d24469..b18b719 100644 --- a/client/webrtc-org.js +++ b/client/webrtc-org.js @@ -28,19 +28,21 @@ function pageReady() { } function getUserMediaSuccess(stream) { + console.log('['+(new Date().toLocaleString())+'] ' + '[getUserMediaSuccess] ' + stream); + localStream = stream; localVideo.src = window.URL.createObjectURL(stream); } function start(isCaller) { + console.log('['+(new Date().toLocaleString())+'] ' + "[start] caller? " + isCaller); peerConnection = new RTCPeerConnection(peerConnectionConfig); peerConnection.onicecandidate = gotIceCandidate; peerConnection.onaddstream = gotRemoteStream; peerConnection.addStream(localStream); - if(isCaller) { + if(isCaller) peerConnection.createOffer(gotDescription, errorHandler); - } } function gotMessageFromServer(message) { @@ -57,23 +59,25 @@ function gotMessageFromServer(message) { } function gotIceCandidate(event) { + console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event); if(event.candidate != null) { + console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event.candidate); serverConnection.send(JSON.stringify({'ice': event.candidate})); } } function gotDescription(description) { - console.log('got description'); + console.log('['+(new Date().toLocaleString())+'] ' + '[gotDescription] got description'); peerConnection.setLocalDescription(description, function () { 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) { - console.log('got remote stream'); + console.log('['+(new Date().toLocaleString())+'] ' + '[gotRemoteStream] got remote stream'); remoteVideo.src = window.URL.createObjectURL(event.stream); } function errorHandler(error) { - console.log(error); + console.log('['+(new Date().toLocaleString())+'] ' + "[errorHandler] " + error); } diff --git a/client/webrtc.js b/client/webrtc.js index 5732003..34d8a08 100644 --- a/client/webrtc.js +++ b/client/webrtc.js @@ -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 remoteVideo; var peerConnection; @@ -90,19 +31,19 @@ function pageReady(ch) { } function onConnection(data) { - console.log('['+(new Date().toLocaleString())+'] ' + '[onConnect]' + data.toString()); - serverConnection.send( JSON.stringify({ channel: channel }) ); + // console.log('['+(new Date().toLocaleString())+'] ' + '[onConnect]' + data.toString()); + serverConnection.send( JSON.stringify({ 'channel': channel }) ); } function getUserMediaSuccess(stream) { - console.log('['+(new Date().toLocaleString())+'] ' + '[getUserMediaSuccess] ' + stream); + // console.log('['+(new Date().toLocaleString())+'] ' + '[getUserMediaSuccess] ' + stream); localStream = stream; localVideo.src = window.URL.createObjectURL(stream); } function start(isCaller) { - console.log('['+(new Date().toLocaleString())+'] ' + "[start] caller? " + isCaller); + // console.log('['+(new Date().toLocaleString())+'] ' + "[start] caller? " + isCaller); peerConnection = new RTCPeerConnection(peerConnectionConfig); peerConnection.onicecandidate = gotIceCandidate; peerConnection.onaddstream = gotRemoteStream; @@ -111,8 +52,6 @@ function start(isCaller) { if(isCaller) peerConnection.createOffer(gotDescription, errorHandler); - else - serverConnection.send( JSON.stringify({ channel: channel }) ); } function gotMessageFromServer(message) { @@ -127,18 +66,14 @@ function gotMessageFromServer(message) { peerConnection.createAnswer(gotDescription, errorHandler); }, errorHandler); } else if(signal.ice) { - console.log(signal.ice.candidate) - var iceCandidate = new RTCIceCandidate(signal.ice); - console.log('test2'); - peerConnection.addIceCandidate(iceCandidate); - console.log('test3') + peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)); } } function gotIceCandidate(event) { - console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event); + // console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event); 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})); } } diff --git a/server/server.js b/server/server.js index 25a8795..a1b5809 100644 --- a/server/server.js +++ b/server/server.js @@ -1,22 +1,21 @@ var utils = require('./utils'); var WebSocketServer = require('ws').Server; - var wss = new WebSocketServer({port: 3434}); wss.broadcast = function(data, channel) { - // if(!channel) - // { - // console.log('empty channel. skipped.'); - // return; - // } + if(!channel) + { + console.log('empty channel. skipped.'); + return; + } 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); this.clients[i].send(data); } @@ -25,24 +24,17 @@ wss.broadcast = function(data, channel) { wss.on('connection', function(ws) { ws.on('message', function(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; + // console.log('['+(new Date().toLocaleString())+'] ' + 'received: %s\n', message); - // if(wss.channel == null) - // wss.channel = new Object(); - // if(wss.channel[channel] == null) - // wss.channel[channel] = new Array(); - // wss.channel[channel].push(ws); - ws.channel = channel; - console.log('new channel : ' + ws.channel); - } - } catch (ex) { - console.log('['+(new Date().toLocaleString())+'] ' + ex); + if(typeof message == "string") { + // console.log('string message : ' + message); + + var obj = JSON.parse(message); + // utils.ObjDump(obj); + if(obj.channel) { + ws.channel = obj.channel; + return; + } } wss.broadcast(message, ws.channel);