Docs


Websockets

Ancient Brain Worlds come with Websocket support to allow real-time communication between different users running the same World. User actions in one client can make changes in the World on other clients. This allows you to write your own real-time multi-user games. It also allows real-time chat between the users of your World.

 

AB.socketStart();                  
  // Start socket for this World.
  // Any user who runs the World joins me in the same multi-user World. 

AB.socketStart ( password );
  // Start socket for this World, with password (alphanumeric). 
  // Only users who have called AB.socketStart with the same password can join me
  // (rather than all users who run the World).
 
 
AB.socketOut ( data );             // send any JS data to all other clients 

// Could check socket has been set up first (socket takes time to set up):
if ( AB.socket )
  if ( AB.socket.connected )
    AB.socketOut ( data );
	 
AB.socketIn = function ( data ) { ... };   
  // Handler for incoming data from other clients.
  // World author must define this function.

 
AB.socketUserlist = function ( array ) { ... };
  // Keep track of other users in multi-user World.
  // The server triggers this to tell us the latest list of users of this World. 
  // This is called when we connect, and any time another user arrives or leaves.
  // The array is a list of (userid, username) pairs. 
  // World author must define this function (what to do with array) if desired.