How can i use js codes in construct and get its result ?

  • Hi , I have some javascript codes to test turn server connection . Is there a way to use it in construct directly and get its final result(yes/no) ?

    Here is a code :

    	function checkTURNServer(turnConfig, timeout){ 
    
     return new Promise(function(resolve, reject){
    
     setTimeout(function(){
     if(promiseResolved) return;
     resolve(false);
     promiseResolved = true;
     }, timeout || 5000);
    
     var promiseResolved = false
     , myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection //compatibility for firefox and chrome
     , pc = new myPeerConnection({iceServers:[turnConfig]})
     , noop = function(){};
     pc.createDataChannel(""); //create a bogus data channel
     pc.createOffer(function(sdp){
     if(sdp.sdp.indexOf('typ relay') > -1){ // sometimes sdp contains the ice candidates...
     promiseResolved = true;
     resolve(true);
     }
     pc.setLocalDescription(sdp, noop, noop);
     }, noop); // create offer and set local description
     pc.onicecandidate = function(ice){ //listen for candidate events
     if(promiseResolved || !ice || !ice.candidate || !ice.candidate.candidate || !(ice.candidate.candidate.indexOf('typ relay')>-1)) return;
     promiseResolved = true;
     resolve(true);
     };
     }); 
    }
    checkTURNServer({
     url: 'turn:127.0.0.1',
     username: 'test',
     credential: 'test'
    }).then(function(bool){
     console.log('is TURN server active? ', bool? 'yes':'no');
    }).catch(console.error.bind(console))
    
    
    
  • Yes, you can call back a function in C2 using this code from JS:

    c2_callFunction("FunctionName", [return_value]);

    .

    Here is a demo:

    dropbox.com/s/f9vte9290eidvm6/JS_Callback.capx

  • Thanks, it works perfectly! but i have one more problem about security .Anyone can access to my js code after export and read my server informations . What should i do ?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can keep these values somewhere else in your C2 project and pass them to JS function as parameters. Something like this:

    Browser execute JavaScript "yourJSFunction('" & serverName & "');"

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)