Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
765bc73197 | |||
105acaf97b | |||
5139136af4 | |||
bcc4ce9a87 | |||
20e7584eb9 | |||
59cbc343cc |
26
package-lock.json
generated
26
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartsocket",
|
"name": "@pushrocks/smartsocket",
|
||||||
"version": "1.1.52",
|
"version": "1.1.55",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -495,20 +495,23 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@pushrocks/smartrx": {
|
"@pushrocks/smartrx": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.5",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrx/-/smartrx-2.0.3.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrx/-/smartrx-2.0.5.tgz",
|
||||||
"integrity": "sha512-OWxagu+CBdPaq76AIg91hJyrNhDTlEpesj01ooWCeVIaLY3G7yvFkqHsEKNOwPUG1LzCWmjq1l1dHQx9p2vJ9A==",
|
"integrity": "sha512-BOlIJmnCO8pxqu9f18D9UV5rIsyrmKeK/mWNMiAe/NH2OTeRPNLpgmhZBkXSKNVD8tSsD8aazs4BcACgYOg1FQ==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/lik": "^3.0.2",
|
"@pushrocks/lik": "^3.0.11",
|
||||||
"@pushrocks/smartevent": "^2.0.3",
|
"@pushrocks/smartevent": "^2.0.3",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"rxjs": "^6.3.3"
|
"rxjs": "^6.5.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": {
|
"rxjs": {
|
||||||
"version": "2.0.5",
|
"version": "6.5.3",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-2.0.5.tgz",
|
"resolved": "https://verdaccio.lossless.one/rxjs/-/rxjs-6.5.3.tgz",
|
||||||
"integrity": "sha512-9j/chLtIiNkR0MDw7Mpxg9slxAVvAQwUZuiaPYX5KpHdKxQaHLI1VZ8IN0vPhwlfgNO4i4vGXV0wB8BvSDj03g=="
|
"integrity": "sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA==",
|
||||||
|
"requires": {
|
||||||
|
"tslib": "^1.9.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -2657,6 +2660,7 @@
|
|||||||
"version": "6.3.3",
|
"version": "6.3.3",
|
||||||
"resolved": "https://verdaccio.lossless.one/rxjs/-/rxjs-6.3.3.tgz",
|
"resolved": "https://verdaccio.lossless.one/rxjs/-/rxjs-6.3.3.tgz",
|
||||||
"integrity": "sha512-JTWmoY9tWCs7zvIk/CvRjhjGaOd+OVBM987mxFo+OW66cGpdKjZcpmc74ES1sB//7Kl/PAe8+wEakuhG4pcgOw==",
|
"integrity": "sha512-JTWmoY9tWCs7zvIk/CvRjhjGaOd+OVBM987mxFo+OW66cGpdKjZcpmc74ES1sB//7Kl/PAe8+wEakuhG4pcgOw==",
|
||||||
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"tslib": "^1.9.0"
|
"tslib": "^1.9.0"
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartsocket",
|
"name": "@pushrocks/smartsocket",
|
||||||
"version": "1.1.52",
|
"version": "1.1.55",
|
||||||
"description": "easy and secure websocket communication",
|
"description": "easy and secure websocket communication",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
@ -26,6 +26,7 @@
|
|||||||
"@pushrocks/smarthash": "^2.0.6",
|
"@pushrocks/smarthash": "^2.0.6",
|
||||||
"@pushrocks/smartlog": "^2.0.21",
|
"@pushrocks/smartlog": "^2.0.21",
|
||||||
"@pushrocks/smartpromise": "^3.0.6",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
|
"@pushrocks/smartrx": "^2.0.5",
|
||||||
"@pushrocks/smartunique": "^3.0.1",
|
"@pushrocks/smartunique": "^3.0.1",
|
||||||
"@types/socket.io": "^2.1.4",
|
"@types/socket.io": "^2.1.4",
|
||||||
"@types/socket.io-client": "^1.4.32",
|
"@types/socket.io-client": "^1.4.32",
|
||||||
|
5
ts/interfaces/connection.ts
Normal file
5
ts/interfaces/connection.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export interface IRequestAuthPayload {
|
||||||
|
serverShortId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TConnectionEvent = 'terminated' | 'error';
|
1
ts/interfaces/index.ts
Normal file
1
ts/interfaces/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './connection';
|
@ -19,7 +19,7 @@ export class Smartsocket {
|
|||||||
/**
|
/**
|
||||||
* a unique id to detect server restarts
|
* a unique id to detect server restarts
|
||||||
*/
|
*/
|
||||||
public id = plugins.smartunique.shortId();
|
public shortId = plugins.smartunique.shortId();
|
||||||
public options: ISmartsocketConstructorOptions;
|
public options: ISmartsocketConstructorOptions;
|
||||||
public io: SocketIO.Server;
|
public io: SocketIO.Server;
|
||||||
public socketConnections = new Objectmap<SocketConnection>();
|
public socketConnections = new Objectmap<SocketConnection>();
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import * as plugins from './smartsocket.plugins';
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
import { SocketConnection } from './smartsocket.classes.socketconnection';
|
import { SocketConnection } from './smartsocket.classes.socketconnection';
|
||||||
import { ISocketFunctionCallDataRequest, SocketFunction } from './smartsocket.classes.socketfunction';
|
import { ISocketFunctionCallDataRequest, SocketFunction } from './smartsocket.classes.socketfunction';
|
||||||
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
|
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
|
||||||
import { SocketRole } from './smartsocket.classes.socketrole';
|
import { SocketRole } from './smartsocket.classes.socketrole';
|
||||||
|
import { defaultLogger } from '@pushrocks/smartlog';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* interface for class SmartsocketClient
|
* interface for class SmartsocketClient
|
||||||
@ -14,14 +16,24 @@ export interface ISmartsocketClientOptions {
|
|||||||
alias: string; // an alias makes it easier to identify this client in a multo client environment
|
alias: string; // an alias makes it easier to identify this client in a multo client environment
|
||||||
role: string;
|
role: string;
|
||||||
password: string; // by setting a password access to functions can be limited
|
password: string; // by setting a password access to functions can be limited
|
||||||
|
autoReconnect?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SmartsocketClient {
|
export class SmartsocketClient {
|
||||||
|
// a unique id
|
||||||
|
public shortId = plugins.smartunique.shortId();
|
||||||
|
|
||||||
|
// the shortId of the remote we connect to
|
||||||
|
public remoteShortId: string = null;
|
||||||
|
|
||||||
public alias: string;
|
public alias: string;
|
||||||
public socketRole: SocketRole;
|
public socketRole: SocketRole;
|
||||||
public socketConnection: SocketConnection;
|
public socketConnection: SocketConnection;
|
||||||
public serverUrl: string;
|
public serverUrl: string;
|
||||||
public serverPort: number;
|
public serverPort: number;
|
||||||
|
public autoReconnect: boolean;
|
||||||
|
|
||||||
|
public eventSubject = new plugins.smartrx.rxjs.Subject();
|
||||||
|
|
||||||
public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>();
|
public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>();
|
||||||
public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>();
|
public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>();
|
||||||
@ -35,6 +47,7 @@ export class SmartsocketClient {
|
|||||||
name: optionsArg.role,
|
name: optionsArg.role,
|
||||||
passwordHash: optionsArg.password
|
passwordHash: optionsArg.password
|
||||||
});
|
});
|
||||||
|
this.autoReconnect = optionsArg.autoReconnect;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addSocketFunction(socketFunction: SocketFunction<any>) {
|
public addSocketFunction(socketFunction: SocketFunction<any>) {
|
||||||
@ -55,29 +68,58 @@ export class SmartsocketClient {
|
|||||||
role: this.socketRole,
|
role: this.socketRole,
|
||||||
side: 'client',
|
side: 'client',
|
||||||
smartsocketHost: this,
|
smartsocketHost: this,
|
||||||
socket: plugins.socketIoClient(socketUrl, { multiplex: false })
|
socket: plugins.socketIoClient(socketUrl, {
|
||||||
|
multiplex: false,
|
||||||
|
reconnectionAttempts: 5,
|
||||||
|
})
|
||||||
});
|
});
|
||||||
this.socketConnection.socket.on('requestAuth', () => {
|
|
||||||
|
const timer = new plugins.smarttime.Timer(5000);
|
||||||
|
timer.start();
|
||||||
|
timer.completed.then(() => {
|
||||||
|
defaultLogger.log('warn', 'connection to server timed out.');
|
||||||
|
this.disconnect();
|
||||||
|
});
|
||||||
|
|
||||||
|
// authentication flow
|
||||||
|
this.socketConnection.socket.on('requestAuth', (requestAuthPayload: interfaces.IRequestAuthPayload) => {
|
||||||
|
timer.reset();
|
||||||
plugins.smartlog.defaultLogger.log('info', 'server requested authentication');
|
plugins.smartlog.defaultLogger.log('info', 'server requested authentication');
|
||||||
this.socketConnection.socket.emit('dataAuth', {
|
|
||||||
role: this.socketRole.name,
|
// lets register the authenticated event
|
||||||
password: this.socketRole.passwordHash,
|
|
||||||
alias: this.alias
|
|
||||||
});
|
|
||||||
this.socketConnection.socket.on('authenticated', () => {
|
this.socketConnection.socket.on('authenticated', () => {
|
||||||
|
this.remoteShortId = requestAuthPayload.serverShortId;
|
||||||
plugins.smartlog.defaultLogger.log('info', 'client is authenticated');
|
plugins.smartlog.defaultLogger.log('info', 'client is authenticated');
|
||||||
this.socketConnection.authenticated = true;
|
this.socketConnection.authenticated = true;
|
||||||
this.socketConnection.listenToFunctionRequests();
|
this.socketConnection.listenToFunctionRequests();
|
||||||
done.resolve();
|
done.resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
// handle errors
|
// lets register the forbidden event
|
||||||
this.socketConnection.socket.on('reconnect_failed', async () => {
|
this.socketConnection.socket.on('forbidden', async () => {
|
||||||
this.disconnect();
|
defaultLogger.log('warn', `disconnecting due to being forbidden to use the ressource`);
|
||||||
|
await this.disconnect();
|
||||||
});
|
});
|
||||||
this.socketConnection.socket.on('connect_error', async () => {
|
|
||||||
this.disconnect();
|
// lets provide the actual auth data
|
||||||
|
this.socketConnection.socket.emit('dataAuth', {
|
||||||
|
role: this.socketRole.name,
|
||||||
|
password: this.socketRole.passwordHash,
|
||||||
|
alias: this.alias
|
||||||
});
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
// handle disconnection and errors
|
||||||
|
this.socketConnection.socket.on('disconnect', async () => {
|
||||||
|
await this.disconnect();
|
||||||
|
});
|
||||||
|
|
||||||
|
this.socketConnection.socket.on('reconnect_failed', async () => {
|
||||||
|
await this.disconnect();
|
||||||
|
});
|
||||||
|
this.socketConnection.socket.on('connect_error', async () => {
|
||||||
|
await this.disconnect();
|
||||||
});
|
});
|
||||||
return done.promise;
|
return done.promise;
|
||||||
}
|
}
|
||||||
@ -87,10 +129,16 @@ export class SmartsocketClient {
|
|||||||
*/
|
*/
|
||||||
public async disconnect() {
|
public async disconnect() {
|
||||||
if (this.socketConnection) {
|
if (this.socketConnection) {
|
||||||
this.socketConnection.socket.disconnect(true);
|
this.socketConnection.disconnect();
|
||||||
this.socketConnection = undefined;
|
this.socketConnection = undefined;
|
||||||
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
|
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
|
||||||
}
|
}
|
||||||
|
defaultLogger.log('warn', `disconnected from server ${this.remoteShortId}`);
|
||||||
|
this.remoteShortId = null;
|
||||||
|
|
||||||
|
if (this.autoReconnect) {
|
||||||
|
this.tryDebouncedReconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import * as plugins from './smartsocket.plugins';
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
import { Objectmap } from '@pushrocks/lik';
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
@ -53,6 +54,9 @@ export class SocketConnection {
|
|||||||
public role: SocketRole;
|
public role: SocketRole;
|
||||||
public smartsocketRef: Smartsocket | SmartsocketClient;
|
public smartsocketRef: Smartsocket | SmartsocketClient;
|
||||||
public socket: SocketIO.Socket | SocketIOClient.Socket;
|
public socket: SocketIO.Socket | SocketIOClient.Socket;
|
||||||
|
|
||||||
|
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionEvent>();
|
||||||
|
|
||||||
constructor(optionsArg: ISocketConnectionConstructorOptions) {
|
constructor(optionsArg: ISocketConnectionConstructorOptions) {
|
||||||
this.alias = optionsArg.alias;
|
this.alias = optionsArg.alias;
|
||||||
this.authenticated = optionsArg.authenticated;
|
this.authenticated = optionsArg.authenticated;
|
||||||
@ -63,12 +67,12 @@ export class SocketConnection {
|
|||||||
|
|
||||||
// standard behaviour that is always true
|
// standard behaviour that is always true
|
||||||
allSocketConnections.add(this);
|
allSocketConnections.add(this);
|
||||||
this.socket.on('disconnect', () => {
|
this.socket.on('disconnect', async () => {
|
||||||
plugins.smartlog.defaultLogger.log(
|
plugins.smartlog.defaultLogger.log(
|
||||||
'info',
|
'info',
|
||||||
`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`
|
`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`
|
||||||
);
|
);
|
||||||
this.socket.disconnect();
|
await this.disconnect();
|
||||||
allSocketConnections.remove(this);
|
allSocketConnections.remove(this);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -80,7 +84,7 @@ export class SocketConnection {
|
|||||||
*/
|
*/
|
||||||
public authenticate() {
|
public authenticate() {
|
||||||
const done = plugins.smartpromise.defer();
|
const done = plugins.smartpromise.defer();
|
||||||
this.socket.on('dataAuth', (dataArg: ISocketConnectionAuthenticationObject) => {
|
this.socket.on('dataAuth', async (dataArg: ISocketConnectionAuthenticationObject) => {
|
||||||
plugins.smartlog.defaultLogger.log(
|
plugins.smartlog.defaultLogger.log(
|
||||||
'info',
|
'info',
|
||||||
'received authentication data. now hashing and comparing...'
|
'received authentication data. now hashing and comparing...'
|
||||||
@ -99,11 +103,14 @@ export class SocketConnection {
|
|||||||
done.resolve(this);
|
done.resolve(this);
|
||||||
} else {
|
} else {
|
||||||
this.authenticated = false;
|
this.authenticated = false;
|
||||||
this.socket.disconnect();
|
await this.disconnect();
|
||||||
done.reject('not authenticated');
|
done.reject('not authenticated');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.socket.emit('requestAuth');
|
const requestAuthPayload: interfaces.IRequestAuthPayload = {
|
||||||
|
serverShortId: this.smartsocketRef.shortId
|
||||||
|
};
|
||||||
|
this.socket.emit('requestAuth', requestAuthPayload);
|
||||||
return done.promise;
|
return done.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,5 +170,9 @@ export class SocketConnection {
|
|||||||
return done.promise;
|
return done.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sending ----------------------
|
// disconnecting ----------------------
|
||||||
|
public async disconnect() {
|
||||||
|
this.socket.disconnect(true);
|
||||||
|
this.eventSubject.next('terminated');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,9 @@ import * as smarthash from '@pushrocks/smarthash';
|
|||||||
import * as smartdelay from '@pushrocks/smartdelay';
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartexpress from '@pushrocks/smartexpress';
|
import * as smartexpress from '@pushrocks/smartexpress';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
import * as smarttime from '@pushrocks/smarttime';
|
||||||
import * as smartunique from '@pushrocks/smartunique';
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
|
import * as smartrx from '@pushrocks/smartrx';
|
||||||
|
|
||||||
|
|
||||||
export {
|
export {
|
||||||
@ -20,7 +22,9 @@ export {
|
|||||||
smartdelay,
|
smartdelay,
|
||||||
smartexpress,
|
smartexpress,
|
||||||
smartpromise,
|
smartpromise,
|
||||||
|
smarttime,
|
||||||
smartunique,
|
smartunique,
|
||||||
|
smartrx
|
||||||
};
|
};
|
||||||
|
|
||||||
// third party scope
|
// third party scope
|
||||||
|
Reference in New Issue
Block a user