Client - element-ts/lithium GitHub Wiki
To create a new client connection to a LiServer
, all you need to do is instantiate a new
LiSocket
. In order to instantiate a new socket you need to provide an object conforming to
LiSocketConfig
to the static method init()
on LiServer
.
NOTE: Do not use constructor on LiSocket
, use LiSocket.init()
.
LiSocket.init()
takes a config object conforming to LiSocketConfig
and three generic parameters
-
LC
: The socket's local command registry. -
RC
: The server's (remote) command registry. -
SC
: The sibling sockets' command registry. (view peer-to-peer for more info)
All type parameters must extend LiCommandRegistryStructure
but you can leave them blank for no
type checking. Don't do this though!!!
const socket: LiSocket<LC, RC> = await LiSocket.init({
address: "ws://localhost:8080",
debug: true
});
Once you have an instance of LiSocket
you can implement all the methods it is required to
implement as provided by the LC
generic parameter when creating the server. To implement a method,
use the implement()
method on your server instance.
For example, if in your LC
type you had add: {param: number, return: number}
you could implement
it like below:
socket.implement("add", async(num: number): Promise<number> => {
return num + 1;
});
Could be invoked using: await socket.invoke("add", 3)
from the server.
When using the implement()
command, note that it is expecting an anonymous function returning a
promise. This is so you can use an async function. If you have a command that has a param
type
of void
you can simply remove the parameter in the function like:
socket.implement("a-function-with-void-param", async(): Promise<void> => {
console.log("Called!!! Also, not going to return anything either!!!");
});
Could be invoked using: await socket.invoke("a-function-with-void-param", undefined)
from the
server.