Wasabi Class
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.
Item Index
Methods
- _getAllObjects
- _invokeRpc
- _packGhost
- _packGhosts
- _packRemovedGhosts
- _packRpc
- _packRpcs
- _packUpdate
- _packUpdates
- _processConnection
- _unpackGhost
- _unpackGhosts
- _unpackRemovedGhosts
- _unpackRpc
- _unpackUpdate
- _unpackUpdates
- addClass
- addClient
- addObject
- addServer
- createGroup
- makeWasabi static
- mkRpc
- processConnections
- removeClient
- removeObject
- removeServer
Methods
_getAllObjects
()
private
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
Pack an RPC invocation to the appropriate connections
Parameters:
-
rpc
Rpcthe rpc to invoke
-
obj
NetObjectthe obj to use as the context the invocation, or false for static invocations
-
args
Arraythe 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
Packs data needed to instantiate a replicated version of obj
Parameters:
-
obj
ObjectThe object to pack
-
bs
BitStreamThe bitstream to pack into
_packGhosts
-
objects
-
bs
Packs ghosts for needed objects into bs
Parameters:
-
objects
ArrayAn Array or map of objects to pack ghosts for
-
bs
BitstreamThe target Bitstream
_packRemovedGhosts
-
objects
-
bs
Packs removed ghosts for objects
into bs
Parameters:
-
objects
ObjectAn Array or map of objects to remove
-
bs
BitstreamThe target Bitstream
_packRpc
-
rpc
-
args
-
obj
-
bs
Pack a call to a registered RP and the supplied arguments into bs
_packRpcs
-
conn
Pack all RPC invocations in the specified Connection
's queue.
Parameters:
-
conn
ConnectionThe connection to pack RPC invocations for
_packUpdate
-
obj
-
bs
Packs update data for obj
into bs
Parameters:
-
obj
ObjectThe object to pack
-
bs
BitStreamThe bitstream to pack into
_packUpdates
-
list
-
bs
Pack the given list of object update data into bs
Parameters:
-
list
ObjectAn Array or map of objects to pack updates for
-
bs
BitstreamThe target Bitstream
_processConnection
()
private
Receive, process, and transmit data as needed for this connection
_unpackGhost
-
bs
Unpacks a newly replicated object from Bitstream
Parameters:
-
bs
BitstreamThe target bitstream
_unpackGhosts
-
bs
Unpack all needed ghosts from bs
Parameters:
-
bs
BitstreamThe source Bitstream
_unpackRemovedGhosts
-
bs
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:
-
bs
BitstreamThe source Bitstream
_unpackRpc
-
bs
-
conn
Unpack and execute a call to a registered RP using the supplied arguments from bs
Parameters:
-
bs
BitstreamThe source Bitstream
-
conn
ConnectionThe connection this RPC was invoked from
_unpackUpdate
-
bs
Unpacks update data from bs
Parameters:
-
bs
BitStreamThe bitstream to unpack from
_unpackUpdates
-
bs
Unpack the given list of objects (with update data) from bs
Parameters:
-
bs
BitstreamThe source Bitstream
addClass
-
klass
Register a class with Wasabi, allowing it to transmit instances of this class through a Connection
Parameters:
-
klass
FunctionThe constructor of the class to add
addClient
-
client
-
scopeCallback
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
SocketThe socket object used to communicate with the new client
-
scopeCallback
FunctionSee Connection
Returns:
The newly created Connection object
addObject
-
obj
-
serial
Register an instance of a klass, which can then be sent to
connected clients as needed (based on the results of their
scopeCallback
s).
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
NetObjectThe object to add to the registry
-
serial
NumberThe serial number to assign to this object. If falsy, the nextSerialNumber will be used
addServer
-
sock
Attach to a server connected through the socket object
Parameters:
-
sock
SocketThe socket object used to communicate with the new server
Returns:
The newly created Connection object
makeWasabi
()
static
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
Create an RPC from the supplied procedure and serialize functions.
Parameters:
-
fn
FunctionThe local function to call when the RPC is invoked on a remote host
-
opt_serialize
FunctionAn optional serialize function describing the arguments used by this RPC
Returns:
The function you should call remotely to invoke the RPC on a connection
processConnections
()
Process the incoming and outgoing data for all connected clients and servers. This is typically called in your game's update loop
removeClient
-
sock
Remove a client by its socket object. sock
must be strictly equal
(===
) to the original socket.
Parameters:
-
sock
SocketThe socket object originally passed to addClient
removeObject
-
arg
Unregister an instance of a klass
Parameters:
-
arg
MixedEither a NetObject or a serial number to be removed from the registry
removeServer
-
sock
Remove a server by its socket object. sock
must be strictly equal
(===
) to the original socket.
Parameters:
-
sock
SocketThe socket object originally passed to addServer
Events
clientGhostCreate
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
ObjectThe newly created ghost
clientGhostDestroy
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
ObjectThe ghost which is about to be destroyed
receive
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
ConnectionThe connection being processed
-
data
StringThe data being received over the connection
send
Fired before Wasabi sends data over a connection. Useful for measuring data transmission statistics.
Event Payload:
-
conn
ConnectionThe connection being processed
-
data
StringThe data being sent over the connection
sendError
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
ConnectionThe connection which generated the error.
-
e
ErrorThe original error