smartsocket/ts/smartsocket.classes.socketconnection.ts

225 lines
7.5 KiB
TypeScript
Raw Normal View History

2022-03-14 21:40:55 +00:00
import * as plugins from './smartsocket.plugins.js';
import * as pluginsTyped from './smartsocket.pluginstyped.js';
import * as interfaces from './interfaces/index.js';
2016-08-08 16:20:00 +00:00
// import classes
2022-03-14 21:40:55 +00:00
import { Smartsocket } from './smartsocket.classes.smartsocket.js';
import { SocketFunction } from './smartsocket.classes.socketfunction.js';
import { SocketRequest, ISocketRequestDataObject } from './smartsocket.classes.socketrequest.js';
2018-03-15 01:29:40 +00:00
// socket.io
2022-03-14 21:40:55 +00:00
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient.js';
import { logger } from './smartsocket.logging.js';
2016-08-08 16:20:00 +00:00
2016-08-09 09:42:21 +00:00
// export interfaces
/**
* defines is a SocketConnection is server or client side. Important for mesh setups.
*/
2018-03-15 01:29:40 +00:00
export type TSocketConnectionSide = 'server' | 'client';
2016-08-09 09:42:21 +00:00
/**
* interface for constructor of class SocketConnection
*/
2016-08-11 23:32:57 +00:00
export interface ISocketConnectionConstructorOptions {
2018-03-15 01:29:40 +00:00
alias: string;
authenticated: boolean;
side: TSocketConnectionSide;
2019-08-12 20:31:40 +00:00
smartsocketHost: Smartsocket | SmartsocketClient;
2022-01-19 17:36:13 +00:00
socket: pluginsTyped.socketIo.Socket | pluginsTyped.socketIoClient.Socket;
2017-07-07 20:02:19 +00:00
}
2016-08-08 16:20:00 +00:00
2016-08-09 09:42:21 +00:00
/**
* interface for authentication data
*/
export interface ISocketConnectionAuthenticationObject {
2022-01-19 14:34:52 +00:00
alias: string;
2017-07-07 20:02:19 +00:00
}
2016-08-09 09:42:21 +00:00
// export classes
2020-09-24 18:03:01 +00:00
export let allSocketConnections = new plugins.lik.ObjectMap<SocketConnection>();
2016-08-09 09:42:21 +00:00
/**
* class SocketConnection represents a websocket connection
*/
2016-08-08 16:20:00 +00:00
export class SocketConnection {
2019-06-07 06:40:24 +00:00
public alias: string;
public side: TSocketConnectionSide;
public authenticated: boolean = false;
2019-08-12 20:31:40 +00:00
public smartsocketRef: Smartsocket | SmartsocketClient;
2022-01-19 17:36:13 +00:00
public socket: pluginsTyped.socketIo.Socket | pluginsTyped.socketIoClient.Socket;
2019-11-06 23:26:47 +00:00
2019-11-08 17:41:08 +00:00
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
public eventStatus: interfaces.TConnectionStatus = 'new';
2019-11-06 23:26:47 +00:00
2021-01-28 01:30:27 +00:00
private tagStore: interfaces.TTagStore = {};
public tagStoreObservable = new plugins.smartrx.rxjs.Subject<interfaces.TTagStore>();
public remoteTagStoreObservable = new plugins.smartrx.rxjs.Subject<interfaces.TTagStore>();
2018-03-15 01:29:40 +00:00
constructor(optionsArg: ISocketConnectionConstructorOptions) {
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.side = optionsArg.side;
2019-08-12 20:31:40 +00:00
this.smartsocketRef = optionsArg.smartsocketHost;
2018-03-15 01:29:40 +00:00
this.socket = optionsArg.socket;
2016-08-12 01:22:36 +00:00
2017-07-07 20:02:19 +00:00
// standard behaviour that is always true
2018-03-15 01:29:40 +00:00
allSocketConnections.add(this);
2019-11-08 17:48:39 +00:00
// handle connection
this.socket.on('connect', async () => {
this.updateStatus('connected');
});
2019-11-08 16:11:41 +00:00
this.socket.on('disconnect', async () => {
2020-09-24 18:03:01 +00:00
logger.log(
'info',
2018-03-15 01:29:40 +00:00
`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`
);
2019-11-08 16:11:41 +00:00
await this.disconnect();
2018-03-15 01:29:40 +00:00
allSocketConnections.remove(this);
2021-02-01 22:36:37 +00:00
this.eventSubject.next('disconnected');
2018-03-15 01:29:40 +00:00
});
2017-07-07 20:02:19 +00:00
}
2016-08-12 01:22:36 +00:00
2021-01-28 01:30:27 +00:00
/**
* adds a tag to a connection
*/
public async addTag(tagArg: interfaces.ITag) {
const done = plugins.smartpromise.defer();
this.tagStore[tagArg.id] = tagArg;
this.tagStoreObservable.next(this.tagStore);
const remoteSubscription = this.remoteTagStoreObservable.subscribe((remoteTagStore) => {
2022-01-19 06:01:58 +00:00
if (!remoteTagStore[tagArg.id]) {
return;
}
2021-01-28 01:30:27 +00:00
const localTagString = plugins.smartjson.stringify(tagArg);
2021-01-28 01:31:42 +00:00
const remoteTagString = plugins.smartjson.stringify(remoteTagStore[tagArg.id]);
2021-01-28 01:30:27 +00:00
if (localTagString === remoteTagString) {
remoteSubscription.unsubscribe();
done.resolve();
}
2021-01-28 01:31:42 +00:00
});
2021-01-28 01:30:27 +00:00
this.socket.emit('updateTagStore', this.tagStore);
await done.promise;
}
/**
* gets a tag by id
* @param tagIdArg
*/
public async getTagById(tagIdArg: interfaces.ITag['id']) {
return this.tagStore[tagIdArg];
2021-01-28 01:31:42 +00:00
}
2021-01-28 01:30:27 +00:00
/**
* removes a tag from a connection
*/
public async removeTagById(tagIdArg: interfaces.ITag['id']) {
delete this.tagStore[tagIdArg];
this.tagStoreObservable.next(this.tagStore);
this.socket.emit('updateTagStore', this.tagStore);
}
2017-07-07 20:02:19 +00:00
// authenticating --------------------------
2016-08-08 16:20:00 +00:00
2017-07-07 20:02:19 +00:00
/**
* authenticate the socket
*/
2019-08-12 20:31:40 +00:00
public authenticate() {
const done = plugins.smartpromise.defer();
2019-11-06 23:26:47 +00:00
this.socket.on('dataAuth', async (dataArg: ISocketConnectionAuthenticationObject) => {
2022-01-19 14:34:52 +00:00
logger.log('info', 'received authentication data...');
2021-01-28 01:30:27 +00:00
this.socket.removeAllListeners('dataAuth');
2022-01-19 14:34:52 +00:00
if (dataArg.alias) {
2018-03-15 01:29:40 +00:00
// TODO: authenticate password
this.alias = dataArg.alias;
this.authenticated = true;
this.socket.emit('authenticated');
2022-01-19 14:34:52 +00:00
logger.log('ok', `socket with >>alias ${this.alias} is authenticated!`);
2018-03-15 01:29:40 +00:00
done.resolve(this);
2017-07-07 20:02:19 +00:00
} else {
2018-03-15 01:29:40 +00:00
this.authenticated = false;
2019-11-06 23:26:47 +00:00
await this.disconnect();
2022-01-19 14:34:52 +00:00
done.reject('a socket tried to connect, but could not authenticated.');
2017-07-07 20:02:19 +00:00
}
2018-03-15 01:29:40 +00:00
});
2019-11-06 23:26:47 +00:00
const requestAuthPayload: interfaces.IRequestAuthPayload = {
2022-01-19 14:34:52 +00:00
serverAlias: this.smartsocketRef.alias,
2019-11-06 23:26:47 +00:00
};
this.socket.emit('requestAuth', requestAuthPayload);
2018-03-15 01:29:40 +00:00
return done.promise;
2017-07-07 20:02:19 +00:00
}
2016-08-12 01:22:36 +00:00
2017-07-07 20:02:19 +00:00
// listening -------------------------------
/**
* listen to function requests
*/
2019-08-12 20:31:40 +00:00
public listenToFunctionRequests() {
const done = plugins.smartpromise.defer();
2017-07-07 20:02:19 +00:00
if (this.authenticated) {
2019-09-09 21:58:32 +00:00
this.socket.on('function', (dataArg: ISocketRequestDataObject<any>) => {
2017-07-07 20:02:19 +00:00
// check if requested function is available to the socket's scope
2020-09-29 17:21:08 +00:00
// logger.log('info', 'function request received');
2022-12-28 12:52:16 +00:00
const referencedFunction: SocketFunction<any> =
this.smartsocketRef.socketFunctions.findSync((socketFunctionArg) => {
2018-03-15 01:29:40 +00:00
return socketFunctionArg.name === dataArg.funcCallData.funcName;
2022-12-28 12:52:16 +00:00
});
2019-08-13 09:36:31 +00:00
if (referencedFunction) {
2020-09-29 17:21:08 +00:00
// logger.log('ok', 'function in access scope');
2019-08-12 20:31:40 +00:00
const localSocketRequest = new SocketRequest(this.smartsocketRef, {
2017-07-07 20:02:19 +00:00
side: 'responding',
originSocketConnection: this,
shortId: dataArg.shortId,
2020-09-24 18:04:11 +00:00
funcCallData: dataArg.funcCallData,
2018-03-15 01:29:40 +00:00
});
localSocketRequest.createResponse(); // takes care of creating response and sending it back
2016-08-08 16:20:00 +00:00
} else {
2020-09-24 18:04:11 +00:00
logger.log('warn', 'function not existent or out of access scope');
2017-07-07 20:02:19 +00:00
}
2018-03-15 01:29:40 +00:00
});
2019-09-09 21:58:32 +00:00
this.socket.on('functionResponse', (dataArg: ISocketRequestDataObject<any>) => {
2020-12-16 01:38:57 +00:00
// logger.log('info', `received response for request with id ${dataArg.shortId}`);
2019-08-12 20:46:57 +00:00
const targetSocketRequest = SocketRequest.getSocketRequestById(
this.smartsocketRef,
dataArg.shortId
);
2018-03-15 01:29:40 +00:00
targetSocketRequest.handleResponse(dataArg);
});
2021-01-28 01:30:27 +00:00
this.socket.on('updateTagStore', async (tagStoreArg: interfaces.TTagStore) => {
2021-01-28 12:39:31 +00:00
if (!plugins.smartjson.deepEqualObjects(this.tagStore, tagStoreArg)) {
2021-01-28 01:30:27 +00:00
this.tagStore = tagStoreArg;
this.socket.emit('updateTagStore', this.tagStore);
this.tagStoreObservable.next(this.tagStore);
}
this.remoteTagStoreObservable.next(tagStoreArg);
2021-01-28 01:31:42 +00:00
});
2021-01-28 01:30:27 +00:00
2022-12-28 12:52:16 +00:00
logger.log(
'info',
`now listening to function requests for ${this.alias} on side ${this.side}`
);
2018-03-15 01:29:40 +00:00
done.resolve(this);
2017-07-07 20:02:19 +00:00
} else {
2019-08-12 20:31:40 +00:00
const errMessage = 'socket needs to be authenticated first';
2020-09-24 18:03:01 +00:00
logger.log('error', errMessage);
2018-03-15 01:29:40 +00:00
done.reject(errMessage);
2017-07-07 20:02:19 +00:00
}
2018-03-15 01:29:40 +00:00
return done.promise;
2017-07-07 20:02:19 +00:00
}
2019-11-06 23:26:47 +00:00
// disconnecting ----------------------
public async disconnect() {
this.socket.disconnect(true);
2019-11-08 17:41:08 +00:00
this.updateStatus('disconnected');
}
2020-09-24 18:04:11 +00:00
private updateStatus(statusArg: interfaces.TConnectionStatus) {
2019-11-08 17:41:08 +00:00
if (this.eventStatus !== statusArg) {
this.eventSubject.next(statusArg);
}
this.eventStatus = statusArg;
2019-11-06 23:26:47 +00:00
}
2017-07-07 20:02:19 +00:00
}