Skip to content
Elijah Cobb edited this page Apr 20, 2020 · 3 revisions

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).

What the he** is a LiBaseSocket?

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.

Pages

element-ts

Element is a group of developer tools I have built in TypeScript. Check them out at: element-ts.com.

About

Author: Elijah Cobb

NPM: @element-ts/lithium

Issue Reporting: Github Issues

Clone this wiki locally