client/webrtc.js
// create a function to classify and respond to messages received from the server

create a function to classify and respond to messages received from the server
create a function to classify and respond to messages received from the server
function gotMessageFromServer(message) {
  var signal = JSON.parse(message.data); 
  var peerUuid = signal.uuid; 
  console.log(signal);
  if(signal.raiseHand == true){
    var answer = window.confirm("Student is trying to ask question ? You want to add him ?"); 
    if(answer){
      raiseHand=1; 
      closePeer(peerUuid);
    }else{
      signal = undefined
    }
  }
  if((signal.ID != undefined)&&(time > tThreshold)){
    lowID = signal.ID;
  }
  if(signal.reform != undefined){
    if(signal.reform == "1"){
      serverConnection.send(JSON.stringify({"reform": "2", "nextPeer": signal.updateDHT, "uuid" : String(DHT[1]), "time": time }));
      DHT[3]=signal.updateDHT
    }else if(signal.reform == "2"){
      DHT[1]=DHT[0]
      DHT[0]=signal.updateDHT
    }else if(signal.reform == "3"){
      DHT[0] = signal.updateDHT
    }
  }
  if(signal.churn == true){
    count=1;
    if(DHT==undefined){
      serverConnection.send(JSON.stringify({ 'displayName': localDisplayName, 'uuid': localUuid, 'dest1': String(localDHT[0]), "time": time }));
    }else{
    serverConnection.send(JSON.stringify({ 'displayName': localDisplayName, 'uuid': localUuid, 'dest1': String(DHT[1]), "time": time }));
    }
  }
  if(signal.DHT == "DHT"){
    DHT = signal.valueDHT;
  }
  if(signal.first!=undefined){ 
  flag = signal.first  
  }
  if (signal.first == 0){ 
    setUpPeer(peerUuid, signal.displayName, false, flag)
    return
  }
  if (peerUuid == localUuid || (signal.dest != localUuid)&&(signal.dest!= "all" )) return;
  if (signal.displayName && signal.dest=="all") {
    setUpPeer(peerUuid, signal.displayName); 
    if(signal.ID == undefined){
      serverConnection.send(JSON.stringify({ 'displayName': localDisplayName, 'uuid': localUuid, 'dest': peerUuid, "time": time })); 
    }else{
      serverConnection.send(JSON.stringify({ 'displayName': localDisplayName, 'uuid': localUuid, 'dest': peerUuid, "time": time , "ID": signal.ID})); 
    }
  } else if (signal.displayName && signal.dest == localUuid) {
    if(DHT == undefined){
      localDHT.push(signal.uuid);}
      setUpPeer(peerUuid, signal.displayName, true);
  } else if (signal.sdp) { 
    peerConnections[peerUuid].pc.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function () {
      if (signal.sdp.type == 'offer') {
        peerConnections[peerUuid].pc.createAnswer().then(description => createdDescription(description, peerUuid)).catch(errorHandler);
      }
    }).catch(errorHandler);
  } else if (signal.ice) { 
    peerConnections[peerUuid].pc.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
  }
}
function gotMessageFromServer ( message ) { var signal = JSON . parse ( message . data ) ; var peerUuid = signal . uuid ; console . log ( signal ) ; if ( signal . raiseHand == true ) { var answer = window . confirm ( "Student is trying to ask question ? You want to add him ?" ) ; if ( answer ) { raiseHand = 1 ; closePeer ( peerUuid ) ; } else { signal = undefined } } if ( ( signal . ID != undefined ) && ( time > tThreshold ) ) { lowID = signal . ID ; } if ( signal . reform != undefined ) { if ( signal . reform == "1" ) { serverConnection . send ( JSON . stringify ( { "reform" : "2" , "nextPeer" : signal . updateDHT , "uuid" : String ( DHT [ 1 ] ) , "time" : time } ) ) ; DHT [ 3 ] = signal . updateDHT } else if ( signal . reform == "2" ) { DHT [ 1 ] = DHT [ 0 ] DHT [ 0 ] = signal . updateDHT } else if ( signal . reform == "3" ) { DHT [ 0 ] = signal . updateDHT } } if ( signal . churn == true ) { count = 1 ; if ( DHT == undefined ) { serverConnection . send ( JSON . stringify ( { 'displayName' : localDisplayName , 'uuid' : localUuid , 'dest1' : String ( localDHT [ 0 ] ) , "time" : time } ) ) ; } else { serverConnection . send ( JSON . stringify ( { 'displayName' : localDisplayName , 'uuid' : localUuid , 'dest1' : String ( DHT [ 1 ] ) , "time" : time } ) ) ; } } if ( signal . DHT == "DHT" ) { DHT = signal . valueDHT ; } if ( signal . first != undefined ) { flag = signal . first } if ( signal . first == 0 ) { setUpPeer ( peerUuid , signal . displayName , false , flag ) return } if ( peerUuid == localUuid || ( signal . dest != localUuid ) && ( signal . dest != "all" ) ) return ; if ( signal . displayName && signal . dest == "all" ) { setUpPeer ( peerUuid , signal . displayName ) ; if ( signal . ID == undefined ) { serverConnection . send ( JSON . stringify ( { 'displayName' : localDisplayName , 'uuid' : localUuid , 'dest' : peerUuid , "time" : time } ) ) ; } else { serverConnection . send ( JSON . stringify ( { 'displayName' : localDisplayName , 'uuid' : localUuid , 'dest' : peerUuid , "time" : time , "ID" : signal . ID } ) ) ; } } else if ( signal . displayName && signal . dest == localUuid ) { if ( DHT == undefined ) { localDHT . push ( signal . uuid ) ; } setUpPeer ( peerUuid , signal . displayName , true ) ; } else if ( signal . sdp ) { peerConnections [ peerUuid ] . pc . setRemoteDescription ( new RTCSessionDescription ( signal . sdp ) ) . then ( function ( ) { if ( signal . sdp . type == 'offer' ) { peerConnections [ peerUuid ] . pc . createAnswer ( ) . then ( description => createdDescription ( description , peerUuid ) ) . catch ( errorHandler ) ; } } ) . catch ( errorHandler ) ; } else if ( signal . ice ) { peerConnections [ peerUuid ] . pc . addIceCandidate ( new RTCIceCandidate ( signal . ice ) ) . catch ( errorHandler ) ; } }
create a function to classify and respond to messages received from the server
create a function to classify and respond to messages received from the server
//convert the JSON string into an object.
 //for master to entertain raise hand requests
 //know the index of high bandwith client in the chain and set the index for low bandwidth clients
 //updating DHT after churn
 //a peer churn prompts clients to reconnect through exchange of offer/answer
 //if peer didn't get a DHT i.e. peer is the last one in the connection
 //for peers having a complete DHT 
 //if peer gets a DHT from the server
 //if peer is the first client
 // set up peer connection object for the master
 // ignore messages that are not for us or from ourselves 
 // set up peer connection object for a newcomer peer
 //send localDisplayName and localUuid to peerUuid
 //define a localDHT for the last peer
 // initiate call if we are the newcomer peer
 // Only create answers in response to offers
 //create a new ice candidate and add to the specific peerUuid
