@wrserver/core
v1.2.3
Published
wrserver - core module
Downloads
1
Readme
WRServer/Core
@wrserver core module that define the basic concepts of the WRS.
Installing
Install this library is easy by cloning the repo. You can install trhought npm too:
Local installation
npm install @wrserver/core
Global installation
npm install -g @wrserver/core
We recomend to use the entire base package (core, crypt, data, auth, mail)
WRServer Concepts
Elements
The WRServer is built up on this elements:
- Module - is a container usefull to pack everything a part of your server needs
- Controller - enstabilish the reachable paths and the sort of responses the server builds
- Model - represent the object on the server, that could be sent as response to the clients
- Service - an intermodule process that allow internal server comunication
- Component - everything the server need to process particular information or run tasks
- Connection - the client representation on the server side
- Message - the client message representation on the server side
Incoming Message Format
//Incoming Message - from Core > Connection
interface IConnectionIncomingParsed {
target: string,
section: string,
page: string,
option: string,
id: number,
data: any
}
Outcoming Message Format
//Outcoming Message - from Core > Connection
interface IConnectionOutcome {
id?: number,
bad: boolean
code: string,
class: string,
data: any,
message: string,
}
WRServer usual procedures
Start-up The Server instanciate once every module disposed at the creation time and search for needs of every module. Then it instanciate every service once and inject them on modules that needs them. The Server is now ready to communicate. Connection A Client want to connect to the server, it make handshacke and the connection is approved. When Server and Client are connected the Server send a lookup table of codes to enstabilish the correct communication. Message Client send a message on the websocket connection. The Server try to parse the message as a well-formatted Message, if it fails reply with a bad-formatted CODE. After the formattation success Server route the Message to the instanciated Module that match name with the message target, check if module can create a Controller named as the message section. If success it create a Controller and try to call the message page method with the Message as argument to generate a response. If not page found try to check if default method is provided else generate a bad response. Anyway if no response is generated the Server provide a bad response as fallback. Broadcast The Server can send a message to every connection that satisfy a filter clause.
How To
Create a custom Module
import { Module, Controller, Service, ModelBase, ControllerType } from "<core>";
//This are just suggested for typing your controller
export type MyControllerServices = { <ServiceName>: <ServiceType> };
export type MyControllerModels = { <ModelName>: <ModelType> };
export class MyModule extends Module {
protected controllers: ControllerType[] = [ ... Controllers needed ... ];
protected models: ModelType[] = [ ... Models needed ... ];
public services: ServiceType[] = [ ... Services needed ... ];
public dependencies: ModuleType[] = [ ... Other Modules needed ... ];
public codes: string[] = [ ... Response Codes needed ... ];
}
Create a custom Controller
import { Controller, Connection, IConnectionIncomingParsed, IConnectionOutcome } from "<core>";
import { MyControllerServices, MyControllerModels } from "<my.module>";
export class MyController extends Controller {
protected services: MyControllerServices;
protected models: MyControllerModels;
//optional
constructor(connection: Connection, services: { [name: string]: any }, models: { [name: string]: any }){
super(connection, services, models);
//... code ...
}
// PAGES
protected <PageName>(message: IConnectionIncomingParsed): IConnectionOutcome {
//... code...
if(BAD){
return this.bad(<code>);
}
if(GOOD){
return this.ok(<class>, <response>)
}
}
public static section: string = <SectionName>;
}
Create a custom Model
NOTE: @wrserver/data gives a new model representation for db-like usage.
import { Model, ModelBase } from '<core>';
@Model
export class MyModel extends ModelBase {
// PROPERTIES
public sendable(): any{ return <object cleaned for response>; }
}
Create a custom Service
import { Service, Emitter, Connection } from "<core>";
/** Auth Service for simple manage of User Account with WS Connections */
export class MyService extends Service {
constructor(eventEmitter: Emitter){
super(eventEmitter);
//... code ...
//REMEBRE to call ready method after the service has started up
this.ready();
}
}
Usage
TS
import { WRServer } from '@wrserver/core';
let options = {
path: <path>,
port: <port>,
modules: [ ... Modules ... ],
root: <static root>
}
let wrserver = new WRServer(options.path, options.port, options.modules).withRoot(options.root);
###JS
const { WRServer } = require('@wrserver/core');
let options = {
path: <path>,
port: <port>,
modules: [ ... Modules ... ],
root: <static root>
};
let wrserver = new WRServer(options.path, options.port, options.modules).withRoot(options.root);
Contacts
If you like the project feel free to contact me on my .
Something gone wrong? Feel free to rise an issue!
Did you like this project and it was usefull? Help me improve my work: