Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a2db05bfe3 | |||
| 69668731be | |||
| e586843d81 | |||
| fef1ae338a | |||
| 969b2d6686 | |||
| f4d820d37e | |||
| c08acd076e | |||
| c968de37d1 | |||
| 5b8f7514f4 | |||
| 53874a7772 | |||
| 479cf60239 | |||
| 825a264327 |
89
package-lock.json
generated
89
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiglobal/typedsocket",
|
||||
"version": "1.0.9",
|
||||
"version": "1.0.15",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@@ -1950,9 +1950,9 @@
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartsocket": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsocket/-/smartsocket-1.2.1.tgz",
|
||||
"integrity": "sha512-vZTnkGFa8MpHrBO+87jzzy5XZYfi4VejzlqNnWmt1iXQx3lMitrvEcQPXWvqtwrI1WBysZhmk0AWB4VXbYWOSQ==",
|
||||
"version": "1.2.8",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsocket/-/smartsocket-1.2.8.tgz",
|
||||
"integrity": "sha512-cuNFT8/ESvLXkZt8yqra45LBaRIkwlzdLWgn/CiPlQFRgptJ0tUwnkSb6spsOJQubQ18M8lmcw0HnYkyFoOtFw==",
|
||||
"requires": {
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||
"@pushrocks/isohash": "^1.0.2",
|
||||
@@ -1960,15 +1960,43 @@
|
||||
"@pushrocks/lik": "^4.0.20",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartenv": "^4.0.16",
|
||||
"@pushrocks/smartexpress": "^3.0.99",
|
||||
"@pushrocks/smartexpress": "^3.0.100",
|
||||
"@pushrocks/smartjson": "^4.0.6",
|
||||
"@pushrocks/smartlog": "^2.0.39",
|
||||
"@pushrocks/smartpromise": "^3.1.3",
|
||||
"@pushrocks/smartrx": "^2.0.19",
|
||||
"@pushrocks/smarttime": "^3.0.37",
|
||||
"@types/socket.io": "^2.1.12",
|
||||
"@types/socket.io-client": "^1.4.34",
|
||||
"socket.io": "^3.0.4",
|
||||
"socket.io-client": "^3.0.4"
|
||||
"@pushrocks/smarttime": "^3.0.38",
|
||||
"@types/socket.io": "^2.1.13",
|
||||
"@types/socket.io-client": "^1.4.35",
|
||||
"socket.io": "^3.1.0",
|
||||
"socket.io-client": "^3.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartjson": {
|
||||
"version": "4.0.6",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartjson/-/smartjson-4.0.6.tgz",
|
||||
"integrity": "sha512-lykr068RSDHs0+EXCvIDVxjKnDtRQ2M7EXOo5jVrUU6/OEdfRl9ErM1K/oPafiEi47/PtTrwLlp1KdSgqkRjmg==",
|
||||
"requires": {
|
||||
"@types/buffer-json": "^2.0.0",
|
||||
"@types/fast-json-stable-stringify": "^2.0.0",
|
||||
"buffer-json": "^2.0.0",
|
||||
"fast-json-stable-stringify": "^2.1.0",
|
||||
"lodash.clonedeep": "^4.5.0"
|
||||
}
|
||||
},
|
||||
"@pushrocks/smarttime": {
|
||||
"version": "3.0.38",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarttime/-/smarttime-3.0.38.tgz",
|
||||
"integrity": "sha512-oVT48eZE66OTmDALAsWkk3GXUQiqzkei7mo526UbuMN2iLY8I9WgsxmLfa8Fp4llQmocncW3WTqGyhnEOAuDgA==",
|
||||
"requires": {
|
||||
"@pushrocks/lik": "^4.0.17",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"croner": "^1.1.23",
|
||||
"dayjs": "^1.9.1",
|
||||
"is-nan": "^1.3.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartstring": {
|
||||
@@ -2036,15 +2064,30 @@
|
||||
}
|
||||
},
|
||||
"@pushrocks/tapbundle": {
|
||||
"version": "3.2.9",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2ftapbundle/-/tapbundle-3.2.9.tgz",
|
||||
"integrity": "sha512-vtmYL/l7BZvAzySh7cYnnTG6CFMp5zYtowJuMAmqUjhIaQaWW1Tvbrpjp7lVwRXj2JlL/i69KcJ6RVdLItK+rA==",
|
||||
"version": "3.2.10",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2ftapbundle/-/tapbundle-3.2.10.tgz",
|
||||
"integrity": "sha512-EPFSiFMx6uxWcZgANT1yDigTknVl1FCRIaiuf2AEkpg4vR5ZIZQKDIWkrFZY0ajUWJ6vCM631TmIcJvUnyWJXw==",
|
||||
"requires": {
|
||||
"@pushrocks/smartdelay": "^2.0.9",
|
||||
"@pushrocks/smartenv": "^4.0.10",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smarttime": "^3.0.19",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartenv": "^4.0.16",
|
||||
"@pushrocks/smartpromise": "^3.1.3",
|
||||
"@pushrocks/smarttime": "^3.0.38",
|
||||
"smartchai": "^2.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smarttime": {
|
||||
"version": "3.0.38",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarttime/-/smarttime-3.0.38.tgz",
|
||||
"integrity": "sha512-oVT48eZE66OTmDALAsWkk3GXUQiqzkei7mo526UbuMN2iLY8I9WgsxmLfa8Fp4llQmocncW3WTqGyhnEOAuDgA==",
|
||||
"requires": {
|
||||
"@pushrocks/lik": "^4.0.17",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"croner": "^1.1.23",
|
||||
"dayjs": "^1.9.1",
|
||||
"is-nan": "^1.3.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"@pushrocks/webrequest": {
|
||||
@@ -2418,9 +2461,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"@types/socket.io": {
|
||||
"version": "2.1.12",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fsocket.io/-/socket.io-2.1.12.tgz",
|
||||
"integrity": "sha512-oStc5VFkpb0AsjOxQUj9ztX5Iziatyla/rjZTYbFGoVrrKwd+JU2mtxk7iSl5RGYx9WunLo6UXW1fBzQok/ZyA==",
|
||||
"version": "2.1.13",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fsocket.io/-/socket.io-2.1.13.tgz",
|
||||
"integrity": "sha512-JRgH3nCgsWel4OPANkhH8TelpXvacAJ9VeryjuqCDiaVDMpLysd6sbt0dr6Z15pqH3p2YpOT3T1C5vQ+O/7uyg==",
|
||||
"requires": {
|
||||
"@types/engine.io": "*",
|
||||
"@types/node": "*",
|
||||
@@ -8134,9 +8177,9 @@
|
||||
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
|
||||
},
|
||||
"pathval": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://verdaccio.lossless.one/pathval/-/pathval-1.1.0.tgz",
|
||||
"integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA="
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://verdaccio.lossless.one/pathval/-/pathval-1.1.1.tgz",
|
||||
"integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ=="
|
||||
},
|
||||
"pbkdf2": {
|
||||
"version": "3.1.1",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiglobal/typedsocket",
|
||||
"version": "1.0.9",
|
||||
"version": "1.0.15",
|
||||
"private": false,
|
||||
"description": "a typedrequest extension supporting websockets",
|
||||
"main": "dist_ts/index.js",
|
||||
@@ -15,7 +15,7 @@
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsbundle": "^1.0.78",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@pushrocks/tapbundle": "^3.2.10",
|
||||
"@types/node": "^14.14.22",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
@@ -25,7 +25,7 @@
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||
"@pushrocks/isohash": "^1.0.2",
|
||||
"@pushrocks/smartexpress": "^3.0.100",
|
||||
"@pushrocks/smartsocket": "^1.2.1",
|
||||
"@pushrocks/smartsocket": "^1.2.8",
|
||||
"@pushrocks/smartstring": "^3.0.24"
|
||||
},
|
||||
"browserslist": [
|
||||
|
||||
@@ -33,7 +33,7 @@ tap.test('should add some handlers', async () => {
|
||||
|
||||
tap.test('should create Server and Client', async (tools) => {
|
||||
testTypedSocketServer = await typedsocket.TypedSocket.createServer(testTypedRouter);
|
||||
testTypedSocketClient = await typedsocket.TypedSocket.createClient(testTypedRouter, 'http://localhost');
|
||||
testTypedSocketClient = await typedsocket.TypedSocket.createClient(testTypedRouter, 'http://localhost:3000');
|
||||
});
|
||||
|
||||
tap.test('should process messages from both sides', async () => {
|
||||
|
||||
@@ -4,6 +4,8 @@ import type * as smartexpress from '@pushrocks/smartexpress';
|
||||
const publicRoleName = 'publicRoleName';
|
||||
const publicRolePass = 'publicRolePass';
|
||||
|
||||
export type TTypedSocketSide = 'server' | 'client';
|
||||
|
||||
export class TypedSocket {
|
||||
// STATIC
|
||||
/**
|
||||
@@ -35,6 +37,7 @@ export class TypedSocket {
|
||||
})
|
||||
);
|
||||
const typedsocket = new TypedSocket(
|
||||
'server',
|
||||
typedrouterArg,
|
||||
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||
dataArg: T,
|
||||
@@ -59,9 +62,6 @@ export class TypedSocket {
|
||||
},
|
||||
smartsocketServer
|
||||
);
|
||||
if (smartexpressServerArg && smartexpressServerArg.serverStatus !== 'running') {
|
||||
await smartexpressServerArg.start()
|
||||
}
|
||||
await smartsocketServer.start();
|
||||
|
||||
return typedsocket;
|
||||
@@ -74,15 +74,17 @@ export class TypedSocket {
|
||||
): Promise<TypedSocket> {
|
||||
const domain = new plugins.smartstring.Domain(serverUrlArg);
|
||||
|
||||
|
||||
const smartsocketClient = new plugins.smartsocket.SmartsocketClient({
|
||||
const socketOptions = {
|
||||
alias: aliasArg,
|
||||
role: publicRoleName,
|
||||
password: publicRolePass,
|
||||
port: domain.port || 3000,
|
||||
url: `${domain.nodeParsedUrl.protocol}//${domain.nodeParsedUrl.host}`,
|
||||
url: `${domain.nodeParsedUrl.protocol}//${domain.nodeParsedUrl.hostname}`,
|
||||
autoReconnect: true,
|
||||
});
|
||||
}
|
||||
console.log(`starting typedsocket with the following settings:`)
|
||||
console.log(socketOptions);
|
||||
const smartsocketClient = new plugins.smartsocket.SmartsocketClient(socketOptions);
|
||||
smartsocketClient.addSocketFunction(
|
||||
new plugins.smartsocket.SocketFunction({
|
||||
funcName: 'processMessage',
|
||||
@@ -93,6 +95,7 @@ export class TypedSocket {
|
||||
})
|
||||
);
|
||||
const typedsocket = new TypedSocket(
|
||||
'client',
|
||||
typedrouterArg,
|
||||
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(dataArg: T): Promise<T> => {
|
||||
const response: T = (smartsocketClient.serverCall('processMessage', dataArg) as any) as T;
|
||||
@@ -107,6 +110,7 @@ export class TypedSocket {
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public side: TTypedSocketSide;
|
||||
public typedrouter: plugins.typedrequest.TypedRouter;
|
||||
private postMethod: plugins.typedrequest.IPostMethod &
|
||||
((
|
||||
@@ -115,15 +119,28 @@ export class TypedSocket {
|
||||
) => Promise<plugins.typedrequestInterfaces.ITypedRequest>);
|
||||
private socketServerOrClient: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient;
|
||||
constructor(
|
||||
sideArg: TTypedSocketSide,
|
||||
typedrouterArg: plugins.typedrequest.TypedRouter,
|
||||
postMethodArg: plugins.typedrequest.IPostMethod,
|
||||
socketServerOrClientArg: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient
|
||||
) {
|
||||
this.side = sideArg;
|
||||
this.typedrouter = typedrouterArg;
|
||||
this.postMethod = postMethodArg;
|
||||
this.socketServerOrClient = socketServerOrClientArg;
|
||||
}
|
||||
|
||||
public addTag(keyArg: string, payloadArg: any) {
|
||||
if (this.side === 'client' && this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient) {
|
||||
this.socketServerOrClient.socketConnection.addTag({
|
||||
id: keyArg,
|
||||
payload: payloadArg
|
||||
})
|
||||
} else {
|
||||
throw new Error('tagging is only supported on clients');
|
||||
}
|
||||
}
|
||||
|
||||
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||
methodName: T['method'],
|
||||
targetConnection?: plugins.smartsocket.SocketConnection
|
||||
@@ -140,20 +157,46 @@ export class TypedSocket {
|
||||
return typedrequest;
|
||||
}
|
||||
|
||||
public async findTargetConnection(
|
||||
findFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => boolean
|
||||
public async findAllTargetConnections(
|
||||
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
|
||||
) {
|
||||
if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) {
|
||||
const matchingSockets: plugins.smartsocket.SocketConnection[] = [];
|
||||
for (const socketConnection of this.socketServerOrClient.socketConnections.getArray()) {
|
||||
if (findFuncArg(socketConnection)) {
|
||||
return socketConnection;
|
||||
if (await asyncFindFuncArg(socketConnection)) {
|
||||
matchingSockets.push(socketConnection);
|
||||
}
|
||||
}
|
||||
return matchingSockets;
|
||||
} else {
|
||||
console.warn('this method >>findTargetConnection<< is only available from the server');
|
||||
throw new Error('this method >>findTargetConnection<< is only available from the server');
|
||||
}
|
||||
}
|
||||
|
||||
public async findTargetConnection(
|
||||
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
|
||||
) {
|
||||
const allMatching = this.findAllTargetConnections(asyncFindFuncArg);
|
||||
return allMatching[0];
|
||||
}
|
||||
|
||||
public async findAllTargetConnectionsByTag(keyArg: string, payloadArg?: any) {
|
||||
return this.findAllTargetConnections(async socketConnectionArg => {
|
||||
let result: boolean;
|
||||
if (!payloadArg) {
|
||||
result = !!socketConnectionArg.getTagById('keyArg')
|
||||
} else {
|
||||
result = !!socketConnectionArg.getTagById('keyArg') === payloadArg;
|
||||
}
|
||||
return result;
|
||||
})
|
||||
}
|
||||
|
||||
public async findTargetConnectionByTag(keyArg: string, payloadArg?: any) {
|
||||
const allResults = this.findAllTargetConnectionsByTag(keyArg, payloadArg)
|
||||
return allResults[0];
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
await this.socketServerOrClient.stop()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user