API Docs for:
Show:

Wasabi Class

Defined in: src/wasabi.js:29

Facade class for interacting with Wasabi.

Note that Wasabi implements the Node.js events.EventEmitter interface for event handling, allowing use of on, once, removeListener and friends. See the related Node.js events.EventEmitter docs for event handling methods.

Methods

_getAllObjects

() private

Defined in src/wasabi.js:557

Returns a clone of the registry's object table. used as a fallback when no _scopeCallback is specified for a connection

_invokeRpc

(
  • rpc
  • obj
  • args
)
private

Defined in src/wasabi.js:412

Pack an RPC invocation to the appropriate connections

Parameters:

  • rpc Rpc

    the rpc to invoke

  • obj NetObject

    the obj to use as the context the invocation, or false for static invocations

  • args Array

    the arguments to the rpc, followed by an optional list of connections to emit the invocation to. If no connections are specified, the invocation is emitted to all connections

_packGhost

(
  • obj
  • bs
)
private

Defined in src/wasabi.js:227

Packs data needed to instantiate a replicated version of obj

Parameters:

  • obj Object

    The object to pack

  • bs BitStream

    The bitstream to pack into

_packGhosts

(
  • objects
  • bs
)
private

Defined in src/wasabi.js:280

Packs ghosts for needed objects into bs

Parameters:

  • objects Array

    An Array or map of objects to pack ghosts for

  • bs Bitstream

    The target Bitstream

_packRemovedGhosts

(
  • objects
  • bs
)
private

Defined in src/wasabi.js:315

Packs removed ghosts for objects into bs

Parameters:

  • objects Object

    An Array or map of objects to remove

  • bs Bitstream

    The target Bitstream

_packRpc

(
  • rpc
  • args
  • obj
  • bs
)
private

Defined in src/wasabi.js:496

Pack a call to a registered RP and the supplied arguments into bs

Parameters:

  • rpc Rpc

    The RPC to pack

  • args Array

    The arguments to be serialized into this invocation

  • obj NetObject

    The NetObject to apply the RPC to (or falsy for "static" RPC invocation

  • bs Bitstream

    The target Bitstream

_packRpcs

(
  • conn
)
private

Defined in src/wasabi.js:480

Pack all RPC invocations in the specified Connection's queue.

Parameters:

  • conn Connection

    The connection to pack RPC invocations for

_packUpdate

(
  • obj
  • bs
)
private

Defined in src/wasabi.js:199

Packs update data for obj into bs

Parameters:

  • obj Object

    The object to pack

  • bs BitStream

    The bitstream to pack into

_packUpdates

(
  • list
  • bs
)
private

Defined in src/wasabi.js:375

Pack the given list of object update data into bs

Parameters:

  • list Object

    An Array or map of objects to pack updates for

  • bs Bitstream

    The target Bitstream

_processConnection

() private

Defined in src/wasabi.js:574

Receive, process, and transmit data as needed for this connection

_unpackGhost

(
  • bs
)
private

Defined in src/wasabi.js:239

Unpacks a newly replicated object from Bitstream

Parameters:

_unpackGhosts

(
  • bs
)
private

Defined in src/wasabi.js:300

Unpack all needed ghosts from bs

Parameters:

_unpackRemovedGhosts

(
  • bs
)
private

Defined in src/wasabi.js:335

Unpack all removed ghosts from bs. An object with its ghost unpacked in this way will be removed from the local Wasabi's registry

Parameters:

_unpackRpc

(
  • bs
  • conn
)
private

Defined in src/wasabi.js:515

Unpack and execute a call to a registered RP using the supplied arguments from bs

Parameters:

_unpackUpdate

(
  • bs
)
private

Defined in src/wasabi.js:211

Unpacks update data from bs

Parameters:

  • bs BitStream

    The bitstream to unpack from

_unpackUpdates

(
  • bs
)
private

Defined in src/wasabi.js:392

Unpack the given list of objects (with update data) from bs

Parameters:

addClass

(
  • klass
)

Defined in src/wasabi.js:51

Register a class with Wasabi, allowing it to transmit instances of this class through a Connection

Parameters:

  • klass Function

    The constructor of the class to add

addClient

(
  • client
  • scopeCallback
)
Connection

Defined in src/wasabi.js:133

Attach a client connected through the given socket object. Currently this must be a WebSocket or socket.io socket, or something that is API compatible (i.e. has an onmessage callback and a send method).

Parameters:

  • client Socket

    The socket object used to communicate with the new client

  • scopeCallback Function

Returns:

Connection:

The newly created Connection object

addObject

(
  • obj
  • serial
)

Defined in src/wasabi.js:61

Register an instance of a klass, which can then be sent to connected clients as needed (based on the results of their scopeCallbacks).

Note: This method should only be called manually on authoritative peers (i.e. server-side). Wasabi clients will automatically add instances to the Registry when their ghosts are unpacked

Parameters:

  • obj NetObject

    The object to add to the registry

  • serial Number

    The serial number to assign to this object. If falsy, the nextSerialNumber will be used

addServer

(
  • sock
)
Connection

Defined in src/wasabi.js:104

Attach to a server connected through the socket object

Parameters:

  • sock Socket

    The socket object used to communicate with the new server

Returns:

Connection:

The newly created Connection object

createGroup

() Group

Defined in src/wasabi.js:190

Create a new visibility group

Returns:

Group:

The new group

makeWasabi

() static

Defined in src/wasabi.js:9

Named and exported function that would otherwise be an IIFE. Used to instantiate a second Wasabi module for use in tests (to simulate a remote client)

mkRpc

(
  • fn
  • opt_serialize
)
Function

Defined in src/wasabi.js:90

Create an RPC from the supplied procedure and serialize functions.

Parameters:

  • fn Function

    The local function to call when the RPC is invoked on a remote host

  • opt_serialize Function

    An optional serialize function describing the arguments used by this RPC

Returns:

Function:

The function you should call remotely to invoke the RPC on a connection

processConnections

()

Defined in src/wasabi.js:167

Process the incoming and outgoing data for all connected clients and servers. This is typically called in your game's update loop

removeClient

(
  • sock
)

Defined in src/wasabi.js:151

Remove a client by its socket object. sock must be strictly equal (===) to the original socket.

Parameters:

  • sock Socket

    The socket object originally passed to addClient

removeObject

(
  • arg
)

Defined in src/wasabi.js:80

Unregister an instance of a klass

Parameters:

  • arg Mixed

    Either a NetObject or a serial number to be removed from the registry

removeServer

(
  • sock
)

Defined in src/wasabi.js:117

Remove a server by its socket object. sock must be strictly equal (===) to the original socket.

Parameters:

  • sock Socket

    The socket object originally passed to addServer

Events

clientGhostCreate

Defined in src/wasabi.js:259

Fired client-side when a ghost (the remote counterpart) of an object is created. This occurs when the scope callback for this client (on the server) returns an object when it did not previously.

The obj will be a newly created instance of the class every time this event is emitted, even when subsequent emissions refer to the same server-side instance. That is, Wasabi created a brand new object every time it creates a ghost.

Note that this event can be emitted multiple times per object, if object comes in and out of scope.

Event Payload:

  • obj Object

    The newly created ghost

clientGhostDestroy

Defined in src/wasabi.js:350

Fired client-side when a ghost (the remote counterpart) of an object is about to be destroyed. This occurs when the scope callback for this client (on the server) does not return the object after it did previously.

Although Wasabi can not acutally "destroy" the object (since JavaScript has no destructors), the particular instance will never be referred to be Wasabi again.

Note that this event can be emitted multiple times per object, if object comes in and out of scope.

Event Payload:

  • obj Object

    The ghost which is about to be destroyed

receive

Defined in src/wasabi.js:643

Fired before Wasabi processes incoming data. Useful for measuring data transmission statistics.

Note that this event fires during processConnections, and is not meant to replace the onmessage handler for typical WebSockets or socket.io sockets.

Event Payload:

  • conn Connection

    The connection being processed

  • data String

    The data being received over the connection

send

Defined in src/wasabi.js:670

Fired before Wasabi sends data over a connection. Useful for measuring data transmission statistics.

Event Payload:

  • conn Connection

    The connection being processed

  • data String

    The data being sent over the connection

sendError

Defined in src/wasabi.js:685

Fired when Wasabi receives an error while sending data over a connection. Note that Wasabi will remove the connection from its list of clients and servers immediately after emitting this event.

An event is used in order to give user code a chance to react to the error without interupting the processing of other connections within the same processConnections call.

Event Payload:

  • conn Connection

    The connection which generated the error.

  • e Error

    The original error