2022-03-24 21:46:08 +00:00
|
|
|
import * as plugins from './typedsocket.plugins.js';
|
2020-12-21 21:01:37 +00:00
|
|
|
|
2020-12-26 18:14:19 +00:00
|
|
|
const publicRoleName = 'publicRoleName';
|
|
|
|
const publicRolePass = 'publicRolePass';
|
|
|
|
|
2022-03-07 23:14:40 +00:00
|
|
|
export type TTypedSocketSide = 'server' | 'client';
|
2021-01-28 02:12:22 +00:00
|
|
|
|
2020-12-21 21:01:37 +00:00
|
|
|
export class TypedSocket {
|
2020-12-26 18:14:19 +00:00
|
|
|
// STATIC
|
|
|
|
/**
|
|
|
|
* creates a typedsocket server
|
|
|
|
* note: this will fail in browser environments as server libs are not bundled.
|
|
|
|
*/
|
2022-10-26 08:54:36 +00:00
|
|
|
public static async createServer(
|
2020-12-26 18:14:19 +00:00
|
|
|
typedrouterArg: plugins.typedrequest.TypedRouter,
|
2021-10-22 22:27:46 +00:00
|
|
|
smartexpressServerArg?: any
|
2020-12-26 18:14:19 +00:00
|
|
|
): Promise<TypedSocket> {
|
|
|
|
const smartsocketServer = new plugins.smartsocket.Smartsocket({
|
2022-01-19 18:17:22 +00:00
|
|
|
alias: 'typedsocketServer',
|
2020-12-26 18:14:19 +00:00
|
|
|
port: 3000,
|
|
|
|
});
|
|
|
|
if (smartexpressServerArg) {
|
|
|
|
smartsocketServer.setExternalServer('smartexpress', smartexpressServerArg);
|
|
|
|
}
|
2022-03-07 23:14:40 +00:00
|
|
|
|
2020-12-26 18:14:19 +00:00
|
|
|
smartsocketServer.socketFunctions.add(
|
|
|
|
new plugins.smartsocket.SocketFunction({
|
|
|
|
funcName: 'processMessage',
|
|
|
|
funcDef: async (dataArg, socketConnectionArg) => {
|
|
|
|
return typedrouterArg.routeAndAddResponse(dataArg);
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const typedsocket = new TypedSocket(
|
2021-01-28 02:12:22 +00:00
|
|
|
'server',
|
2020-12-26 18:14:19 +00:00
|
|
|
typedrouterArg,
|
2022-10-26 08:54:36 +00:00
|
|
|
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
2020-12-26 18:14:19 +00:00
|
|
|
dataArg: T,
|
|
|
|
targetConnectionArg?: plugins.smartsocket.SocketConnection
|
|
|
|
): Promise<T> => {
|
|
|
|
if (!targetConnectionArg) {
|
|
|
|
if ((smartsocketServer.socketConnections.getArray().length = 1)) {
|
|
|
|
console.log(
|
|
|
|
'Since no targetConnection was supplied and there is only one active one present, choosing that one automatically'
|
|
|
|
);
|
2022-10-26 08:54:36 +00:00
|
|
|
targetConnectionArg = smartsocketServer.socketConnections.getArray()[0];
|
2020-12-26 18:14:19 +00:00
|
|
|
} else {
|
2022-10-26 08:54:36 +00:00
|
|
|
throw new Error(
|
|
|
|
'you need to specify the wanted targetConnection. Currently no target is selectable automatically.'
|
|
|
|
);
|
2020-12-26 18:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-26 08:54:36 +00:00
|
|
|
const response: T = (await smartsocketServer.clientCall(
|
2020-12-26 18:14:19 +00:00
|
|
|
'processMessage',
|
|
|
|
dataArg,
|
|
|
|
targetConnectionArg
|
2022-10-26 08:54:36 +00:00
|
|
|
)) as any;
|
2020-12-26 18:14:19 +00:00
|
|
|
return response;
|
|
|
|
},
|
|
|
|
smartsocketServer
|
|
|
|
);
|
|
|
|
await smartsocketServer.start();
|
|
|
|
|
|
|
|
return typedsocket;
|
|
|
|
}
|
|
|
|
|
2022-10-26 08:54:36 +00:00
|
|
|
public static async createClient(
|
2020-12-26 18:14:19 +00:00
|
|
|
typedrouterArg: plugins.typedrequest.TypedRouter,
|
2021-01-21 14:26:48 +00:00
|
|
|
serverUrlArg: string,
|
|
|
|
aliasArg = 'clientArg'
|
2020-12-26 18:14:19 +00:00
|
|
|
): Promise<TypedSocket> {
|
2021-01-22 15:13:05 +00:00
|
|
|
const domain = new plugins.smartstring.Domain(serverUrlArg);
|
|
|
|
|
2022-01-19 18:17:22 +00:00
|
|
|
const socketOptions: plugins.smartsocket.ISmartsocketClientOptions = {
|
2021-01-21 14:26:48 +00:00
|
|
|
alias: aliasArg,
|
2021-01-22 15:13:05 +00:00
|
|
|
port: domain.port || 3000,
|
2021-01-23 05:14:29 +00:00
|
|
|
url: `${domain.nodeParsedUrl.protocol}//${domain.nodeParsedUrl.hostname}`,
|
2020-12-26 18:14:19 +00:00
|
|
|
autoReconnect: true,
|
2022-10-26 08:54:36 +00:00
|
|
|
};
|
|
|
|
console.log(`starting typedsocket with the following settings:`);
|
2021-01-23 05:14:29 +00:00
|
|
|
console.log(socketOptions);
|
|
|
|
const smartsocketClient = new plugins.smartsocket.SmartsocketClient(socketOptions);
|
2020-12-26 18:45:17 +00:00
|
|
|
smartsocketClient.addSocketFunction(
|
2020-12-26 18:14:19 +00:00
|
|
|
new plugins.smartsocket.SocketFunction({
|
|
|
|
funcName: 'processMessage',
|
|
|
|
funcDef: async (dataArg, socketConnectionArg) => {
|
|
|
|
return typedrouterArg.routeAndAddResponse(dataArg);
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const typedsocket = new TypedSocket(
|
2021-01-28 02:12:22 +00:00
|
|
|
'client',
|
2020-12-26 18:14:19 +00:00
|
|
|
typedrouterArg,
|
2022-10-26 08:54:36 +00:00
|
|
|
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(dataArg: T): Promise<T> => {
|
|
|
|
const response: T = smartsocketClient.serverCall('processMessage', dataArg) as any as T;
|
2020-12-26 18:14:19 +00:00
|
|
|
return response;
|
|
|
|
},
|
|
|
|
smartsocketClient
|
|
|
|
);
|
2023-03-20 16:06:33 +00:00
|
|
|
console.log(`typedsocket triggering smartsocket to connect...`);
|
|
|
|
const before = Date.now();
|
2020-12-26 18:14:19 +00:00
|
|
|
await smartsocketClient.connect();
|
2023-03-20 16:06:33 +00:00
|
|
|
console.log(`typedsocket triggered smartsocket connected in ${Date.now() - before}ms!!!`)
|
2020-12-26 18:14:19 +00:00
|
|
|
|
|
|
|
return typedsocket;
|
|
|
|
}
|
|
|
|
|
2023-03-29 11:47:03 +00:00
|
|
|
public static useWindowLocationOriginUrl = () => {
|
|
|
|
const windowLocationResult = plugins.smarturl.Smarturl.createFromUrl(globalThis.location.origin).toString();
|
|
|
|
return windowLocationResult;
|
|
|
|
}
|
|
|
|
|
2020-12-26 18:14:19 +00:00
|
|
|
// INSTANCE
|
2021-01-28 02:12:22 +00:00
|
|
|
public side: TTypedSocketSide;
|
2020-12-26 18:14:19 +00:00
|
|
|
public typedrouter: plugins.typedrequest.TypedRouter;
|
2023-03-29 14:29:58 +00:00
|
|
|
public get eventSubject() {
|
|
|
|
return this.socketServerOrClient.eventSubject;
|
|
|
|
}
|
2020-12-26 18:14:19 +00:00
|
|
|
private postMethod: plugins.typedrequest.IPostMethod &
|
|
|
|
((
|
|
|
|
typedRequestPostObject: plugins.typedrequestInterfaces.ITypedRequest,
|
|
|
|
socketConnectionArg?: plugins.smartsocket.SocketConnection
|
|
|
|
) => Promise<plugins.typedrequestInterfaces.ITypedRequest>);
|
2022-10-26 08:54:36 +00:00
|
|
|
private socketServerOrClient:
|
|
|
|
| plugins.smartsocket.Smartsocket
|
|
|
|
| plugins.smartsocket.SmartsocketClient;
|
2020-12-26 18:14:19 +00:00
|
|
|
constructor(
|
2021-01-28 02:12:22 +00:00
|
|
|
sideArg: TTypedSocketSide,
|
2020-12-26 18:14:19 +00:00
|
|
|
typedrouterArg: plugins.typedrequest.TypedRouter,
|
|
|
|
postMethodArg: plugins.typedrequest.IPostMethod,
|
|
|
|
socketServerOrClientArg: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient
|
|
|
|
) {
|
2021-01-28 02:12:22 +00:00
|
|
|
this.side = sideArg;
|
2020-12-26 18:14:19 +00:00
|
|
|
this.typedrouter = typedrouterArg;
|
|
|
|
this.postMethod = postMethodArg;
|
|
|
|
this.socketServerOrClient = socketServerOrClientArg;
|
|
|
|
}
|
|
|
|
|
2022-12-28 14:15:19 +00:00
|
|
|
public addTag<T extends plugins.typedrequestInterfaces.ITag = any>(
|
|
|
|
nameArg: T['name'],
|
|
|
|
payloadArg: T['payload']
|
|
|
|
) {
|
2022-10-26 08:54:36 +00:00
|
|
|
if (
|
|
|
|
this.side === 'client' &&
|
|
|
|
this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient
|
|
|
|
) {
|
2021-01-28 02:12:22 +00:00
|
|
|
this.socketServerOrClient.socketConnection.addTag({
|
2022-10-26 20:39:39 +00:00
|
|
|
id: nameArg,
|
2022-10-26 08:54:36 +00:00
|
|
|
payload: payloadArg,
|
|
|
|
});
|
2021-01-28 02:12:22 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('tagging is only supported on clients');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 08:54:36 +00:00
|
|
|
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
|
|
|
methodName: T['method'],
|
2020-12-26 18:14:19 +00:00
|
|
|
targetConnection?: plugins.smartsocket.SocketConnection
|
|
|
|
): plugins.typedrequest.TypedRequest<T> {
|
|
|
|
const typedrequest = new plugins.typedrequest.TypedRequest<T>(
|
|
|
|
new plugins.typedrequest.TypedTarget({
|
|
|
|
postMethod: async (requestDataArg) => {
|
|
|
|
const result = await this.postMethod(requestDataArg, targetConnection);
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
methodName
|
|
|
|
);
|
|
|
|
return typedrequest;
|
|
|
|
}
|
2020-12-21 21:01:37 +00:00
|
|
|
|
2021-10-22 19:09:42 +00:00
|
|
|
/**
|
|
|
|
* returns all matching target connection
|
|
|
|
* @param asyncFindFuncArg
|
2022-10-26 08:54:36 +00:00
|
|
|
* @returns
|
2021-10-22 19:09:42 +00:00
|
|
|
*/
|
2022-10-26 08:54:36 +00:00
|
|
|
public async findAllTargetConnections(
|
2021-01-28 12:58:42 +00:00
|
|
|
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
|
2020-12-26 18:14:19 +00:00
|
|
|
) {
|
|
|
|
if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) {
|
2021-01-28 12:58:42 +00:00
|
|
|
const matchingSockets: plugins.smartsocket.SocketConnection[] = [];
|
2020-12-26 18:14:19 +00:00
|
|
|
for (const socketConnection of this.socketServerOrClient.socketConnections.getArray()) {
|
2021-01-28 12:58:42 +00:00
|
|
|
if (await asyncFindFuncArg(socketConnection)) {
|
|
|
|
matchingSockets.push(socketConnection);
|
2020-12-26 18:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 12:58:42 +00:00
|
|
|
return matchingSockets;
|
2020-12-26 18:14:19 +00:00
|
|
|
} else {
|
2021-01-28 02:12:22 +00:00
|
|
|
throw new Error('this method >>findTargetConnection<< is only available from the server');
|
2020-12-26 18:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-21 21:01:37 +00:00
|
|
|
|
2021-10-22 19:09:42 +00:00
|
|
|
/**
|
|
|
|
* returns a single target connection by returning the first one of all matching ones
|
|
|
|
* @param asyncFindFuncArg
|
2022-10-26 08:54:36 +00:00
|
|
|
* @returns
|
2021-10-22 19:09:42 +00:00
|
|
|
*/
|
2022-10-26 08:54:36 +00:00
|
|
|
public async findTargetConnection(
|
2021-01-28 12:58:42 +00:00
|
|
|
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
|
|
|
|
) {
|
2021-10-22 19:09:42 +00:00
|
|
|
const allMatching = await this.findAllTargetConnections(asyncFindFuncArg);
|
2022-10-26 08:54:36 +00:00
|
|
|
return allMatching[0];
|
2021-01-28 12:58:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 14:15:19 +00:00
|
|
|
public async findAllTargetConnectionsByTag<
|
|
|
|
TTag extends plugins.typedrequestInterfaces.ITag = any
|
|
|
|
>(keyArg: TTag['name'], payloadArg?: TTag['payload']) {
|
2022-10-26 08:54:36 +00:00
|
|
|
return this.findAllTargetConnections(async (socketConnectionArg) => {
|
2021-01-28 02:12:22 +00:00
|
|
|
let result: boolean;
|
2021-01-28 12:58:42 +00:00
|
|
|
if (!payloadArg) {
|
2022-03-07 23:03:42 +00:00
|
|
|
result = !!(await socketConnectionArg.getTagById(keyArg));
|
2021-01-28 02:12:22 +00:00
|
|
|
} else {
|
2022-10-26 08:54:18 +00:00
|
|
|
result = !!(
|
2022-10-26 08:54:36 +00:00
|
|
|
plugins.smartjson.stringify((await socketConnectionArg.getTagById(keyArg))?.payload) ===
|
|
|
|
plugins.smartjson.stringify(payloadArg)
|
2022-10-26 08:54:18 +00:00
|
|
|
);
|
2021-01-28 02:12:22 +00:00
|
|
|
}
|
|
|
|
return result;
|
2022-10-26 08:54:36 +00:00
|
|
|
});
|
2021-01-28 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 14:15:19 +00:00
|
|
|
public async findTargetConnectionByTag<TTag extends plugins.typedrequestInterfaces.ITag = any>(
|
|
|
|
keyArg: TTag['name'],
|
|
|
|
payloadArg?: TTag['payload']
|
|
|
|
) {
|
2022-10-26 08:54:36 +00:00
|
|
|
const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg);
|
|
|
|
return allResults[0];
|
2021-01-28 12:58:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 08:54:36 +00:00
|
|
|
public async stop() {
|
|
|
|
await this.socketServerOrClient.stop();
|
2020-12-26 18:14:19 +00:00
|
|
|
}
|
|
|
|
}
|