Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
0aa073f2a7 | |||
7170e58457 | |||
cccee35f2c | |||
118ec84ec5 | |||
dd31eea263 |
98
README.md
98
README.md
@ -2,100 +2,28 @@
|
|||||||
easy and secure websocket communication, TypeScript ready
|
easy and secure websocket communication, TypeScript ready
|
||||||
|
|
||||||
## Availabililty
|
## Availabililty
|
||||||
[](https://www.npmjs.com/package/smartsocket)
|
[](https://www.npmjs.com/package/smartsocket)
|
||||||
[](https://gitlab.com/pushrocks/smartsocket)
|
[](https://GitLab.com/pushrocks/smartsocket)
|
||||||
[](https://github.com/pushrocks/smartsocket)
|
[](https://github.com/pushrocks/smartsocket)
|
||||||
[](https://pushrocks.gitlab.io/smartsocket/docs)
|
[](https://pushrocks.gitlab.io/smartsocket/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
[](https://GitLab.com/pushrocks/smartsocket/commits/master)
|
||||||
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
[](https://GitLab.com/pushrocks/smartsocket/commits/master)
|
||||||
|
[](https://www.npmjs.com/package/smartsocket)
|
||||||
[](https://david-dm.org/pushrocks/smartsocket)
|
[](https://david-dm.org/pushrocks/smartsocket)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartsocket/master/dependencies/npm)
|
[](https://www.bithound.io/github/pushrocks/smartsocket/master/dependencies/npm)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartsocket)
|
[](https://www.bithound.io/github/pushrocks/smartsocket)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
|
[](http://standardjs.com/)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
We recommend the use of TypeScript.
|
Use TypeScript for best in class instellisense.
|
||||||
Under the hood we use socket.io and shortid for managed data exchange.
|
|
||||||
|
|
||||||
### Serverside
|
For further information read the linked docs at the top of this README.
|
||||||
```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.
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
let mySmartsocket = new smartsocket.Smartsocket({
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||||
port: 3000 // the port smartsocket will listen on
|
|
||||||
});
|
|
||||||
|
|
||||||
// A "SocketRole" can be referenced by "SocketFunction"s.
|
[](https://push.rocks)
|
||||||
// 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.
|
|
||||||
|
9
dist/smartsocket.classes.smartsocket.d.ts
vendored
9
dist/smartsocket.classes.smartsocket.d.ts
vendored
@ -12,10 +12,13 @@ export declare class Smartsocket {
|
|||||||
socketRoles: Objectmap<SocketRole>;
|
socketRoles: Objectmap<SocketRole>;
|
||||||
constructor(optionsArg: ISmartsocketConstructorOptions);
|
constructor(optionsArg: ISmartsocketConstructorOptions);
|
||||||
/**
|
/**
|
||||||
* starts listening to incling sockets:
|
* starts listening to incoming sockets:
|
||||||
*/
|
*/
|
||||||
startServer: () => void;
|
startServer(): Promise<void>;
|
||||||
closeServer: () => void;
|
/**
|
||||||
|
* closes the server
|
||||||
|
*/
|
||||||
|
closeServer(): Promise<void>;
|
||||||
/**
|
/**
|
||||||
* allows call to specific client.
|
* allows call to specific client.
|
||||||
*/
|
*/
|
||||||
|
33
dist/smartsocket.classes.smartsocket.js
vendored
33
dist/smartsocket.classes.smartsocket.js
vendored
@ -1,4 +1,12 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||||
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
|
});
|
||||||
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const plugins = require("./smartsocket.plugins");
|
const plugins = require("./smartsocket.plugins");
|
||||||
// classes
|
// classes
|
||||||
@ -9,24 +17,31 @@ class Smartsocket {
|
|||||||
constructor(optionsArg) {
|
constructor(optionsArg) {
|
||||||
this.openSockets = new lik_1.Objectmap();
|
this.openSockets = new lik_1.Objectmap();
|
||||||
this.socketRoles = new lik_1.Objectmap();
|
this.socketRoles = new lik_1.Objectmap();
|
||||||
/**
|
this.options = optionsArg;
|
||||||
* starts listening to incling sockets:
|
}
|
||||||
*/
|
/**
|
||||||
this.startServer = () => {
|
* starts listening to incoming sockets:
|
||||||
|
*/
|
||||||
|
startServer() {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
this.io = plugins.socketIo(this.options.port);
|
this.io = plugins.socketIo(this.options.port);
|
||||||
this.io.on('connection', (socketArg) => {
|
this.io.on('connection', (socketArg) => {
|
||||||
this._handleSocketConnection(socketArg);
|
this._handleSocketConnection(socketArg);
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
this.closeServer = () => {
|
}
|
||||||
|
/**
|
||||||
|
* closes the server
|
||||||
|
*/
|
||||||
|
closeServer() {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
this.openSockets.forEach((socketObjectArg) => {
|
this.openSockets.forEach((socketObjectArg) => {
|
||||||
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
|
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
|
||||||
socketObjectArg.socket.disconnect();
|
socketObjectArg.socket.disconnect();
|
||||||
});
|
});
|
||||||
this.openSockets.wipe();
|
this.openSockets.wipe();
|
||||||
this.io.close();
|
this.io.close();
|
||||||
};
|
});
|
||||||
this.options = optionsArg;
|
|
||||||
}
|
}
|
||||||
// communication
|
// communication
|
||||||
/**
|
/**
|
||||||
@ -82,4 +97,4 @@ class Smartsocket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.Smartsocket = Smartsocket;
|
exports.Smartsocket = Smartsocket;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpREFBZ0Q7QUFHaEQsVUFBVTtBQUNWLDZCQUErQjtBQUUvQixpR0FBeUU7QUFDekU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QyxnQkFBZ0IsQ0FBQyx3QkFBd0IsRUFBRSxDQUFBO1FBQ3BELENBQUMsQ0FBQzthQUNELEtBQUssQ0FBQyxDQUFDLEdBQUc7WUFDVCxPQUFPLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxDQUFBO1FBQ2xCLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQztDQUVGO0FBdkZELGtDQXVGQyJ9
|
@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"structure": {
|
|
||||||
"readme": "index.md"
|
|
||||||
},
|
|
||||||
"plugins": [
|
|
||||||
"tonic",
|
|
||||||
"edit-link"
|
|
||||||
],
|
|
||||||
"pluginsConfig": {
|
|
||||||
"edit-link": {
|
|
||||||
"base": "https://gitlab.com/pushrocks/npmts/edit/master/docs/",
|
|
||||||
"label": "Edit on GitLab"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,47 +1,63 @@
|
|||||||
# smartsocket
|
# smartsocket
|
||||||
easy and secure websocket communication, Typescript ready
|
easy and secure websocket communication, TypeScript ready
|
||||||
|
|
||||||
## Availabililty
|
## Availabililty
|
||||||
[](https://www.npmjs.com/package/smartsocket)
|
[](https://www.npmjs.com/package/smartsocket)
|
||||||
[](https://gitlab.com/pushrocks/smartsocket)
|
[](https://GitLab.com/pushrocks/smartsocket)
|
||||||
[](https://github.com/pushrocks/smartsocket)
|
[](https://github.com/pushrocks/smartsocket)
|
||||||
[](https://pushrocks.gitlab.io/smartsocket/docs)
|
[](https://pushrocks.gitlab.io/smartsocket/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
[](https://GitLab.com/pushrocks/smartsocket/commits/master)
|
||||||
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
[](https://GitLab.com/pushrocks/smartsocket/commits/master)
|
||||||
|
[](https://www.npmjs.com/package/smartsocket)
|
||||||
[](https://david-dm.org/pushrocks/smartsocket)
|
[](https://david-dm.org/pushrocks/smartsocket)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartsocket/master/dependencies/npm)
|
[](https://www.bithound.io/github/pushrocks/smartsocket/master/dependencies/npm)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartsocket)
|
[](https://www.bithound.io/github/pushrocks/smartsocket)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
|
[](http://standardjs.com/)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
We recommend the use of typescript.
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
Under the hood we use socket.io and shortid for managed data exchange.
|
Under the hood we use socket.io and shortid for managed data exchange.
|
||||||
|
|
||||||
### Serverside
|
### Serverside
|
||||||
```typescript
|
```typescript
|
||||||
import * as smartsocket from "smartsocket";
|
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({
|
let mySmartsocket = new smartsocket.Smartsocket({
|
||||||
port: 3000 // the port smartsocket will listen on
|
port: 3000 // the port smartsocket will listen on
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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({
|
let mySocketRole = new smartsocket.SocketRole({
|
||||||
name: "someRoleName",
|
name: "someRoleName",
|
||||||
passwordHash: "someHashedString"
|
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({
|
let testSocketFunction1 = new smartsocket.SocketFunction({
|
||||||
funcName:"testSocketFunction1",
|
funcName:"testSocketFunction1",
|
||||||
funcDef:(data) => {
|
funcDef:(data) => {
|
||||||
|
console.log('testSocketFunction1 executed successfully!')
|
||||||
}, // the function to execute
|
},
|
||||||
allowedRoles:[mySocketRole] // all roles that have access to a specific function
|
allowedRoles:[mySocketRole] // all roles that have access to a specific function
|
||||||
});
|
});
|
||||||
|
|
||||||
mySmartsocket.clientCall("","restart",data,someTargetConnection)
|
// 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) => {
|
.then((responseData) => {
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -51,6 +67,8 @@ mySmartsocket.clientCall("","restart",data,someTargetConnection)
|
|||||||
```typescript
|
```typescript
|
||||||
import * as smartsocket from "smartsocket";
|
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({
|
let testSmartsocketClient = new smartsocket.SmartsocketClient({
|
||||||
port: testConfig.port,
|
port: testConfig.port,
|
||||||
url: "http://localhost",
|
url: "http://localhost",
|
||||||
@ -58,24 +76,22 @@ let testSmartsocketClient = new smartsocket.SmartsocketClient({
|
|||||||
alias: "testClient1",
|
alias: "testClient1",
|
||||||
role: "testRole1"
|
role: "testRole1"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// You can .connect() and .disconnect() from a "Smartsocket"
|
||||||
testSmartsocketClient.connect()
|
testSmartsocketClient.connect()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
done();
|
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({
|
let testSocketFunction2 = new smartsocket.SocketFunction({
|
||||||
funcName: "testSocketFunction2",
|
funcName: "testSocketFunction2",
|
||||||
funcDef: (data) => {}, // the function to execute, has to return promise
|
funcDef: (data) => {}, // the function to execute, has to return promise
|
||||||
allowedRoles:[]
|
allowedRoles:[]
|
||||||
});
|
});
|
||||||
|
|
||||||
let functionCalldata = {
|
|
||||||
funcName: "",
|
|
||||||
funcData: {
|
|
||||||
someKey:"someValue"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// A "SmartsocketClient" can call functions on the serverside using .serverCall() analog to the "Smartsocket"'s .clientCall method.
|
||||||
mySmartsocketClient.serverCall("function",functionCallData)
|
mySmartsocketClient.serverCall("function",functionCallData)
|
||||||
.then((functionResponseData) => { // the functionResponseData comes from the server... awesome, right?
|
.then((functionResponseData) => { // the functionResponseData comes from the server... awesome, right?
|
||||||
|
|
||||||
@ -85,4 +101,11 @@ mySmartsocketClient.serverCall("function",functionCallData)
|
|||||||
> **NOTE:**
|
> **NOTE:**
|
||||||
you can easily chain dependent requests on either the server or client side with promises.
|
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.
|
`data` is always a js object that you can design for your specific needs.
|
||||||
It supports buffers for large binary data network exchange.
|
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.html)
|
||||||
|
|
||||||
|
[](https://push.rocks)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "smartsocket",
|
"name": "smartsocket",
|
||||||
"version": "1.1.8",
|
"version": "1.1.10",
|
||||||
"description": "easy and secure websocket communication, TypeScript ready",
|
"description": "easy and secure websocket communication, TypeScript ready",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
|
@ -37,17 +37,15 @@ tap.test('should add a socketrole', async () => {
|
|||||||
tap.test('should register a new Function', async () => {
|
tap.test('should register a new Function', async () => {
|
||||||
testSocketFunction1 = new smartsocket.SocketFunction({
|
testSocketFunction1 = new smartsocket.SocketFunction({
|
||||||
funcName: 'testFunction1',
|
funcName: 'testFunction1',
|
||||||
funcDef: (dataArg) => {
|
funcDef: async (dataArg) => {
|
||||||
let done = smartq.defer()
|
return dataArg
|
||||||
done.resolve(dataArg)
|
|
||||||
return done.promise
|
|
||||||
},
|
},
|
||||||
allowedRoles: [ testSocketRole1 ]
|
allowedRoles: [ testSocketRole1 ]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// class SmartsocketClient
|
// class SmartsocketClient
|
||||||
tap.test('should react to a new websocket connection from client',async () => {
|
tap.test('should react to a new websocket connection from client', async () => {
|
||||||
let done = smartq.defer()
|
let done = smartq.defer()
|
||||||
testSmartsocketClient = new smartsocket.SmartsocketClient({
|
testSmartsocketClient = new smartsocket.SmartsocketClient({
|
||||||
port: testConfig.port,
|
port: testConfig.port,
|
||||||
|
@ -22,15 +22,19 @@ export class Smartsocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* starts listening to incling sockets:
|
* starts listening to incoming sockets:
|
||||||
*/
|
*/
|
||||||
startServer = () => {
|
async startServer () {
|
||||||
this.io = plugins.socketIo(this.options.port)
|
this.io = plugins.socketIo(this.options.port)
|
||||||
this.io.on('connection', (socketArg) => {
|
this.io.on('connection', (socketArg) => {
|
||||||
this._handleSocketConnection(socketArg)
|
this._handleSocketConnection(socketArg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
closeServer = () => {
|
|
||||||
|
/**
|
||||||
|
* closes the server
|
||||||
|
*/
|
||||||
|
async closeServer () {
|
||||||
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
|
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
|
||||||
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`)
|
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`)
|
||||||
socketObjectArg.socket.disconnect()
|
socketObjectArg.socket.disconnect()
|
||||||
|
Reference in New Issue
Block a user