Compare commits

...

12 Commits

Author SHA1 Message Date
c5e60d804a 1.2.21 2022-01-20 18:33:47 +01:00
c5d52013e6 fix(core): update 2022-01-20 18:33:46 +01:00
64195e4c78 1.2.20 2022-01-20 17:14:12 +01:00
01b5b3dc1a fix(core): update 2022-01-20 17:14:11 +01:00
b34d7faec2 1.2.19 2022-01-20 16:50:26 +01:00
7c98e19988 fix(core): update 2022-01-20 16:50:25 +01:00
608669ec44 1.2.18 2022-01-19 19:52:16 +01:00
a7b14cd383 fix(core): update 2022-01-19 19:52:14 +01:00
21dc933302 1.2.17 2022-01-19 19:05:45 +01:00
4e7455fa26 fix(core): update 2022-01-19 19:05:44 +01:00
c424d589ea 1.2.16 2022-01-19 18:36:14 +01:00
9435796333 fix(core): update 2022-01-19 18:36:13 +01:00
11 changed files with 261 additions and 354 deletions

285
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@pushrocks/smartsocket", "name": "@pushrocks/smartsocket",
"version": "1.2.15", "version": "1.2.21",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@pushrocks/smartsocket", "name": "@pushrocks/smartsocket",
"version": "1.2.15", "version": "1.2.21",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.15", "@apiglobal/typedrequest-interfaces": "^1.0.15",
@ -2620,9 +2620,9 @@
} }
}, },
"node_modules/@pushrocks/smartsocket": { "node_modules/@pushrocks/smartsocket": {
"version": "1.2.14", "version": "1.2.15",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsocket/-/smartsocket-1.2.14.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsocket/-/smartsocket-1.2.15.tgz",
"integrity": "sha512-X55iqiQhfHCMr4//4zqcmDRvpseXe7liMvJtS77zaEN5YVCYMMOrVDToNWP0fHRAaoRtJvBpvRYqSv5OeapiNA==", "integrity": "sha512-2BEEnqd7h8UHIKSNno91ZyYAqtlWgjyC3OkfyH3k/Q0zcD9D0PiekUXeSl2cvsq7sso/3J9mN8OJEESyh6GxTQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.15", "@apiglobal/typedrequest-interfaces": "^1.0.15",
@ -2637,131 +2637,8 @@
"@pushrocks/smartpromise": "^3.1.6", "@pushrocks/smartpromise": "^3.1.6",
"@pushrocks/smartrx": "^2.0.19", "@pushrocks/smartrx": "^2.0.19",
"@pushrocks/smarttime": "^3.0.43", "@pushrocks/smarttime": "^3.0.43",
"@types/socket.io": "^3.0.2", "socket.io": "^4.4.1",
"@types/socket.io-client": "^3.0.0", "socket.io-client": "^4.4.1"
"socket.io": "^3.1.0",
"socket.io-client": "^3.1.0"
}
},
"node_modules/@pushrocks/smartsocket/node_modules/engine.io": {
"version": "4.1.2",
"resolved": "https://verdaccio.lossless.one/engine.io/-/engine.io-4.1.2.tgz",
"integrity": "sha512-t5z6zjXuVLhXDMiFJPYsPOWEER8B0tIsD3ETgw19S1yg9zryvUfY3Vhtk3Gf4sihw/bQGIqQ//gjvVlu+Ca0bQ==",
"license": "MIT",
"dependencies": {
"accepts": "~1.3.4",
"base64id": "2.0.0",
"cookie": "~0.4.1",
"cors": "~2.8.5",
"debug": "~4.3.1",
"engine.io-parser": "~4.0.0",
"ws": "~7.4.2"
},
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/@pushrocks/smartsocket/node_modules/engine.io-client": {
"version": "4.1.4",
"resolved": "https://verdaccio.lossless.one/engine.io-client/-/engine.io-client-4.1.4.tgz",
"integrity": "sha512-843fqAdKeUMFqKi1sSjnR11tJ4wi8sIefu6+JC1OzkkJBmjtc/gM/rZ53tJfu5Iae/3gApm5veoS+v+gtT0+Fg==",
"license": "MIT",
"dependencies": {
"base64-arraybuffer": "0.1.4",
"component-emitter": "~1.3.0",
"debug": "~4.3.1",
"engine.io-parser": "~4.0.1",
"has-cors": "1.1.0",
"parseqs": "0.0.6",
"parseuri": "0.0.6",
"ws": "~7.4.2",
"xmlhttprequest-ssl": "~1.6.2",
"yeast": "0.1.2"
}
},
"node_modules/@pushrocks/smartsocket/node_modules/engine.io-parser": {
"version": "4.0.3",
"resolved": "https://verdaccio.lossless.one/engine.io-parser/-/engine.io-parser-4.0.3.tgz",
"integrity": "sha512-xEAAY0msNnESNPc00e19y5heTPX4y/TJ36gr8t1voOaNmTojP9b3oK3BbJLFufW2XFPQaaijpFewm2g2Um3uqA==",
"license": "MIT",
"dependencies": {
"base64-arraybuffer": "0.1.4"
},
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/@pushrocks/smartsocket/node_modules/socket.io": {
"version": "3.1.2",
"resolved": "https://verdaccio.lossless.one/socket.io/-/socket.io-3.1.2.tgz",
"integrity": "sha512-JubKZnTQ4Z8G4IZWtaAZSiRP3I/inpy8c/Bsx2jrwGrTbKeVU5xd6qkKMHpChYeM3dWZSO0QACiGK+obhBNwYw==",
"license": "MIT",
"dependencies": {
"@types/cookie": "^0.4.0",
"@types/cors": "^2.8.8",
"@types/node": ">=10.0.0",
"accepts": "~1.3.4",
"base64id": "~2.0.0",
"debug": "~4.3.1",
"engine.io": "~4.1.0",
"socket.io-adapter": "~2.1.0",
"socket.io-parser": "~4.0.3"
},
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/@pushrocks/smartsocket/node_modules/socket.io-adapter": {
"version": "2.1.0",
"resolved": "https://verdaccio.lossless.one/socket.io-adapter/-/socket.io-adapter-2.1.0.tgz",
"integrity": "sha512-+vDov/aTsLjViYTwS9fPy5pEtTkrbEKsw2M+oVSoFGw6OD1IpvlV1VPhUzNbofCQ8oyMbdYJqDtGdmHQK6TdPg==",
"license": "MIT"
},
"node_modules/@pushrocks/smartsocket/node_modules/socket.io-client": {
"version": "3.1.3",
"resolved": "https://verdaccio.lossless.one/socket.io-client/-/socket.io-client-3.1.3.tgz",
"integrity": "sha512-4sIGOGOmCg3AOgGi7EEr6ZkTZRkrXwub70bBB/F0JSkMOUFpA77WsL87o34DffQQ31PkbMUIadGOk+3tx1KGbw==",
"license": "MIT",
"dependencies": {
"@types/component-emitter": "^1.2.10",
"backo2": "~1.0.2",
"component-emitter": "~1.3.0",
"debug": "~4.3.1",
"engine.io-client": "~4.1.0",
"parseuri": "0.0.6",
"socket.io-parser": "~4.0.4"
},
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/@pushrocks/smartsocket/node_modules/ws": {
"version": "7.4.6",
"resolved": "https://verdaccio.lossless.one/ws/-/ws-7.4.6.tgz",
"integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==",
"license": "MIT",
"engines": {
"node": ">=8.3.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": "^5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
},
"node_modules/@pushrocks/smartsocket/node_modules/xmlhttprequest-ssl": {
"version": "1.6.3",
"resolved": "https://verdaccio.lossless.one/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.6.3.tgz",
"integrity": "sha512-3XfeQE/wNkvrIktn2Kf0869fC0BN6UpydVasGIeSm2B1Llihf7/0UfZM+eCkOw3P7bP4+qPgqhm7ZoxuJtFU0Q==",
"engines": {
"node": ">=0.4.0"
} }
}, },
"node_modules/@pushrocks/smartspawn": { "node_modules/@pushrocks/smartspawn": {
@ -3398,26 +3275,6 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/socket.io": {
"version": "3.0.2",
"resolved": "https://verdaccio.lossless.one/@types%2fsocket.io/-/socket.io-3.0.2.tgz",
"integrity": "sha512-pu0sN9m5VjCxBZVK8hW37ZcMe8rjn4HHggBN5CbaRTvFwv5jOmuIRZEuddsBPa9Th0ts0SIo3Niukq+95cMBbQ==",
"deprecated": "This is a stub types definition. socket.io provides its own type definitions, so you do not need this installed.",
"license": "MIT",
"dependencies": {
"socket.io": "*"
}
},
"node_modules/@types/socket.io-client": {
"version": "3.0.0",
"resolved": "https://verdaccio.lossless.one/@types%2fsocket.io-client/-/socket.io-client-3.0.0.tgz",
"integrity": "sha512-s+IPvFoEIjKA3RdJz/Z2dGR4gLgysKi8owcnrVwNjgvc01Lk68LJDDsG2GRqegFITcxmvCMYM7bhMpwEMlHmDg==",
"deprecated": "This is a stub types definition. socket.io-client provides its own type definitions, so you do not need this installed.",
"license": "MIT",
"dependencies": {
"socket.io-client": "*"
}
},
"node_modules/@types/through2": { "node_modules/@types/through2": {
"version": "2.0.36", "version": "2.0.36",
"resolved": "https://verdaccio.lossless.one/@types%2fthrough2/-/through2-2.0.36.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fthrough2/-/through2-2.0.36.tgz",
@ -4071,14 +3928,6 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/base64-arraybuffer": {
"version": "0.1.4",
"resolved": "https://verdaccio.lossless.one/base64-arraybuffer/-/base64-arraybuffer-0.1.4.tgz",
"integrity": "sha1-mBjHngWbE1X5fgQooBfIOOkLqBI=",
"engines": {
"node": ">= 0.6.0"
}
},
"node_modules/base64-js": { "node_modules/base64-js": {
"version": "1.5.1", "version": "1.5.1",
"resolved": "https://verdaccio.lossless.one/base64-js/-/base64-js-1.5.1.tgz", "resolved": "https://verdaccio.lossless.one/base64-js/-/base64-js-1.5.1.tgz",
@ -17498,9 +17347,9 @@
} }
}, },
"@pushrocks/smartsocket": { "@pushrocks/smartsocket": {
"version": "1.2.14", "version": "1.2.15",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsocket/-/smartsocket-1.2.14.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsocket/-/smartsocket-1.2.15.tgz",
"integrity": "sha512-X55iqiQhfHCMr4//4zqcmDRvpseXe7liMvJtS77zaEN5YVCYMMOrVDToNWP0fHRAaoRtJvBpvRYqSv5OeapiNA==", "integrity": "sha512-2BEEnqd7h8UHIKSNno91ZyYAqtlWgjyC3OkfyH3k/Q0zcD9D0PiekUXeSl2cvsq7sso/3J9mN8OJEESyh6GxTQ==",
"requires": { "requires": {
"@apiglobal/typedrequest-interfaces": "^1.0.15", "@apiglobal/typedrequest-interfaces": "^1.0.15",
"@pushrocks/isohash": "^1.0.2", "@pushrocks/isohash": "^1.0.2",
@ -17514,97 +17363,8 @@
"@pushrocks/smartpromise": "^3.1.6", "@pushrocks/smartpromise": "^3.1.6",
"@pushrocks/smartrx": "^2.0.19", "@pushrocks/smartrx": "^2.0.19",
"@pushrocks/smarttime": "^3.0.43", "@pushrocks/smarttime": "^3.0.43",
"@types/socket.io": "^3.0.2", "socket.io": "^4.4.1",
"@types/socket.io-client": "^3.0.0", "socket.io-client": "^4.4.1"
"socket.io": "^3.1.0",
"socket.io-client": "^3.1.0"
},
"dependencies": {
"engine.io": {
"version": "4.1.2",
"resolved": "https://verdaccio.lossless.one/engine.io/-/engine.io-4.1.2.tgz",
"integrity": "sha512-t5z6zjXuVLhXDMiFJPYsPOWEER8B0tIsD3ETgw19S1yg9zryvUfY3Vhtk3Gf4sihw/bQGIqQ//gjvVlu+Ca0bQ==",
"requires": {
"accepts": "~1.3.4",
"base64id": "2.0.0",
"cookie": "~0.4.1",
"cors": "~2.8.5",
"debug": "~4.3.1",
"engine.io-parser": "~4.0.0",
"ws": "~7.4.2"
}
},
"engine.io-client": {
"version": "4.1.4",
"resolved": "https://verdaccio.lossless.one/engine.io-client/-/engine.io-client-4.1.4.tgz",
"integrity": "sha512-843fqAdKeUMFqKi1sSjnR11tJ4wi8sIefu6+JC1OzkkJBmjtc/gM/rZ53tJfu5Iae/3gApm5veoS+v+gtT0+Fg==",
"requires": {
"base64-arraybuffer": "0.1.4",
"component-emitter": "~1.3.0",
"debug": "~4.3.1",
"engine.io-parser": "~4.0.1",
"has-cors": "1.1.0",
"parseqs": "0.0.6",
"parseuri": "0.0.6",
"ws": "~7.4.2",
"xmlhttprequest-ssl": "~1.6.2",
"yeast": "0.1.2"
}
},
"engine.io-parser": {
"version": "4.0.3",
"resolved": "https://verdaccio.lossless.one/engine.io-parser/-/engine.io-parser-4.0.3.tgz",
"integrity": "sha512-xEAAY0msNnESNPc00e19y5heTPX4y/TJ36gr8t1voOaNmTojP9b3oK3BbJLFufW2XFPQaaijpFewm2g2Um3uqA==",
"requires": {
"base64-arraybuffer": "0.1.4"
}
},
"socket.io": {
"version": "3.1.2",
"resolved": "https://verdaccio.lossless.one/socket.io/-/socket.io-3.1.2.tgz",
"integrity": "sha512-JubKZnTQ4Z8G4IZWtaAZSiRP3I/inpy8c/Bsx2jrwGrTbKeVU5xd6qkKMHpChYeM3dWZSO0QACiGK+obhBNwYw==",
"requires": {
"@types/cookie": "^0.4.0",
"@types/cors": "^2.8.8",
"@types/node": ">=10.0.0",
"accepts": "~1.3.4",
"base64id": "~2.0.0",
"debug": "~4.3.1",
"engine.io": "~4.1.0",
"socket.io-adapter": "~2.1.0",
"socket.io-parser": "~4.0.3"
}
},
"socket.io-adapter": {
"version": "2.1.0",
"resolved": "https://verdaccio.lossless.one/socket.io-adapter/-/socket.io-adapter-2.1.0.tgz",
"integrity": "sha512-+vDov/aTsLjViYTwS9fPy5pEtTkrbEKsw2M+oVSoFGw6OD1IpvlV1VPhUzNbofCQ8oyMbdYJqDtGdmHQK6TdPg=="
},
"socket.io-client": {
"version": "3.1.3",
"resolved": "https://verdaccio.lossless.one/socket.io-client/-/socket.io-client-3.1.3.tgz",
"integrity": "sha512-4sIGOGOmCg3AOgGi7EEr6ZkTZRkrXwub70bBB/F0JSkMOUFpA77WsL87o34DffQQ31PkbMUIadGOk+3tx1KGbw==",
"requires": {
"@types/component-emitter": "^1.2.10",
"backo2": "~1.0.2",
"component-emitter": "~1.3.0",
"debug": "~4.3.1",
"engine.io-client": "~4.1.0",
"parseuri": "0.0.6",
"socket.io-parser": "~4.0.4"
}
},
"ws": {
"version": "7.4.6",
"resolved": "https://verdaccio.lossless.one/ws/-/ws-7.4.6.tgz",
"integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==",
"requires": {}
},
"xmlhttprequest-ssl": {
"version": "1.6.3",
"resolved": "https://verdaccio.lossless.one/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.6.3.tgz",
"integrity": "sha512-3XfeQE/wNkvrIktn2Kf0869fC0BN6UpydVasGIeSm2B1Llihf7/0UfZM+eCkOw3P7bP4+qPgqhm7ZoxuJtFU0Q=="
}
} }
}, },
"@pushrocks/smartspawn": { "@pushrocks/smartspawn": {
@ -18128,22 +17888,6 @@
"integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=", "integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=",
"dev": true "dev": true
}, },
"@types/socket.io": {
"version": "3.0.2",
"resolved": "https://verdaccio.lossless.one/@types%2fsocket.io/-/socket.io-3.0.2.tgz",
"integrity": "sha512-pu0sN9m5VjCxBZVK8hW37ZcMe8rjn4HHggBN5CbaRTvFwv5jOmuIRZEuddsBPa9Th0ts0SIo3Niukq+95cMBbQ==",
"requires": {
"socket.io": "*"
}
},
"@types/socket.io-client": {
"version": "3.0.0",
"resolved": "https://verdaccio.lossless.one/@types%2fsocket.io-client/-/socket.io-client-3.0.0.tgz",
"integrity": "sha512-s+IPvFoEIjKA3RdJz/Z2dGR4gLgysKi8owcnrVwNjgvc01Lk68LJDDsG2GRqegFITcxmvCMYM7bhMpwEMlHmDg==",
"requires": {
"socket.io-client": "*"
}
},
"@types/through2": { "@types/through2": {
"version": "2.0.36", "version": "2.0.36",
"resolved": "https://verdaccio.lossless.one/@types%2fthrough2/-/through2-2.0.36.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fthrough2/-/through2-2.0.36.tgz",
@ -18648,11 +18392,6 @@
} }
} }
}, },
"base64-arraybuffer": {
"version": "0.1.4",
"resolved": "https://verdaccio.lossless.one/base64-arraybuffer/-/base64-arraybuffer-0.1.4.tgz",
"integrity": "sha1-mBjHngWbE1X5fgQooBfIOOkLqBI="
},
"base64-js": { "base64-js": {
"version": "1.5.1", "version": "1.5.1",
"resolved": "https://verdaccio.lossless.one/base64-js/-/base64-js-1.5.1.tgz", "resolved": "https://verdaccio.lossless.one/base64-js/-/base64-js-1.5.1.tgz",

View File

@ -1,13 +1,12 @@
{ {
"name": "@pushrocks/smartsocket", "name": "@pushrocks/smartsocket",
"version": "1.2.15", "version": "1.2.21",
"description": "easy and secure websocket communication", "description": "easy and secure websocket communication",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"scripts": { "scripts": {
"test": "(tstest test/)", "test": "(tstest test/)",
"build": "tsbuild --web --skiplibcheck", "build": "(tsbuild --web && tsbundle --from ./ts/index.ts --to dist_bundle/bundle.js)"
"buildswitch": "(tsbuild --web && tsbundle --from ./ts/index.ts --to dist_bundle/bundle.js)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,5 +1,151 @@
import { tap, expect } from '@pushrocks/tapbundle'; // tslint:disable-next-line:no-implicit-dependencies
import { expect, tap } from '@pushrocks/tapbundle';
tap.test('should run a test', async () => {}); import smartsocket = require('../ts/index');
let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
let testSocketFunctionClient: smartsocket.SocketFunction<any>;
export interface IReqResClient {
method: 'testFunction1';
request: {
value1: string;
};
response: {
value1: string;
};
}
export interface IReqResServer {
method: 'testFunction2';
request: {
hi: string;
};
response: {
hi: string;
};
}
const testConfig = {
port: 3000,
};
// class smartsocket
tap.test('should create a new smartsocket', async () => {
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver1', port: testConfig.port });
await testSmartsocket.start();
});
// class SocketFunction
tap.test('should register a new Function', async () => {
testSocketFunctionForServer = new smartsocket.SocketFunction({
funcDef: async (dataArg, socketConnectionArg) => {
return dataArg;
},
funcName: 'testFunction1',
});
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
testSocketFunctionClient = new smartsocket.SocketFunction({
funcDef: async (dataArg, socketConnectionArg) => {
return dataArg;
},
funcName: 'testFunction2',
});
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
});
// class SmartsocketClient
tap.test('should react to a new websocket connection from client', async () => {
testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: 'http://localhost',
alias: 'testClient1',
autoReconnect: true,
});
testSmartsocketClient.addSocketFunction(testSocketFunctionClient);
await testSmartsocketClient.connect();
});
tap.test('should be able to tag a connection from client', async (tools) => {
await testSmartsocketClient.addTag({
id: 'awesome',
payload: 'yes',
});
const tagOnServerSide = await testSmartsocket.socketConnections
.findSync((socketConnection) => {
return true;
})
.getTagById('awesome');
expect(tagOnServerSide.payload).to.equal('yes');
});
tap.test('should be able to tag a connection from server', async (tools) => {
await testSmartsocket.socketConnections
.findSync((socketConnection) => {
return true;
})
.addTag({
id: 'awesome2',
payload: 'absolutely',
});
const tagOnClientSide = await testSmartsocketClient.socketConnection.getTagById('awesome2');
expect(tagOnClientSide.payload).to.equal('absolutely');
});
tap.test('should be able to make a functionCall from client to server', async () => {
const response = await testSmartsocketClient.serverCall<IReqResClient>('testFunction1', {
value1: 'hello',
});
console.log(response);
expect(response.value1).to.equal('hello');
});
tap.test('should be able to make a functionCall from server to client', async () => {
const response = await testSmartsocket.clientCall<IReqResServer>(
'testFunction2',
{
hi: 'hi there from server',
},
testSmartsocket.socketConnections.findSync((socketConnection) => {
return true;
})
);
console.log(response);
expect(response.hi).to.equal('hi there from server');
});
tap.test('client should disconnect and reconnect', async (toolsArg) => {
await testSmartsocketClient.disconnect();
await testSmartsocketClient.connect();
await toolsArg.delayFor(2000);
expect(testSmartsocket.socketConnections.getArray().length).to.equal(1);
});
// class smartsocket
tap.test('should be able to switch to a new server', async (toolsArg) => {
await testSmartsocket.stop();
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver2', port: testConfig.port });
await testSmartsocket.start();
await toolsArg.delayFor(30000);
});
tap.test('should be able to locate a connection tag after reconnect', async (tools) => {
expect(testSmartsocket.socketConnections.getArray().length).to.equal(1);
const tagOnServerSide = await testSmartsocket.socketConnections
.findSync((socketConnection) => {
return true;
})
.getTagById('awesome');
expect(tagOnServerSide.payload).to.equal('yes');
});
// terminate
tap.test('should close the server', async () => {
await testSmartsocketClient.stop();
await testSmartsocket.stop();
});
tap.start(); tap.start();

View File

@ -2,11 +2,9 @@
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import smartsocket = require('../ts/index'); import smartsocket = require('../ts/index');
import * as isohash from '@pushrocks/isohash';
let testSmartsocket: smartsocket.Smartsocket; let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient; let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketConnection: smartsocket.SocketConnection;
let testSocketFunctionForServer: smartsocket.SocketFunction<any>; let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
let testSocketFunctionClient: smartsocket.SocketFunction<any>; let testSocketFunctionClient: smartsocket.SocketFunction<any>;
@ -57,7 +55,6 @@ tap.test('should register a new Function', async () => {
funcName: 'testFunction2', funcName: 'testFunction2',
}); });
testSmartsocket.addSocketFunction(testSocketFunctionForServer); testSmartsocket.addSocketFunction(testSocketFunctionForServer);
console.log(testSmartsocket.socketFunctions);
}); });
tap.test('should start listening when .started is called', async () => { tap.test('should start listening when .started is called', async () => {
@ -72,7 +69,6 @@ tap.test('should react to a new websocket connection from client', async () => {
alias: 'testClient1', alias: 'testClient1',
}); });
testSmartsocketClient.addSocketFunction(testSocketFunctionClient); testSmartsocketClient.addSocketFunction(testSocketFunctionClient);
console.log(testSmartsocketClient.socketFunctions);
await testSmartsocketClient.connect(); await testSmartsocketClient.connect();
}); });

View File

@ -7,4 +7,5 @@ export type TConnectionStatus =
| 'connecting' | 'connecting'
| 'connected' | 'connected'
| 'disconnecting' | 'disconnecting'
| 'disconnected'; | 'disconnected'
| 'timedOut';

View File

@ -47,7 +47,7 @@ export class Smartsocket {
*/ */
public async start() { public async start() {
const socketIoModule = this.smartenv.getSafeNodeModule('socket.io'); const socketIoModule = this.smartenv.getSafeNodeModule('socket.io');
this.io = socketIoModule(this.socketServer.getServerForSocketIo()); this.io = socketIoModule(await this.socketServer.getServerForSocketIo());
await this.socketServer.start(); await this.socketServer.start();
this.io.on('connection', (socketArg) => { this.io.on('connection', (socketArg) => {
this._handleSocketConnection(socketArg); this._handleSocketConnection(socketArg);

View File

@ -91,7 +91,13 @@ export class SmartsocketClient {
public async connect() { public async connect() {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
const smartenvInstance = new plugins.smartenv.Smartenv(); const smartenvInstance = new plugins.smartenv.Smartenv();
const socketIoClient = plugins.socketIoClient; const socketIoClient: any = await smartenvInstance.getEnvAwareModule({
nodeModuleName: 'socket.io-client',
webUrlArg: 'https://cdn.jsdelivr.net/npm/socket.io-client@4/dist/socket.io.js',
getFunction: () => {
return (globalThis as any).io;
},
});
logger.log('info', 'trying to connect...'); logger.log('info', 'trying to connect...');
const socketUrl = `${this.serverUrl}:${this.serverPort}`; const socketUrl = `${this.serverUrl}:${this.serverPort}`;
this.socketConnection = new SocketConnection({ this.socketConnection = new SocketConnection({
@ -99,24 +105,26 @@ export class SmartsocketClient {
authenticated: false, authenticated: false,
side: 'client', side: 'client',
smartsocketHost: this, smartsocketHost: this,
socket: await socketIoClient.connect(socketUrl, { socket: await socketIoClient
.connect(socketUrl, {
multiplex: false, multiplex: false,
reconnectionAttempts: 5, autoConnect: false,
reconnectionAttempts: 0,
rejectUnauthorized: socketUrl.startsWith('https://localhost') ? false : true, rejectUnauthorized: socketUrl.startsWith('https://localhost') ? false : true,
}), })
.open(),
}); });
const timer = new plugins.smarttime.Timer(5000); const timer = new plugins.smarttime.Timer(5000);
timer.start(); timer.start();
timer.completed.then(() => { timer.completed.then(() => {
this.updateStatus('timedOut');
logger.log('warn', 'connection to server timed out.'); logger.log('warn', 'connection to server timed out.');
this.disconnect(); this.disconnect(true);
}); });
// authentication flow // authentication flow
this.socketConnection.socket.on( this.socketConnection.socket.on('requestAuth', (dataArg: interfaces.IRequestAuthPayload) => {
'requestAuth',
(dataArg: interfaces.IRequestAuthPayload) => {
timer.reset(); timer.reset();
logger.log('info', `server ${dataArg.serverAlias} requested authentication`); logger.log('info', `server ${dataArg.serverAlias} requested authentication`);
@ -144,6 +152,7 @@ export class SmartsocketClient {
for (const tag of Object.keys(oldTagStore)) { for (const tag of Object.keys(oldTagStore)) {
await this.addTag(oldTagStore[tag]); await this.addTag(oldTagStore[tag]);
} }
this.updateStatus('connected');
done.resolve(); done.resolve();
}); });
@ -157,44 +166,56 @@ export class SmartsocketClient {
this.socketConnection.socket.emit('dataAuth', { this.socketConnection.socket.emit('dataAuth', {
alias: this.alias, alias: this.alias,
}); });
} });
);
// handle connection // handle connection
this.socketConnection.socket.on('connect', async () => { this.socketConnection.socket.on('connect', async () => {});
this.updateStatus('connected');
});
// handle disconnection and errors // handle disconnection and errors
this.socketConnection.socket.on('disconnect', async () => { this.socketConnection.socket.on('disconnect', async () => {
await this.disconnect(); await this.disconnect(true);
}); });
this.socketConnection.socket.on('reconnect_failed', async () => { this.socketConnection.socket.on('reconnect_failed', async () => {
await this.disconnect(); await this.disconnect(true);
}); });
this.socketConnection.socket.on('connect_error', async () => { this.socketConnection.socket.on('connect_error', async () => {
await this.disconnect(); await this.disconnect(true);
}); });
return done.promise; return done.promise;
} }
private disconnectRunning = false;
/** /**
* disconnect from the server * disconnect from the server
*/ */
public async disconnect() { public async disconnect(useAutoReconnectSetting = false) {
if (this.disconnectRunning) {
return;
}
this.disconnectRunning = true;
this.updateStatus('disconnecting');
this.tagStoreSubscription?.unsubscribe(); this.tagStoreSubscription?.unsubscribe();
if (this.socketConnection) { if (this.socketConnection) {
await this.socketConnection.disconnect(); await this.socketConnection.disconnect();
this.socketConnection = undefined; this.socketConnection = undefined;
logger.log('ok', 'disconnected!'); logger.log('ok', 'disconnected socket!');
} else {
this.disconnectRunning = false;
logger.log('warn', 'tried to disconnect, without a SocketConnection');
return;
} }
logger.log('warn', `disconnected from server ${this.remoteShortId}`); logger.log('warn', `disconnected from server ${this.remoteShortId}`);
this.remoteShortId = null; this.remoteShortId = null;
this.updateStatus('disconnected');
if (this.autoReconnect) { if (this.autoReconnect && useAutoReconnectSetting && this.eventStatus !== 'connecting') {
this.tryDebouncedReconnect(); this.updateStatus('connecting');
await this.tryDebouncedReconnect();
this.disconnectRunning = false;
} else {
this.disconnectRunning = false;
} }
} }
@ -210,7 +231,8 @@ export class SmartsocketClient {
* try a reconnection * try a reconnection
*/ */
public async tryDebouncedReconnect() { public async tryDebouncedReconnect() {
await plugins.smartdelay.delayForRandom(10000, 50000); console.log('debounced reconnect!');
await plugins.smartdelay.delayForRandom(10000, 20000);
await this.connect(); await this.connect();
} }

View File

@ -26,7 +26,7 @@ export interface ISocketConnectionConstructorOptions {
authenticated: boolean; authenticated: boolean;
side: TSocketConnectionSide; side: TSocketConnectionSide;
smartsocketHost: Smartsocket | SmartsocketClient; smartsocketHost: Smartsocket | SmartsocketClient;
socket: pluginsTyped.socketIo.Socket | plugins.socketIoClient.Socket; socket: pluginsTyped.socketIo.Socket | pluginsTyped.socketIoClient.Socket;
} }
/** /**
@ -47,7 +47,7 @@ export class SocketConnection {
public side: TSocketConnectionSide; public side: TSocketConnectionSide;
public authenticated: boolean = false; public authenticated: boolean = false;
public smartsocketRef: Smartsocket | SmartsocketClient; public smartsocketRef: Smartsocket | SmartsocketClient;
public socket: pluginsTyped.socketIo.Socket | plugins.socketIoClient.Socket; public socket: pluginsTyped.socketIo.Socket | pluginsTyped.socketIoClient.Socket;
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>(); public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
public eventStatus: interfaces.TConnectionStatus = 'new'; public eventStatus: interfaces.TConnectionStatus = 'new';
@ -197,7 +197,7 @@ export class SocketConnection {
this.remoteTagStoreObservable.next(tagStoreArg); this.remoteTagStoreObservable.next(tagStoreArg);
}); });
logger.log('info', `now listening to function requests for ${this.alias}`); logger.log('info', `now listening to function requests for ${this.alias} on side ${this.side}`);
done.resolve(this); done.resolve(this);
} else { } else {
const errMessage = 'socket needs to be authenticated first'; const errMessage = 'socket needs to be authenticated first';

View File

@ -11,6 +11,7 @@ import { logger } from './smartsocket.logging';
*/ */
export class SocketServer { export class SocketServer {
private smartsocket: Smartsocket; private smartsocket: Smartsocket;
private httpServerDeferred: plugins.smartpromise.Deferred<any>;
private httpServer: pluginsTyped.http.Server | pluginsTyped.https.Server; private httpServer: pluginsTyped.http.Server | pluginsTyped.https.Server;
/** /**
@ -30,14 +31,19 @@ export class SocketServer {
serverType: 'smartexpress', serverType: 'smartexpress',
serverArg: pluginsTyped.smartexpress.Server serverArg: pluginsTyped.smartexpress.Server
) { ) {
this.httpServerDeferred = plugins.smartpromise.defer();
await serverArg.startedPromise; await serverArg.startedPromise;
this.httpServer = serverArg.httpServer; this.httpServer = serverArg.httpServer;
this.httpServerDeferred.resolve();
} }
/** /**
* gets the server for socket.io * gets the server for socket.io
*/ */
public getServerForSocketIo() { public async getServerForSocketIo() {
if (this.httpServerDeferred) {
await this.httpServerDeferred.promise;
}
if (this.httpServer) { if (this.httpServer) {
return this.httpServer; return this.httpServer;
} else { } else {

View File

@ -27,9 +27,3 @@ export {
smarttime, smarttime,
smartrx, smartrx,
}; };
// thirdparty
import * as socketIoClient from 'socket.io-client';
export {
socketIoClient
}

View File

@ -11,6 +11,10 @@ export { smartexpress };
// third party scope // third party scope
import type socketIo from 'socket.io'; import type socketIo from 'socket.io';
import type socketIoClient from 'socket.io-client'; import type { Socket as ClientSocket, connect as ClientIo } from 'socket.io-client';
export { socketIoClient, socketIo }; export { socketIo };
export namespace socketIoClient {
export type Socket = ClientSocket;
export type connect = typeof ClientIo;
}