Class: Session

Session

A game session that connects and manages multiple players.

Mixes In

Members

<readonly> maxPlayerAllowed

Source:
See:

<readonly> minPlayerNeeded

Source:
See:

<readonly> players :Object.<string, module:shared/player~Player>

Dictionary of all players currently connected to this session mapped on their ids.
Type:
Source:

<readonly> token :string

unique token identifying this session
Type:
  • string
Source:

Methods

<protected> addPlayer(player)

Adds the given player to this session. Override if needed.
Parameters:
Name Type Description
player module:shared/player~Player player instance to add
Source:
Fires:

<protected> destroy()

Deconstructs this session when no longer needed and informs listening objects.
Source:

disablePlayerJoining()

When you call this new players are not allowed to join this session any more. Instead their promise will be rejected with a JoiningDisabledError.
Source:

enablePlayerJoining()

A call to this method will allow new players to join this session again.
Source:

getPlayerArray() → {Array.<module:shared/player~Player>}

Source:
Returns:
an array of all players currently connected to this session. The array is sorted by player numbers from small to high.
Type
Array.<module:shared/player~Player>

getPlayerById() → {module:shared/player~Player}

Source:
Returns:
the player with the given id (even if this is myself) or null if no player with this id exists
Type
module:shared/player~Player

getPlayerByNumber() → {module:shared/player~Player}

Source:
Returns:
the player with the given player numbers (even if this is myself) or null if no player with this number exists
Type
module:shared/player~Player

getPlayerCount() → {integer}

Source:
Returns:
number of currently connected players including myself
Type
integer

isFull() → {boolean}

Source:
Returns:
true if there are as many ore more players connected to this session as are allowed
Type
boolean

message(type, data, toClient, volatile)

Sends the given message to all other instances of this session.
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:client/multi~toClient | module:server/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 client no 1 or server
session.on('ping', function (event) {
  // outputs 'bar'
  console.log(event.data.foo);
});
// on client no 2 or server, instance of same session
session.message('ping', { foo: 'bar' });

<protected> onSessionReady()

Child classes should call this method when they are finished building and are ready to add listeners to themselves.
Source:

<protected> removePlayer(player)

Removes the given player from this session. Override if needed.
Parameters:
Name Type Description
player module:shared/player~Player player instance to remove
Source:
Fires:
  • module:shared/session~Session#event:playerRemoved

serialize() → {object}

Prepares this session and all its players for sending it via socket message while avoiding circular dependencies.
Source:
Returns:
serialized session object including players
Type
object

Events

aboveMaxPlayerAllowed

Fired when a new player has been added to this session and there are now exactly as many players connected to this session as stated in maxPlayerAllowed.
Source:

aboveMinPlayerNeeded

Fired when a new player has been added to this session and there are now exactly as many players connected to this session as stated in minPlayerNeeded.

You could listen for this event to start your game when enough players have connected.
Source:

belowMaxPlayerAllowed

Fired when a player has been removed from this session and there are now less player connected to this session than stated in maxPlayerNeeded.
Source:

belowMinPlayerNeeded

Fired when a player has been removed from this session and there are now less player connected to this session than stated in minPlayerNeeded.

You could listen for this event to stop a running game when the player count is getting to low.
Source:

destroyed

Fired when this session is no longer valid.
The reason could be a broken connection or the removal of your own player.

Don't use this session any longer after the event has been fired.
Source:

playerJoined

Fired when a new player has been added to this session. From now on you can safely communicate with this player.
Properties:
Name Type Description
player module:shared/player~Player The newly added player.
Source:
Example

Adding connected players to the DOM

session.on('playerJoined', function (event) {
  var playerDiv = $('#player').clone();
  $('#players').append(playerDiv);
  event.player.on('disconnected', function () {
    playerDiv.remove();
  });
}

playerLeft

Fired when a player has been removed from this session.
Properties:
Name Type Description
player module:shared/player~Player The removed player.
Source: