Server - element-ts/lithium GitHub Wiki
All sockets communicate with an instance of LiServer
. To create a new instance of a LiServer
use its constructor and provide an object conforming to LiServerConfig
. You are required two
types as well that conform to LiCommandRegistryStructure
.
To read about command registry types view the Command Registry page.
const server: LiServer<PlaygroundServerCommands, PlaygroundClientCommands> = new LiServer({
port: 8080,
debug: true
});
Once you have an instance of LiServer
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:
server.implement("add", async(num: number): Promise<number> => {
return num + 1;
});
A client could invoke with: await socket.invoke("add", 3)
.
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:
server.implement("a-function-with-void-param", async(): Promise<void> => {
console.log("Called!!! Also, not going to return anything either!!!");
});
A client could invoke with: await socket.invoke("a-function-with-void-param", undefined)
.
The base socket class is the basis for what a socket is. It contains the actual connection,
manages message coding, and quite a bit more. Every LiSocket
extends LiBaseSocket
and a
LiServer
has a mapping of LiBaseSockets
.