-
Notifications
You must be signed in to change notification settings - Fork 0
Starter's Guide
This guide will serve as a short and sweet introduction to the CommandSocket framework, including the use of both a server and client.
Before we get started, we need to go over a couple of important key concepts of the CommandSocket framework.
First off, we have commands. Commands are the most important piece of the puzzle, and the Command interface is going to be the one that you will probably be implementing the most. A CommandSocket Command is a structure for defining executable code segments that have well defined inputs as well as outputs.
Every Command implementation has an #execute function as a part of its definition. The interface's #execute member is defined as follows:
execute(params: C["params"], context: CommandSocket): Promise<C["return"]>;Let's clarify a couple things about this function signature...
First off, the params: C["params"] and Promise<C["return"]> part. Don't worry too much about exactly what the type of the generic type C is. All you need to know is that the params parameter is of a type that can be defined for each individual Command implementation, and the same goes for the return type of the function, although said return type does always need to be a Promise. That said, the generic type of the Promise can be specified.
OK, it's time to create the CommandSocket server! To do so, all we have to do it create a new instance of CommandSocketServer from the @command-socket/server package.
The constructor for the CommandSocketServer class takes one argument: the port on which the server should be started.
// Import the CommandSocket server package:
import { CommandSocketServer } from "@command-socket/server";
// Create a new instance of the CommandSocketServer class, starting on port 3849:
let server: CommandSocketServer = new CommandSocketServer(3849);Now, we can also optionally specify the LCS (local command set) and RCS (remote command set) of our CommandSocketServer as generic arguments upon initialization. Both of these generic arguments must implement the CommandSetStructure interface. If we wish to do so, those generic arguments can be passed as follows:
// Initialize the CommandSocketServer with generic arguments.
let server: CommandSocketServer<MyLCS, MyRCS> = new CommandSocketServer(3849);