now working as expected

This commit is contained in:
Philipp Kunz 2018-03-19 10:00:11 +01:00
parent abf546eec3
commit aef4f9c591
10 changed files with 292 additions and 111 deletions

View File

@ -1,7 +1,5 @@
/// <reference types="node" />
/// <reference types="socket.io" /> /// <reference types="socket.io" />
import * as plugins from './smartsocket.plugins'; import * as plugins from './smartsocket.plugins';
import * as http from 'http';
import { SocketConnection } from './smartsocket.classes.socketconnection'; import { SocketConnection } from './smartsocket.classes.socketconnection';
import { SocketRole } from './smartsocket.classes.socketrole'; import { SocketRole } from './smartsocket.classes.socketrole';
import * as SocketIO from 'socket.io'; import * as SocketIO from 'socket.io';
@ -10,24 +8,20 @@ export interface ISmartsocketConstructorOptions {
} }
export declare class Smartsocket { export declare class Smartsocket {
options: ISmartsocketConstructorOptions; options: ISmartsocketConstructorOptions;
httpServer: http.Server;
io: SocketIO.Server; io: SocketIO.Server;
openSockets: plugins.lik.Objectmap<SocketConnection>; openSockets: plugins.lik.Objectmap<SocketConnection>;
socketRoles: plugins.lik.Objectmap<SocketRole>; socketRoles: plugins.lik.Objectmap<SocketRole>;
private socketServer;
constructor(optionsArg: ISmartsocketConstructorOptions); constructor(optionsArg: ISmartsocketConstructorOptions);
setExternalServer: (serverType: "express" | "http", serverArg: any) => Promise<void>;
/** /**
* starts listening to incoming sockets: * starts smartsocket
*/ */
startServer(): Promise<{}>; start(): Promise<void>;
/** /**
* starts the server with another server * stops smartsocket
* also works with an express style server
*/ */
setServer(httpServerArg: http.Server): Promise<void>; stop(): Promise<void>;
/**
* closes the server
*/
closeServer(): Promise<void>;
/** /**
* allows call to specific client. * allows call to specific client.
*/ */

View File

@ -9,49 +9,36 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartsocket.plugins"); const plugins = require("./smartsocket.plugins");
const http = require("http");
// classes // classes
const lik_1 = require("lik"); const lik_1 = require("lik");
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection"); const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest"); const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
const smartsocket_classes_socketserver_1 = require("./smartsocket.classes.socketserver");
class Smartsocket { 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.socketServer = new smartsocket_classes_socketserver_1.SocketServer(this);
// tslint:disable-next-line:member-ordering
this.setExternalServer = this.socketServer.setExternalServer;
this.options = optionsArg; this.options = optionsArg;
} }
/** /**
* starts listening to incoming sockets: * starts smartsocket
*/ */
startServer() { start() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let done = plugins.smartq.defer(); this.io = plugins.socketIo(this.socketServer.getServerForSocketIo());
if (!this.httpServer) { yield this.socketServer.start();
this.httpServer = new http.Server();
}
this.io = plugins.socketIo(this.httpServer);
this.io.on('connection', socketArg => { this.io.on('connection', socketArg => {
this._handleSocketConnection(socketArg); this._handleSocketConnection(socketArg);
}); });
this.httpServer.listen(this.options.port, () => {
done.resolve();
});
return yield done.promise;
}); });
} }
/** /**
* starts the server with another server * stops smartsocket
* also works with an express style server
*/ */
setServer(httpServerArg) { stop() {
return __awaiter(this, void 0, void 0, function* () {
this.httpServer = httpServerArg;
});
}
/**
* closes the server
*/
closeServer() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield plugins.smartdelay.delayFor(1000); yield plugins.smartdelay.delayFor(1000);
this.openSockets.forEach((socketObjectArg) => { this.openSockets.forEach((socketObjectArg) => {
@ -60,6 +47,8 @@ class Smartsocket {
}); });
this.openSockets.wipe(); this.openSockets.wipe();
this.io.close(); this.io.close();
// stop the corresponging server
this.socketServer.stop();
}); });
} }
// communication // communication
@ -67,26 +56,29 @@ class Smartsocket {
* allows call to specific client. * allows call to specific client.
*/ */
clientCall(functionNameArg, dataArg, targetSocketConnectionArg) { clientCall(functionNameArg, dataArg, targetSocketConnectionArg) {
let done = plugins.smartq.defer(); return __awaiter(this, void 0, void 0, function* () {
let socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({ const done = plugins.smartq.defer();
side: 'requesting', const socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
funcCallData: {
funcDataArg: dataArg,
funcName: functionNameArg
},
originSocketConnection: targetSocketConnectionArg, originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(), shortId: plugins.shortid.generate(),
funcCallData: { side: 'requesting'
funcName: functionNameArg,
funcDataArg: dataArg
}
}); });
socketRequest.dispatch().then((dataArg) => { socketRequest.dispatch().then((dataArg) => {
done.resolve(dataArg.funcDataArg); done.resolve(dataArg.funcDataArg);
}); });
return done.promise; const result = yield done.promise;
return result;
});
} }
/** /**
* adds socketRoles * adds socketRoles
*/ */
addSocketRoles(socketRolesArray) { addSocketRoles(socketRolesArray) {
for (let socketRole of socketRolesArray) { for (const socketRole of socketRolesArray) {
this.socketRoles.add(socketRole); this.socketRoles.add(socketRole);
} }
return; return;
@ -95,7 +87,7 @@ class Smartsocket {
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
_handleSocketConnection(socketArg) { _handleSocketConnection(socketArg) {
let socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({ const socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
alias: undefined, alias: undefined,
authenticated: false, authenticated: false,
role: undefined, role: undefined,
@ -116,4 +108,4 @@ class Smartsocket {
} }
} }
exports.Smartsocket = Smartsocket; exports.Smartsocket = Smartsocket;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3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ZixJQUFJLEVBQUUsUUFBUTtZQUNkLGVBQWUsRUFBRSxJQUFJO1lBQ3JCLE1BQU0sRUFBRSxTQUFTO1NBQ2xCLENBQUMsQ0FBQztRQUNILE9BQU8sQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLDZDQUE2QyxDQUFDLENBQUM7UUFDckUsSUFBSSxDQUFDLFdBQVcsQ0FBQyxHQUFHLENBQUMsZ0JBQWdCLENBQUMsQ0FBQztRQUN2QyxnQkFBZ0I7YUFDYixZQUFZLEVBQUU7YUFDZCxJQUFJLENBQUMsR0FBRyxFQUFFO1lBQ1QsTUFBTSxDQUFDLGdCQUFnQixDQUFDLHdCQUF3QixFQUFFLENBQUM7UUFDckQsQ0FBQyxDQUFDO2FBQ0QsS0FBSyxDQUFDLEdBQUcsQ0FBQyxFQUFFO1lBQ1gsT0FBTyxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsQ0FBQztRQUNuQixDQUFDLENBQUMsQ0FBQztJQUNQLENBQUM7Q0FDRjtBQXRHRCxrQ0FzR0MifQ==

View File

@ -1,7 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartsocket.plugins"); const plugins = require("./smartsocket.plugins");
// import classes
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection"); const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest"); const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
class SmartsocketClient { class SmartsocketClient {
@ -69,4 +68,4 @@ class SmartsocketClient {
} }
} }
exports.SmartsocketClient = SmartsocketClient; exports.SmartsocketClient = SmartsocketClient;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJ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M7UUFDMUMsSUFBSSxDQUFDLGdCQUFnQixHQUFHLFNBQVMsQ0FBQztRQUNsQyxPQUFPLENBQUMsU0FBUyxDQUFDLEVBQUUsQ0FBQyxlQUFlLENBQUMsQ0FBQztRQUN0QyxJQUFJLENBQUMsT0FBTyxFQUFFLENBQUM7UUFDZixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztJQUN0QixDQUFDO0lBRUQsVUFBVSxDQUFDLGVBQXVCLEVBQUUsT0FBWTtRQUM5QyxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFDLEtBQUssRUFBRSxDQUFDO1FBQ2xDLElBQUksYUFBYSxHQUFHLElBQUksaURBQWEsQ0FBQztZQUNwQyxJQUFJLEVBQUUsWUFBWTtZQUNsQixzQkFBc0IsRUFBRSxJQUFJLENBQUMsZ0JBQWdCO1lBQzdDLE9BQU8sRUFBRSxPQUFPLENBQUMsT0FBTyxDQUFDLFFBQVEsRUFBRTtZQUNuQyxZQUFZLEVBQUU7Z0JBQ1osUUFBUSxFQUFFLGVBQWU7Z0JBQ3pCLFdBQVcsRUFBRSxPQUFPO2FBQ3JCO1NBQ0YsQ0FBQyxDQUFDO1FBQ0gsYUFBYSxDQUFDLFFBQVEsRUFBRSxDQUFDLElBQUksQ0FBQyxDQUFDLE9BQTRCLEVBQUUsRUFBRTtZQUM3RCxJQUFJLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxXQUFXLENBQUMsQ0FBQztRQUNwQyxDQUFDLENBQUMsQ0FBQztRQUNILE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0lBQ3RCLENBQUM7Q0FDRjtBQXhFRCw4Q0F3RUMifQ==

View File

@ -0,0 +1,29 @@
import { Smartsocket } from './smartsocket.classes.smartsocket';
/**
* class socketServer
* handles the attachment of socketIo to whatever server is in play
*/
export declare class SocketServer {
private smartsocket;
private httpServer;
private standaloneServer;
private expressServer;
constructor(smartSocketInstance: Smartsocket);
/**
* starts the server with another server
* also works with an express style server
*/
setExternalServer(serverType: 'express' | 'http', serverArg: any): Promise<void>;
/**
* gets the server for socket.io
*/
getServerForSocketIo(): any;
/**
* starts listening to incoming sockets:
*/
start(): Promise<void>;
/**
* closes the server
*/
stop(): Promise<void>;
}

View File

@ -0,0 +1,86 @@
"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 });
const plugins = require("./smartsocket.plugins");
// used in case no other server is supplied
const http = require("http");
/**
* class socketServer
* handles the attachment of socketIo to whatever server is in play
*/
class SocketServer {
constructor(smartSocketInstance) {
// wether httpServer is standalone
this.standaloneServer = false;
this.smartsocket = smartSocketInstance;
}
/**
* starts the server with another server
* also works with an express style server
*/
setExternalServer(serverType, serverArg) {
return __awaiter(this, void 0, void 0, function* () {
if (serverType === 'http') {
this.httpServer = serverArg;
}
else if (serverType === 'express') {
this.expressServer = serverArg;
}
});
}
/**
* gets the server for socket.io
*/
getServerForSocketIo() {
if (this.httpServer) {
return this.httpServer;
}
else if (this.expressServer) {
return this.expressServer;
}
else if (!this.httpServer && !this.expressServer) {
this.httpServer = new http.Server();
this.standaloneServer = true;
return this.httpServer;
}
else {
throw new Error('no server specified!');
}
}
/**
* starts listening to incoming sockets:
*/
start() {
return __awaiter(this, void 0, void 0, function* () {
const done = plugins.smartq.defer();
// handle http servers
if (this.httpServer && this.standaloneServer) {
this.httpServer.listen(this.smartsocket.options.port, () => {
console.log(`Server started in standalone mode on ${this.smartsocket.options.port}`);
done.resolve();
});
}
else {
done.resolve();
}
// nothing else to do if express server is set
yield done.promise;
return;
});
}
/**
* closes the server
*/
stop() {
return __awaiter(this, void 0, void 0, function* () { });
}
}
exports.SocketServer = SocketServer;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRzZXJ2ZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHNvY2tldC5jbGFzc2VzLnNvY2tldHNlcnZlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsaURBQWlEO0FBRWpELDJDQUEyQztBQUMzQyw2QkFBNkI7QUFHN0I7OztHQUdHO0FBQ0g7SUFPRSxZQUFZLG1CQUFnQztRQUo1QyxrQ0FBa0M7UUFDMUIscUJBQWdCLEdBQUcsS0FBSyxDQUFDO1FBSS9CLElBQUksQ0FBQyxXQUFXLEdBQUcsbUJBQW1CLENBQUM7SUFDekMsQ0FBQztJQUVEOzs7T0FHRztJQUNVLGlCQUFpQixDQUFDLFVBQThCLEVBQUUsU0FBYzs7WUFDM0UsRUFBRSxDQUFDLENBQUMsVUFBVSxLQUFLLE1BQU0sQ0FBQyxDQUFDLENBQUM7Z0JBQzFCLElBQUksQ0FBQyxVQUFVLEdBQUcsU0FBUyxDQUFDO1lBQzlCLENBQUM7WUFBQyxJQUFJLENBQUMsRUFBRSxDQUFDLENBQUMsVUFBVSxLQUFLLFNBQVMsQ0FBQyxDQUFDLENBQUM7Z0JBQ3BDLElBQUksQ0FBQyxhQUFhLEdBQUcsU0FBUyxDQUFDO1lBQ2pDLENBQUM7UUFDSCxDQUFDO0tBQUE7SUFFRDs7T0FFRztJQUNJLG9CQUFvQjtRQUN6QixFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsVUFBVSxDQUFDLENBQUMsQ0FBQztZQUNwQixNQUFNLENBQUMsSUFBSSxDQUFDLFVBQVUsQ0FBQztRQUN6QixDQUFDO1FBQUMsSUFBS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UM7Z0JBQ2pCLENBQUMsQ0FBQyxDQUFDO1lBQ0wsQ0FBQztZQUFDLElBQUksQ0FBQyxDQUFDO2dCQUNOLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztZQUNqQixDQUFDO1lBRUQsOENBQThDO1lBQzlDLE1BQU0sSUFBSSxDQUFDLE9BQU8sQ0FBQztZQUNuQixNQUFNLENBQUM7UUFDVCxDQUFDO0tBQUE7SUFFRDs7T0FFRztJQUNVLElBQUk7OERBQUksQ0FBQztLQUFBO0NBQ3ZCO0FBakVELG9DQWlFQyJ9

View File

@ -1,16 +1,18 @@
// tslint:disable-next-line:no-implicit-dependencies
import { expect, tap } from 'tapbundle'; import { expect, tap } from 'tapbundle';
import * as nodehash from 'nodehash';
import * as smartq from 'smartq';
import socketIoClient = require('socket.io-client'); import socketIoClient = require('socket.io-client');
import smartsocket = require('../ts/index'); import smartsocket = require('../ts/index');
import smartq = require('smartq');
import nodehash = require('nodehash');
let testSmartsocket: smartsocket.Smartsocket; let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient; let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketRole1: smartsocket.SocketRole; let testSocketRole1: smartsocket.SocketRole;
let testSocketFunction1: smartsocket.SocketFunction; let testSocketFunction1: smartsocket.SocketFunction;
let testConfig = { const testConfig = {
port: 3000 port: 3000
}; };
@ -21,7 +23,7 @@ tap.test('should create a new smartsocket', async () => {
}); });
tap.test('should start listening when .started is called', async () => { tap.test('should start listening when .started is called', async () => {
testSmartsocket.startServer(); await testSmartsocket.start();
}); });
// class socketrole // class socketrole
@ -36,17 +38,17 @@ tap.test('should add a socketrole', async () => {
// class SocketFunction // class SocketFunction
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', allowedRoles: [testSocketRole1],
funcDef: async dataArg => { funcDef: async dataArg => {
return dataArg; return dataArg;
}, },
allowedRoles: [testSocketRole1] funcName: 'testFunction1',
}); });
}); });
// 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(); const done = smartq.defer();
testSmartsocketClient = new smartsocket.SmartsocketClient({ testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port, port: testConfig.port,
url: 'http://localhost', url: 'http://localhost',
@ -116,7 +118,7 @@ tap.test('should be able to make a functionCall from server to client', async ()
// terminate // terminate
tap.test('should close the server', async () => { tap.test('should close the server', async () => {
await testSmartsocket.closeServer(); await testSmartsocket.stop();
}); });
tap.start(); tap.start();

View File

@ -1,14 +1,13 @@
import * as plugins from './smartsocket.plugins'; import * as plugins from './smartsocket.plugins';
import * as helpers from './smartsocket.helpers'; import * as helpers from './smartsocket.helpers';
import * as http from 'http';
// classes // classes
import { Objectmap } from 'lik'; import { Objectmap } from 'lik';
import { SocketFunction, ISocketFunctionCall } from './smartsocket.classes.socketfunction';
import { SocketConnection } from './smartsocket.classes.socketconnection'; import { SocketConnection } from './smartsocket.classes.socketconnection';
import { ISocketFunctionCall, SocketFunction } from './smartsocket.classes.socketfunction';
import { SocketRequest } from './smartsocket.classes.socketrequest'; import { SocketRequest } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole'; import { SocketRole } from './smartsocket.classes.socketrole';
import { SocketServer } from './smartsocket.classes.socketserver';
// socket.io // socket.io
import * as SocketIO from 'socket.io'; import * as SocketIO from 'socket.io';
@ -18,45 +17,35 @@ export interface ISmartsocketConstructorOptions {
} }
export class Smartsocket { export class Smartsocket {
options: ISmartsocketConstructorOptions; public options: ISmartsocketConstructorOptions;
httpServer: http.Server; public io: SocketIO.Server;
io: SocketIO.Server; public openSockets = new Objectmap<SocketConnection>();
openSockets = new Objectmap<SocketConnection>(); public socketRoles = new Objectmap<SocketRole>();
socketRoles = new Objectmap<SocketRole>();
private socketServer = new SocketServer(this);
constructor(optionsArg: ISmartsocketConstructorOptions) { constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg; this.options = optionsArg;
} }
// tslint:disable-next-line:member-ordering
public setExternalServer = this.socketServer.setExternalServer;
/** /**
* starts listening to incoming sockets: * starts smartsocket
*/ */
async startServer() { public async start() {
let done = plugins.smartq.defer(); this.io = plugins.socketIo(this.socketServer.getServerForSocketIo());
if (!this.httpServer) { await this.socketServer.start();
this.httpServer = new http.Server();
}
this.io = plugins.socketIo(this.httpServer);
this.io.on('connection', socketArg => { this.io.on('connection', socketArg => {
this._handleSocketConnection(socketArg); this._handleSocketConnection(socketArg);
}); });
this.httpServer.listen(this.options.port, () => {
done.resolve();
});
return await done.promise;
} }
/** /**
* starts the server with another server * stops smartsocket
* also works with an express style server
*/ */
async setServer(httpServerArg: http.Server) { public async stop() {
this.httpServer = httpServerArg;
}
/**
* closes the server
*/
async closeServer() {
await plugins.smartdelay.delayFor(1000); await plugins.smartdelay.delayFor(1000);
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}`);
@ -64,6 +53,9 @@ export class Smartsocket {
}); });
this.openSockets.wipe(); this.openSockets.wipe();
this.io.close(); this.io.close();
// stop the corresponging server
this.socketServer.stop();
} }
// communication // communication
@ -71,28 +63,33 @@ export class Smartsocket {
/** /**
* allows call to specific client. * allows call to specific client.
*/ */
clientCall(functionNameArg: string, dataArg: any, targetSocketConnectionArg: SocketConnection) { public async clientCall(
let done = plugins.smartq.defer(); functionNameArg: string,
let socketRequest = new SocketRequest({ dataArg: any,
side: 'requesting', targetSocketConnectionArg: SocketConnection
) {
const done = plugins.smartq.defer();
const socketRequest = new SocketRequest({
funcCallData: {
funcDataArg: dataArg,
funcName: functionNameArg
},
originSocketConnection: targetSocketConnectionArg, originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(), shortId: plugins.shortid.generate(),
funcCallData: { side: 'requesting'
funcName: functionNameArg,
funcDataArg: dataArg
}
}); });
socketRequest.dispatch().then((dataArg: ISocketFunctionCall) => { socketRequest.dispatch().then((dataArg: ISocketFunctionCall) => {
done.resolve(dataArg.funcDataArg); done.resolve(dataArg.funcDataArg);
}); });
return done.promise; const result = await done.promise;
return result;
} }
/** /**
* adds socketRoles * adds socketRoles
*/ */
addSocketRoles(socketRolesArray: SocketRole[]): void { public addSocketRoles(socketRolesArray: SocketRole[]): void {
for (let socketRole of socketRolesArray) { for (const socketRole of socketRolesArray) {
this.socketRoles.add(socketRole); this.socketRoles.add(socketRole);
} }
return; return;
@ -102,7 +99,7 @@ export class Smartsocket {
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
private _handleSocketConnection(socketArg) { private _handleSocketConnection(socketArg) {
let socketConnection: SocketConnection = new SocketConnection({ const socketConnection: SocketConnection = new SocketConnection({
alias: undefined, alias: undefined,
authenticated: false, authenticated: false,
role: undefined, role: undefined,

View File

@ -1,13 +1,9 @@
import * as plugins from './smartsocket.plugins'; import * as plugins from './smartsocket.plugins';
// import interfaces
import { ISocketFunctionCall } from './smartsocket.classes.socketfunction';
import { ISocketRequestDataObject } from './smartsocket.classes.socketrequest';
// import classes
import { SocketConnection } from './smartsocket.classes.socketconnection'; import { SocketConnection } from './smartsocket.classes.socketconnection';
import { SocketFunction } from './smartsocket.classes.socketfunction'; import { ISocketFunctionCall, SocketFunction } from './smartsocket.classes.socketfunction';
import { SocketRequest } from './smartsocket.classes.socketrequest'; import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
/** /**
* interface for class SmartsocketClient * interface for class SmartsocketClient
*/ */

View File

@ -0,0 +1,76 @@
import * as plugins from './smartsocket.plugins';
// used in case no other server is supplied
import * as http from 'http';
import { Smartsocket } from './smartsocket.classes.smartsocket';
/**
* class socketServer
* handles the attachment of socketIo to whatever server is in play
*/
export class SocketServer {
private smartsocket: Smartsocket;
private httpServer: http.Server;
// wether httpServer is standalone
private standaloneServer = false;
private expressServer: any;
constructor(smartSocketInstance: Smartsocket) {
this.smartsocket = smartSocketInstance;
}
/**
* starts the server with another server
* also works with an express style server
*/
public async setExternalServer(serverType: 'express' | 'http', serverArg: any) {
if (serverType === 'http') {
this.httpServer = serverArg;
} else if (serverType === 'express') {
this.expressServer = serverArg;
}
}
/**
* gets the server for socket.io
*/
public getServerForSocketIo() {
if (this.httpServer) {
return this.httpServer;
} else if (this.expressServer) {
return this.expressServer;
} else if (!this.httpServer && !this.expressServer) {
this.httpServer = new http.Server();
this.standaloneServer = true;
return this.httpServer;
} else {
throw new Error('no server specified!');
}
}
/**
* starts listening to incoming sockets:
*/
public async start() {
const done = plugins.smartq.defer();
// handle http servers
if (this.httpServer && this.standaloneServer) {
this.httpServer.listen(this.smartsocket.options.port, () => {
console.log(`Server started in standalone mode on ${this.smartsocket.options.port}`);
done.resolve();
});
} else {
done.resolve();
}
// nothing else to do if express server is set
await done.promise;
return;
}
/**
* closes the server
*/
public async stop() {}
}

View File

@ -1,3 +1,13 @@
{ {
"extends": "tslint-config-standard" "extends": [
} "tslint:latest",
"tslint-config-prettier"
],
"rules": {
"semicolon": [
true,
"always"
]
}
}