Skip to content

Latest commit

 

History

History
64 lines (61 loc) · 3.3 KB

File metadata and controls

64 lines (61 loc) · 3.3 KB

CommandSocket - JS Core

A command-based WebSocket communication framework written in TypeScript/JavaScript.

A note about development progress...

The CommandSocket framework is functional, but nevertheless still in its infancy. While the basic functionality should work as documented, bugs are still likely.

Table of Contents

Installation

Install from NPM with $ npm install --save @command-socket/core

Related Packages

There are three other TypeScript/JavaScript CommandSocket packages that you are most likely looking for:

  1. CommandSocket Server [GitHub] [NPM]
    • A centralized server which can spin up individual CommandSockets in connection with any number of clients.
  2. CommandSocket Browser Client [GitHub] [NPM]
    • A CommandSocket client that can be used in the browser.
  3. CommandSocket Node Client [GitHub] [NPM]
    • A CommandSocket client that can be used in a NodeJS environment. Each of these packages provides a specific part of the total framework of the CommandSocket system.

Basic Usage

Begin by spinning up a CommandSocket server (see Related Packages if you are unsure where to find this). typescript

SERVER SIDE

import { CommandSocketServer } from "@command-socket/server";
let serverPort: number = 3849;
let server: CommandSocketServer = new CommandSocketServer(serverPort);

Then, in your choice of environment, create a client and point it to the location of the server that you've just started. Don't worry about which environment you're starting the client in (browser or NodeJS), both implementations expose the exact same functionality. typescript

CLIENT SIDE

import { CommandSocket } from "@command-socket/browser-client";
// OR
import { CommandSocket } from "@command-socket/node-client";
const serverPort: number = 3849;
const serverIP: string = "4.3.2.1";
const wsAddress: string = "ws://" + serverIP + ":" + serverPort;
let client: CommandSocket = new CommandSocket(wsAddress);

And voilà! You have a connected server-client pair! The client also exposes on open and on close events, so you can also perform any desired action when such an event occurs, for example: logging it to the console.

CLIENT SIDE

client.getEvents().OPEN.subscribe(() => console.log("Socket successfully connected!"));
client.getEvents().CLOSE.subscribe(() => console.log("Socket closed."));

After all is said and done, remember to close the server when you are done with it.

SERVER SIDE

server.close();

Documentation

See the wiki for full documentation.

License

@command-socket/core is made available under the GNU General Public License v3. Copyright (C) 2019 Trevor Sears