fix(core): update
This commit is contained in:
parent
e188b18016
commit
0d94ed9345
33
test/test.ts
33
test/test.ts
@ -9,8 +9,9 @@ import smartsocket = require('../ts/index');
|
||||
|
||||
let testSmartsocket: smartsocket.Smartsocket;
|
||||
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
||||
let testSocketConnection: smartsocket.SocketConnection;
|
||||
let testSocketRole1: smartsocket.SocketRole;
|
||||
let testSocketFunctionServer: smartsocket.SocketFunction;
|
||||
let testSocketFunctionForServer: smartsocket.SocketFunction;
|
||||
let testSocketFunctionClient: smartsocket.SocketFunction;
|
||||
|
||||
const testConfig = {
|
||||
@ -34,14 +35,14 @@ tap.test('should add a socketrole', async () => {
|
||||
|
||||
// class SocketFunction
|
||||
tap.test('should register a new Function', async () => {
|
||||
testSocketFunctionServer = new smartsocket.SocketFunction({
|
||||
testSocketFunctionForServer = new smartsocket.SocketFunction({
|
||||
allowedRoles: [testSocketRole1],
|
||||
funcDef: async (dataArg, socketConnectionArg) => {
|
||||
return dataArg;
|
||||
},
|
||||
funcName: 'testFunction1'
|
||||
});
|
||||
testSmartsocket.addSocketFunction(testSocketFunctionServer);
|
||||
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
|
||||
|
||||
testSocketFunctionClient = new smartsocket.SocketFunction({
|
||||
allowedRoles: [],
|
||||
@ -50,7 +51,7 @@ tap.test('should register a new Function', async () => {
|
||||
},
|
||||
funcName: 'testFunction1'
|
||||
});
|
||||
testSmartsocket.addSocketFunction(testSocketFunctionServer);
|
||||
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
|
||||
console.log(testSmartsocket.socketFunctions);
|
||||
});
|
||||
|
||||
@ -72,12 +73,6 @@ tap.test('should react to a new websocket connection from client', async () => {
|
||||
await testSmartsocketClient.connect();
|
||||
});
|
||||
|
||||
tap.test('client should disconnect and reconnect', async tools => {
|
||||
await testSmartsocketClient.disconnect();
|
||||
await tools.delayFor(100);
|
||||
await testSmartsocketClient.connect();
|
||||
});
|
||||
|
||||
tap.test('2 clients should connect in parallel', async () => {
|
||||
// TODO: implement parallel test
|
||||
});
|
||||
@ -90,12 +85,24 @@ tap.test('should be able to make a functionCall from client to server', async ()
|
||||
});
|
||||
|
||||
tap.test('should be able to make a functionCall from server to client', async () => {
|
||||
const response = await testSmartsocketClient.serverCall('testFunction1', {
|
||||
hi: 'hi there from client'
|
||||
});
|
||||
const response = await testSmartsocket.clientCall(
|
||||
'testFunction1',
|
||||
{
|
||||
hi: 'hi there from server'
|
||||
},
|
||||
testSmartsocket.socketConnections.find(socketConnection => {
|
||||
return true;
|
||||
})
|
||||
);
|
||||
console.log(response);
|
||||
});
|
||||
|
||||
tap.test('client should disconnect and reconnect', async tools => {
|
||||
await testSmartsocketClient.disconnect();
|
||||
await tools.delayFor(100);
|
||||
await testSmartsocketClient.connect();
|
||||
});
|
||||
|
||||
// terminate
|
||||
tap.test('should close the server', async () => {
|
||||
await testSmartsocket.stop();
|
||||
|
@ -1,5 +1,3 @@
|
||||
import * as plugins from './smartsocket.plugins';
|
||||
|
||||
// export main classes
|
||||
export * from './smartsocket.classes.smartsocket';
|
||||
export * from './smartsocket.classes.smartsocketclient';
|
||||
|
@ -18,7 +18,7 @@ export interface ISmartsocketConstructorOptions {
|
||||
export class Smartsocket {
|
||||
public options: ISmartsocketConstructorOptions;
|
||||
public io: SocketIO.Server;
|
||||
public openSockets = new Objectmap<SocketConnection>();
|
||||
public socketConnections = new Objectmap<SocketConnection>();
|
||||
public socketRoles = new Objectmap<SocketRole>();
|
||||
public socketFunctions = new Objectmap<SocketFunction>();
|
||||
public socketRequests = new Objectmap<SocketRequest>();
|
||||
@ -50,14 +50,14 @@ export class Smartsocket {
|
||||
*/
|
||||
public async stop() {
|
||||
await plugins.smartdelay.delayFor(1000);
|
||||
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
|
||||
this.socketConnections.forEach((socketObjectArg: SocketConnection) => {
|
||||
plugins.smartlog.defaultLogger.log(
|
||||
'info',
|
||||
`disconnect socket with >>alias ${socketObjectArg.alias}`
|
||||
);
|
||||
socketObjectArg.socket.disconnect();
|
||||
});
|
||||
this.openSockets.wipe();
|
||||
this.socketConnections.wipe();
|
||||
this.io.close();
|
||||
|
||||
// stop the corresponging server
|
||||
@ -74,7 +74,6 @@ export class Smartsocket {
|
||||
dataArg: any,
|
||||
targetSocketConnectionArg: SocketConnection
|
||||
) {
|
||||
const done = plugins.smartpromise.defer();
|
||||
const socketRequest = new SocketRequest(this, {
|
||||
funcCallData: {
|
||||
funcDataArg: dataArg,
|
||||
@ -84,10 +83,8 @@ export class Smartsocket {
|
||||
shortId: plugins.shortid.generate(),
|
||||
side: 'requesting'
|
||||
});
|
||||
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => {
|
||||
done.resolve(dataArg2.funcDataArg);
|
||||
});
|
||||
const result = await done.promise;
|
||||
const response: ISocketFunctionCall = await socketRequest.dispatch();
|
||||
const result = response.funcDataArg;
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -108,7 +105,7 @@ export class Smartsocket {
|
||||
/**
|
||||
* the standard handler for new socket connections
|
||||
*/
|
||||
private _handleSocketConnection(socketArg) {
|
||||
private async _handleSocketConnection(socketArg: plugins.socketIo.Socket) {
|
||||
const socketConnection: SocketConnection = new SocketConnection({
|
||||
alias: undefined,
|
||||
authenticated: false,
|
||||
@ -118,14 +115,8 @@ export class Smartsocket {
|
||||
socket: socketArg
|
||||
});
|
||||
plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...');
|
||||
this.openSockets.add(socketConnection);
|
||||
socketConnection
|
||||
.authenticate()
|
||||
.then(() => {
|
||||
return socketConnection.listenToFunctionRequests();
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
this.socketConnections.add(socketConnection);
|
||||
await socketConnection.authenticate();
|
||||
await socketConnection.listenToFunctionRequests();
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,10 @@ export interface ISmartsocketClientOptions {
|
||||
|
||||
export class SmartsocketClient {
|
||||
public alias: string;
|
||||
public role: string;
|
||||
public socketRole: SocketRole;
|
||||
public socketConnection: SocketConnection;
|
||||
public serverUrl: string;
|
||||
public serverPort: number;
|
||||
public serverPassword: string;
|
||||
|
||||
public socketFunctions = new plugins.lik.Objectmap<SocketFunction>();
|
||||
public socketRequests = new plugins.lik.Objectmap<SocketRequest>();
|
||||
@ -30,14 +29,17 @@ export class SmartsocketClient {
|
||||
|
||||
constructor(optionsArg: ISmartsocketClientOptions) {
|
||||
this.alias = optionsArg.alias;
|
||||
this.role = optionsArg.role;
|
||||
this.serverUrl = optionsArg.url;
|
||||
this.serverPort = optionsArg.port;
|
||||
this.serverPassword = optionsArg.password;
|
||||
this.socketRole = new SocketRole({
|
||||
name: optionsArg.role,
|
||||
passwordHash: optionsArg.password
|
||||
});
|
||||
}
|
||||
|
||||
public addSocketFunction(socketFunction: SocketFunction) {
|
||||
this.socketFunctions.add(socketFunction);
|
||||
this.socketRole.allowedFunctions.add(socketFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -50,7 +52,7 @@ export class SmartsocketClient {
|
||||
this.socketConnection = new SocketConnection({
|
||||
alias: this.alias,
|
||||
authenticated: false,
|
||||
role: undefined,
|
||||
role: this.socketRole,
|
||||
side: 'client',
|
||||
smartsocketHost: this,
|
||||
socket: plugins.socketIoClient(socketUrl, { multiplex: false })
|
||||
@ -58,8 +60,8 @@ export class SmartsocketClient {
|
||||
this.socketConnection.socket.on('requestAuth', () => {
|
||||
console.log('server requested authentication');
|
||||
this.socketConnection.socket.emit('dataAuth', {
|
||||
role: this.role,
|
||||
password: this.serverPassword,
|
||||
role: this.socketRole.name,
|
||||
password: this.socketRole.passwordHash,
|
||||
alias: this.alias
|
||||
});
|
||||
this.socketConnection.socket.on('authenticated', () => {
|
||||
@ -72,16 +74,21 @@ export class SmartsocketClient {
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
const done = plugins.smartpromise.defer();
|
||||
this.socketConnection.socket.disconnect();
|
||||
/**
|
||||
* disconnect from the server
|
||||
*/
|
||||
public async disconnect() {
|
||||
this.socketConnection.socket.disconnect(true);
|
||||
this.socketConnection = undefined;
|
||||
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
public serverCall(functionNameArg: string, dataArg: any) {
|
||||
/**
|
||||
* dispatches a server call
|
||||
* @param functionNameArg
|
||||
* @param dataArg
|
||||
*/
|
||||
public async serverCall(functionNameArg: string, dataArg: any): Promise<any> {
|
||||
const done = plugins.smartpromise.defer();
|
||||
const socketRequest = new SocketRequest(this, {
|
||||
side: 'requesting',
|
||||
@ -92,9 +99,8 @@ export class SmartsocketClient {
|
||||
funcDataArg: dataArg
|
||||
}
|
||||
});
|
||||
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => {
|
||||
done.resolve(dataArg2.funcDataArg);
|
||||
});
|
||||
return done.promise;
|
||||
const response = await socketRequest.dispatch();
|
||||
const result = response.funcDataArg;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ export class SocketConnection {
|
||||
return socketFunctionArg.name === dataArg.funcCallData.funcName;
|
||||
}
|
||||
);
|
||||
if (referencedFunction !== undefined) {
|
||||
if (referencedFunction) {
|
||||
plugins.smartlog.defaultLogger.log('ok', 'function in access scope');
|
||||
const localSocketRequest = new SocketRequest(this.smartsocketRef, {
|
||||
side: 'responding',
|
||||
|
@ -17,7 +17,7 @@ export type TSocketRequestSide = 'requesting' | 'responding';
|
||||
/**
|
||||
* interface of constructor of class SocketRequest
|
||||
*/
|
||||
export interface SocketRequestConstructorOptions {
|
||||
export interface ISocketRequestConstructorOptions {
|
||||
side: TSocketRequestSide;
|
||||
originSocketConnection: SocketConnection;
|
||||
shortId: string;
|
||||
@ -51,13 +51,13 @@ export class SocketRequest {
|
||||
public shortid: string;
|
||||
public originSocketConnection: SocketConnection;
|
||||
public funcCallData: ISocketFunctionCall;
|
||||
public done = plugins.smartpromise.defer();
|
||||
public done = plugins.smartpromise.defer<ISocketFunctionCall>();
|
||||
|
||||
public smartsocketRef: Smartsocket | SmartsocketClient;
|
||||
|
||||
constructor(
|
||||
smartsocketRefArg: Smartsocket | SmartsocketClient,
|
||||
optionsArg: SocketRequestConstructorOptions
|
||||
optionsArg: ISocketRequestConstructorOptions
|
||||
) {
|
||||
this.smartsocketRef = smartsocketRefArg;
|
||||
this.side = optionsArg.side;
|
||||
@ -72,7 +72,7 @@ export class SocketRequest {
|
||||
/**
|
||||
* dispatches a socketrequest from the requesting to the receiving side
|
||||
*/
|
||||
public dispatch() {
|
||||
public dispatch(): Promise<ISocketFunctionCall> {
|
||||
const requestData: ISocketRequestDataObject = {
|
||||
funcCallData: this.funcCallData,
|
||||
shortId: this.shortid
|
||||
|
@ -10,7 +10,7 @@ import { ISocketConnectionAuthenticationObject } from './smartsocket.classes.soc
|
||||
/**
|
||||
* interface for class SocketRole
|
||||
*/
|
||||
export interface SocketRoleOptions {
|
||||
export interface ISocketRoleOptions {
|
||||
name: string;
|
||||
passwordHash: string;
|
||||
}
|
||||
@ -43,10 +43,15 @@ export class SocketRole {
|
||||
public name: string;
|
||||
public passwordHash: string;
|
||||
public allowedFunctions = new Objectmap<SocketFunction>();
|
||||
constructor(optionsArg: SocketRoleOptions) {
|
||||
constructor(optionsArg: ISocketRoleOptions) {
|
||||
this.name = optionsArg.name;
|
||||
this.passwordHash = optionsArg.passwordHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* adds the socketfunction to the socketrole
|
||||
* @param socketFunctionArg
|
||||
*/
|
||||
public addSocketFunction(socketFunctionArg: SocketFunction) {
|
||||
this.allowedFunctions.add(socketFunctionArg);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user