Code viewer for World: Experiment - Invite only W...

// Websocket world in which only certain users can join

// runs from the World homepage have AB.myuserid set up
// owner of World edits first line to show who can join 
// no passwords needed


// restrict to these users
const allowedusers = [ "test2", "stewalsh", "cadogav2" ];


if ( ! allowedusers.includes ( AB.myuserid ) ) 
{
    console.log ("You are not invited to this World");      // in case close does not work
    AB.closeMe();                                           // try to close window - sometimes JS cannot close window
}
else
{
    AB.socketStart();       // socket only exists if invited 
}



// problem: just construct URL to write AB.myuserid:
// https://run.ancientbrain.com/run.php?world=8909561677&userid=stewalsh

// runs are just:
// open_run_window ( URL );
// user can see URL needed to join and can fake it

// solution for "save data" is dataticket - cannot be faked 
// "save data" calls Ajax PHP that checks dataticket
// but AB.socket.emit does not call PHP
// AB socket functions only talk to Node 




// ==== Starter World =================================================================================================
// This code is designed for use on the Ancient Brain site.
// This code may be freely copied and edited by anyone on the Ancient Brain site.
// To include a working run of this program on another site, see the "Embed code" links provided on Ancient Brain.
// ====================================================================================================================



// Demo of Ancient Brain Websockets functionality. 
// Click button and it changes colour in all clients. 

const yellow 	= "#ffffcc";
const grey		= "#eeeeee";


// default body is margin 0 and padding 0 

  $('body').css( "margin", "20px" );
  $('body').css( "padding", "20px" );

 
document.write ( `

<h1> Websockets test </h1>

Run this on two different devices.
<p>
Click button to  set background color  in all runs on all devices:
<p>

 <button onclick="setYellow();"   class=ab-largenormbutton > Yellow </button> 
 <button onclick="setGrey();"     class=ab-largenormbutton > Grey </button> 

` );





// --- When I click, set the color and send info to server ----------------------------------------------------------------

function setYellow()
{
  document.body.style.background = yellow;
  sendtoServer(yellow);
}

function setGrey()
{
  document.body.style.background = grey;
  sendtoServer(grey);
}

function sendtoServer ( thecolor )
{  
  var data = 
  {
   color: thecolor
  };
  
  AB.socketOut ( data );        // server gets this, and sends the data to all clients running this World
}



// --- When someone else clicks, server will send me a message and I can set the color -------------------------------------
// define what to do when we get incoming data on the socket: 

AB.socketIn = function(data)
{
    document.body.style.background = data.color;
};