nettalk
v1.3.0
Published
Library for connecting via sockets with a NetTalk's Clarion Application using WPP or Partial Packet Protocol
Downloads
7
Readme
NetTalk
Library for connecting via sockets with a NetTalk's Clarion Application using WPP or Partial Packet Protocol
Installation
npm i NetTalk
Usage
The following examples create a Server and Client that can communicate between each other using Partiall Packet Protocol (PPP)
Server.js
const NetTalk = require("NetTalk");
const options = {
host: "",
port: 8080,
protocol: "PPP"
};
const server = new NetTalk.Server(options);
server.startServer();
server.on("packageReceived", (connection, data) => {
if (data === "Hello World!") {
connection.send("Hello World!");
}
});
Client.js
Connection per request:
const NetTalk = require("NetTalk");
const options = {
host: "127.0.0.1",
port: 8080,
protocol: "PPP"
};
const client = new NetTalk.Client(options);
client
.sendRequest("Hello World!")
.then(data => {
console.log(data);
})
.catch(e => {
console.error(e);
});
Connection opened and closed manually:
const NetTalk = require("NetTalk");
const options = {
host: "127.0.0.1",
port: 8080,
protocol: "PPP"
};
const client = new NetTalk.Client(options);
client
.send("Hello World!")
.then(data => {
return client.respond("Hello World!");
})
.then(data => {
console.log(data);
})
.catch(e => {
console.error(e);
});
Specifications
Server
The server part of NetTalk library provides a series of Methods and properties to handle connections and packets.
Options:
Following is an interface representing a complete Server Configuration Object:
interface NetTalkOptions {
host: string; // for server this is for focused listening; i.e. listening for packages comming only from a defined address.
port: number;
ssl?: {
// if present, SSL will be used on the connection.
key: string;
certificate: string;
password: string;
rejectUnauthorized: boolean;
};
protocol: "WPP" | "PPP"; // protocol to be used WPP("Whole Packet Protocol") or PPP ("Partial Packet Protocol")
delimiter?: string; // in case of protocol: "PPP", represents the character to send at the end of stream.
timeOut?: number;
keepAlive?: number;
log?: boolean; // if present = true; if you want a more in depth logging, you may activate it.
}
Properties:
The Properties provided by the NetTalk object are:
connectionType: "SSL" | "TCP" — Returns the type of connection beign used on the socket i.e. "SSL" | "TCP"
isServerUp: boolean — true
if server is up, false
if server is down.
currentConnections: NetTalkConnection[] — Returns an array with the current open connections.
Methods:
A NetTalk Object has the following methods defined:
startServer: (): void — Creates and starts a new Server.
on (event: string, listener: *()=> void): void — attaches a listener to the event provided.
shutDown(): void
*Note: every listener uses diferent parammeters, for more information on listeners and events see Events.
Client
The client part of NetTalk library is defined as followed:
Options:
The follow code represents a complete Client Config Object:
interface NetTalkOptions {
host: string; // The server to connect to.
port: number;
ssl?: {
// if present, SSL will be used on the connection.
key: string;
certificate: string;
password: string;
};
protocol: "WPP" | "PPP"; // protocol to be used WPP("Whole Packet Protocol") or PPP ("Partial Packet Protocol")
delimiter?: string; // in case of protocol: "PPP", represents the character to send at the end of stream.
timeOut?: number;
keepAlive?: number;
log?: boolean; // if present = true; if you want a more in depth logging, you may activate it.
}
Methods:
As the client part of the library is build for maximum ease of use, it provides one method for sending request and awaiting immediate response and closing the connection, and other for sending a request and not closing the connection after a response is achieved, so you can keep connection alive and keep sending packages over the same socket.
sendRequest(request: string): Promise< string > — Sends a request to the specified host. The method returs a promise that will be resolved with the data responded by the server, so it is possible to use it on async/await functions. After data is received, the connection is closed immediately. Any further use of send request will create a new socket i.e. send SYN ACK packages, which takes time on each request send.
send(request: string): Promise< string > — An alternative for sendRequest is using the send function. This method connects to the server and sends the request the same as sendRequest, however it does not close the connection immediately after a response is received, therefore it is necessary to close it manually with the close method.
respond(request: string): Promise< string > — In conjunction with the send method, it is used to respond to the server over the same connection opened by the send method. It throws an error if no connection is still opened..
close(): void — Closes the opened connection, it throws an error if no connection is open.
Events
"serverStarted"
Listener: (serverType: "SSL" | "TCP") => void
This event fires up immediately after the server starts listening on the specified port. The listener is called with the type of server that was started "SSL" or "TCP".
"connectionReceived"
Listener: (connection: NetTalkConnection) => void
This event fires up for every new connection that is established. The listener is called with the connection object.
"packageReceived"
Listener: (connection: NetTalkConnection, data: string) => void
This event fires up every time a new package is received. The listener is called with the connection object where the message was sent, and the data sent.
"connectionLost"
Listener: (connection: NetTalkConnection, error?: Error) => void
This event fires up when a connection is closed, timed up, or killed. The listener is called with the connection object that was lost, and if it was lost for some error, the error that occurred.
Note: when this event is fired, the connection lost have already been remove from the currentConnections array.
NetTalkConnection
A NettalkConnection is an object that represents a connection on NetTalk, it is basically a wrapper on a TCP/SSL: Socket that abstracts most of the deep stuff on the Socket.
Properties
It provides the following properties:
clientIP: string — The ip of the client connected.
UUID: string — The UUID that uniquely identifies the connection.
Methods
The following methods are available on the object:
on(event: string, listener: *()=> void): void — attaches a listener to the event provided.
send(data: string): void — sends data back to the client.
*Note: every listener uses diferent parammeters, for more information on listeners and events see Events.