Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
09dec2071e | |||
a1443deafe | |||
9eac5ad336 | |||
cf607a79d5 | |||
8426c976bf | |||
1086065000 |
111
README.md
111
README.md
@ -1,111 +0,0 @@
|
|||||||
# @pushrocks/smartsocket
|
|
||||||
easy and secure websocket communication
|
|
||||||
|
|
||||||
## Availabililty and Links
|
|
||||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartsocket)
|
|
||||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartsocket)
|
|
||||||
* [github.com (source mirror)](https://github.com/pushrocks/smartsocket)
|
|
||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartsocket/)
|
|
||||||
|
|
||||||
## Status for master
|
|
||||||
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartsocket)
|
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartsocket)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://prettier.io/)
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
|
||||||
|
|
||||||
Under the hood we use socket.io and shortid for managed data exchange.
|
|
||||||
|
|
||||||
### Serverside
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import * as smartsocket from 'smartsocket';
|
|
||||||
import * as q from q; // q is a promise library
|
|
||||||
|
|
||||||
// The "Smartsocket" listens on a port and can receive new "SocketConnection" requests.
|
|
||||||
let mySmartsocket = new smartsocket.Smartsocket({
|
|
||||||
port: 3000 // the port smartsocket will listen on
|
|
||||||
});
|
|
||||||
|
|
||||||
// optional:
|
|
||||||
// run this with anothoer existing server like express
|
|
||||||
declare var someExpressServer; // read the express docs about how express actually works
|
|
||||||
mySmartsocket.setServer(someExpressServer);
|
|
||||||
|
|
||||||
// A "SocketRole" can be referenced by "SocketFunction"s.
|
|
||||||
// All "SocketRequest"s carry authentication data for a specific "SocketRole".
|
|
||||||
// "SocketFunction"s know which "SocketRole"s are allowed to execute them
|
|
||||||
let mySocketRole = new smartsocket.SocketRole({
|
|
||||||
name: 'someRoleName',
|
|
||||||
passwordHash: 'someHashedString'
|
|
||||||
});
|
|
||||||
|
|
||||||
// A "SocketFunction" executes a referenced function and passes in any data of the corresponding "SocketRequest".
|
|
||||||
// The referenced function must return a promise and resolve with data of type any.
|
|
||||||
// Any "SocketRequest" carries a unique identifier. If the referenced function's promise resolved any passed on argument will be returned to the requesting party
|
|
||||||
let testSocketFunction1 = new smartsocket.SocketFunction({
|
|
||||||
funcName: 'testSocketFunction1',
|
|
||||||
funcDef: data => {
|
|
||||||
console.log('testSocketFunction1 executed successfully!');
|
|
||||||
},
|
|
||||||
allowedRoles: [mySocketRole] // all roles that have access to a specific function
|
|
||||||
});
|
|
||||||
|
|
||||||
// A "Smartsocket" exposes a .clientCall() that gets
|
|
||||||
// 1. the name of the "SocketFunction" on the client side
|
|
||||||
// 2. the data to pass in
|
|
||||||
// 3. And a target "SocketConnection" (there can be multiple connections at once)
|
|
||||||
// any unique id association is done internally
|
|
||||||
mySmartsocket.clientCall('restart', data, someTargetConnection).then(responseData => {});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Client side
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import * as smartsocket from 'smartsocket';
|
|
||||||
|
|
||||||
// A "SmartsocketClient" is different from a "Smartsocket" in that it doesn't expose any public address.
|
|
||||||
// Thus any new "SocketConnection"s must be innitiated from a "SmartsocketClient".
|
|
||||||
let testSmartsocketClient = new smartsocket.SmartsocketClient({
|
|
||||||
port: testConfig.port,
|
|
||||||
url: 'http://localhost',
|
|
||||||
password: 'testPassword',
|
|
||||||
alias: 'testClient1',
|
|
||||||
role: 'testRole1'
|
|
||||||
});
|
|
||||||
|
|
||||||
// You can .connect() and .disconnect() from a "Smartsocket"
|
|
||||||
testSmartsocketClient.connect().then(() => {
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
// The client can also specify "SocketFunction"s. It can also specify "SocketRole"s in case a client connects to multiple servers at once
|
|
||||||
let testSocketFunction2 = new smartsocket.SocketFunction({
|
|
||||||
funcName: 'testSocketFunction2',
|
|
||||||
funcDef: data => {}, // the function to execute, has to return promise
|
|
||||||
allowedRoles: []
|
|
||||||
});
|
|
||||||
|
|
||||||
// A "SmartsocketClient" can call functions on the serverside using .serverCall() analog to the "Smartsocket"'s .clientCall method.
|
|
||||||
mySmartsocketClient.serverCall('function', functionCallData).then(functionResponseData => {
|
|
||||||
// the functionResponseData comes from the server... awesome, right?
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
> **NOTE:**
|
|
||||||
> you can easily chain dependent requests on either the server or client side with promises.
|
|
||||||
> `data` is always a js object that you can design for your specific needs.
|
|
||||||
> It supports buffers for large binary data network exchange.
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
|
13
package-lock.json
generated
13
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartsocket",
|
"name": "@pushrocks/smartsocket",
|
||||||
"version": "1.1.62",
|
"version": "1.1.65",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -1560,11 +1560,10 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@pushrocks/smartenv": {
|
"@pushrocks/smartenv": {
|
||||||
"version": "4.0.11",
|
"version": "4.0.15",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartenv/-/smartenv-4.0.11.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartenv/-/smartenv-4.0.15.tgz",
|
||||||
"integrity": "sha512-dmphN7A3sUBdoBYbWdIYRvNEhvsZSZn/57tSMlpgqcyfGixYC8PGXKvqbsBKLnEwfpBFVQD95hHe26srANqELA==",
|
"integrity": "sha512-7bz2jzxAnojlIu3jYCZwnG5LEIjAQbLEQFRLpWDzoS80U62kEhC8bhOJ7+pdMHH3xqDYszabiv2PEr5/zQZ/Nw==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/smartparam": "^1.1.6",
|
|
||||||
"@pushrocks/smartpromise": "^3.0.6",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
"@types/node": "^14.11.2"
|
"@types/node": "^14.11.2"
|
||||||
}
|
}
|
||||||
@ -1889,6 +1888,7 @@
|
|||||||
"version": "1.1.6",
|
"version": "1.1.6",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartparam/-/smartparam-1.1.6.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartparam/-/smartparam-1.1.6.tgz",
|
||||||
"integrity": "sha512-1El/F2QTWYDGy4Nh6vz9Ry1JVg1FEeyexB7Uvi4zHElpXYVxwso6xImRTLj+SW50JAg7nwEZ+ljkzTG9XvnwWA==",
|
"integrity": "sha512-1El/F2QTWYDGy4Nh6vz9Ry1JVg1FEeyexB7Uvi4zHElpXYVxwso6xImRTLj+SW50JAg7nwEZ+ljkzTG9XvnwWA==",
|
||||||
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/smartpromise": "^3.0.6",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
"is-promise": "^2.1.0",
|
"is-promise": "^2.1.0",
|
||||||
@ -6855,7 +6855,8 @@
|
|||||||
"is-promise": {
|
"is-promise": {
|
||||||
"version": "2.2.2",
|
"version": "2.2.2",
|
||||||
"resolved": "https://verdaccio.lossless.one/is-promise/-/is-promise-2.2.2.tgz",
|
"resolved": "https://verdaccio.lossless.one/is-promise/-/is-promise-2.2.2.tgz",
|
||||||
"integrity": "sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ=="
|
"integrity": "sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==",
|
||||||
|
"dev": true
|
||||||
},
|
},
|
||||||
"is-reference": {
|
"is-reference": {
|
||||||
"version": "1.2.1",
|
"version": "1.2.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartsocket",
|
"name": "@pushrocks/smartsocket",
|
||||||
"version": "1.1.62",
|
"version": "1.1.65",
|
||||||
"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",
|
||||||
@ -24,7 +24,7 @@
|
|||||||
"@pushrocks/isounique": "^1.0.4",
|
"@pushrocks/isounique": "^1.0.4",
|
||||||
"@pushrocks/lik": "^4.0.17",
|
"@pushrocks/lik": "^4.0.17",
|
||||||
"@pushrocks/smartdelay": "^2.0.10",
|
"@pushrocks/smartdelay": "^2.0.10",
|
||||||
"@pushrocks/smartenv": "^4.0.11",
|
"@pushrocks/smartenv": "^4.0.15",
|
||||||
"@pushrocks/smartexpress": "^3.0.76",
|
"@pushrocks/smartexpress": "^3.0.76",
|
||||||
"@pushrocks/smartlog": "^2.0.39",
|
"@pushrocks/smartlog": "^2.0.39",
|
||||||
"@pushrocks/smartpromise": "^3.0.6",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
|
@ -64,8 +64,16 @@ export class SmartsocketClient {
|
|||||||
/**
|
/**
|
||||||
* connect the client to the server
|
* connect the client to the server
|
||||||
*/
|
*/
|
||||||
public connect() {
|
public async connect() {
|
||||||
const done = plugins.smartpromise.defer();
|
const done = plugins.smartpromise.defer();
|
||||||
|
const smartenvInstance = new plugins.smartenv.Smartenv();
|
||||||
|
const socketIoClient = await smartenvInstance.getEnvAwareModule({
|
||||||
|
nodeModuleName: 'socket.io-client',
|
||||||
|
webUrlArg: 'https://cdn.jsdelivr.net/npm/socket.io-client@2/dist/socket.io.js',
|
||||||
|
getFunction: () => {
|
||||||
|
return globalThis.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({
|
||||||
@ -74,7 +82,7 @@ export class SmartsocketClient {
|
|||||||
role: this.socketRole,
|
role: this.socketRole,
|
||||||
side: 'client',
|
side: 'client',
|
||||||
smartsocketHost: this,
|
smartsocketHost: this,
|
||||||
socket: plugins.socketIoClient(socketUrl, {
|
socket: await socketIoClient.connect(socketUrl, {
|
||||||
multiplex: false,
|
multiplex: false,
|
||||||
reconnectionAttempts: 5,
|
reconnectionAttempts: 5,
|
||||||
}),
|
}),
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
import * as plugins from './smartsocket.plugins';
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
|
||||||
export class SocketStats {}
|
export class SocketStats {}
|
||||||
|
@ -25,10 +25,3 @@ export {
|
|||||||
smarttime,
|
smarttime,
|
||||||
smartrx,
|
smartrx,
|
||||||
};
|
};
|
||||||
|
|
||||||
// third party
|
|
||||||
import socketIoClient from 'socket.io-client';
|
|
||||||
|
|
||||||
export {
|
|
||||||
socketIoClient
|
|
||||||
};
|
|
||||||
|
@ -2,19 +2,15 @@
|
|||||||
import type http from 'http';
|
import type http from 'http';
|
||||||
import type https from 'https';
|
import type https from 'https';
|
||||||
|
|
||||||
export {
|
export { http, https };
|
||||||
http,
|
|
||||||
https
|
|
||||||
};
|
|
||||||
|
|
||||||
// pushrocks scope
|
// pushrocks scope
|
||||||
import type * as smartexpress from '@pushrocks/smartexpress';
|
import type * as smartexpress from '@pushrocks/smartexpress';
|
||||||
|
|
||||||
export {
|
export { smartexpress };
|
||||||
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';
|
||||||
|
|
||||||
export { socketIo };
|
export { socketIo, socketIoClient };
|
||||||
|
Reference in New Issue
Block a user