Class: Player

Player

This player class represents a device connected to a session. Every player will be mirrored to the server and all connected devices. This class can be used the same way on server and client side.

<protected> new Player(id, messageBus)

Parameters:
Name Type Description
id string unique identifier of this player
messageBus module:client/messages~MessageBus | module:server/messages~MessageBus message bus instance this player should use to communicate
Source:
Fires:

Mixes In

Members

attributes :object

Object with user attributes for this player. All changes within this object will automatically be synced to all other clients.
Listen for changes by subscribing to the attributesChanged event.
Type:
  • object
Source:

<readonly> height :integer

pixel height of this clients screen
Type:
  • integer
Source:

<readonly> id :string

unique id for this player
Type:
  • string
Source:

<readonly> number :integer

Unique player-number inside this session beginning with 0. Free numbers from disconnected players will be reused to avoid gaps.
Type:
  • integer
Source:

<readonly> width :integer

pixel width of this clients screen
Type:
  • integer
Source:

Methods

disconnect()

Disconnect the client represented by this player from the framework. Due to security reasons this will only work with the player that represents this client (session.myself) and fail silently on all others.
Source:
Fires:

getAttributeAsync(name, timeout) → {external:Promise}

Get the value of a specific attributes field. If the value is not present yet, it will be passed to the returned promise later on. This should make handling async code a bit easier.
This method is especially useful for attributes that are set just once right after the player joined a session but need a bit of time to sync to all clients, eg. player color, name, etc.
Parameters:
Name Type Argument Default Description
name string name of the attribute whose value you want to know
timeout integer <optional>
1000 time in milliseconds after which the returned promise will be rejected, if the attribute is not present
Source:
Returns:
On success the promise will be resolved with the value of the requested attribute. Has the attribute not been available after the given timout, the promise will be rejected with a generic error.
Type
external:Promise
Example
session.on('playerJoined', function (event) {
  event.player.getAttributeAsync('foo').then(function (value) {
    console.log(value); // will be '#ff0000'
  });
});

// on another client:
player.attributes.color = '#ff0000';

isFirst() → {boolean}

Determines if this player is the first one inside its session (has player number 0). Use this method to find out if this player has opened the session or has joined later on.
Source:
Returns:
true if the player number is 0, false otherwise
Type
boolean

message(type, data, toClient, volatile)

Sends the given message to all client instances of this player.
Parameters:
Name Type Argument Default Description
type string type of message that should be send
data object <optional>
message data that should be send
toClient module:server/multi~toClient | module:client/multi~toClient <optional>
'all' which client should receive this message
volatile boolean <optional>
false if true, the message may be dropped by the framework. Use this option for real time data where one dropped message does not interrupt your application.
Source:
Example
// on any client
player.on('ping', function (event) {
  // outputs 'bar'
  console.log(event.data.foo);
});
// on server, instance of same player
player.message('ping', { foo: 'bar' });

serialize() → {object}

Prepares this player for sending it via socket message while avoiding circular dependencies.
Source:
Returns:
serialized player object (without socket)
Type
object

Events

attributesChanged

Fired when the attributes of this player have been changed by any client or the server.
Properties:
Name Type Description
value * new value of the changed attribute
Source:
Example
player.on('attributeChanged/score', function (score) {
  console.log(score);
});

// on another client or server
player.attributes.score++;

disconnected

Fired when this player disconnects from the server. Don't use this instance any longer after this event has been fired.
Source: