JS viewer

Syntax-highlighted view of JS of World: Websocket buttons

// ==== 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.
Click button to  set background color  in all runs on all devices:

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

` );

// start socket for this World


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

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

function setGrey()
  document.body.style.background = 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;