2018-03-19 09:00:11 +00:00
|
|
|
import * as plugins from './smartsocket.plugins';
|
2020-09-29 17:21:08 +00:00
|
|
|
import * as pluginsTyped from './smartsocket.pluginstyped';
|
2018-03-19 09:00:11 +00:00
|
|
|
|
|
|
|
// used in case no other server is supplied
|
|
|
|
import { Smartsocket } from './smartsocket.classes.smartsocket';
|
2020-09-24 18:03:01 +00:00
|
|
|
import { logger } from './smartsocket.logging';
|
2018-03-19 09:00:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* class socketServer
|
|
|
|
* handles the attachment of socketIo to whatever server is in play
|
|
|
|
*/
|
|
|
|
export class SocketServer {
|
|
|
|
private smartsocket: Smartsocket;
|
2022-01-20 15:50:25 +00:00
|
|
|
private httpServerDeferred: plugins.smartpromise.Deferred<any>;
|
2020-09-29 17:21:08 +00:00
|
|
|
private httpServer: pluginsTyped.http.Server | pluginsTyped.https.Server;
|
2022-01-19 07:05:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* wether httpServer is standalone
|
|
|
|
*/
|
2018-03-19 09:00:11 +00:00
|
|
|
private standaloneServer = false;
|
|
|
|
|
|
|
|
constructor(smartSocketInstance: Smartsocket) {
|
|
|
|
this.smartsocket = smartSocketInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* starts the server with another server
|
|
|
|
* also works with an express style server
|
|
|
|
*/
|
2019-04-26 15:35:15 +00:00
|
|
|
public async setExternalServer(
|
|
|
|
serverType: 'smartexpress',
|
2020-09-29 17:21:08 +00:00
|
|
|
serverArg: pluginsTyped.smartexpress.Server
|
2019-04-26 15:35:15 +00:00
|
|
|
) {
|
2022-01-20 15:50:25 +00:00
|
|
|
this.httpServerDeferred = plugins.smartpromise.defer();
|
2019-04-24 15:10:51 +00:00
|
|
|
await serverArg.startedPromise;
|
|
|
|
this.httpServer = serverArg.httpServer;
|
2022-01-20 15:50:25 +00:00
|
|
|
this.httpServerDeferred.resolve();
|
2018-03-19 09:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the server for socket.io
|
|
|
|
*/
|
2022-01-20 15:50:25 +00:00
|
|
|
public async getServerForSocketIo() {
|
|
|
|
if (this.httpServerDeferred) {
|
|
|
|
await this.httpServerDeferred.promise;
|
|
|
|
}
|
2018-03-19 09:00:11 +00:00
|
|
|
if (this.httpServer) {
|
|
|
|
return this.httpServer;
|
2019-04-24 15:10:51 +00:00
|
|
|
} else {
|
2020-09-29 17:21:08 +00:00
|
|
|
const httpModule = this.smartsocket.smartenv.getSafeNodeModule('http');
|
|
|
|
this.httpServer = new httpModule.Server();
|
2018-03-19 09:00:11 +00:00
|
|
|
this.standaloneServer = true;
|
|
|
|
return this.httpServer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* starts listening to incoming sockets:
|
|
|
|
*/
|
|
|
|
public async start() {
|
2019-01-30 02:14:02 +00:00
|
|
|
const done = plugins.smartpromise.defer();
|
2018-03-19 09:00:11 +00:00
|
|
|
|
|
|
|
// handle http servers
|
2019-04-24 13:47:28 +00:00
|
|
|
// in case an external server has been set "this.standaloneServer" should be false
|
2018-03-19 09:00:11 +00:00
|
|
|
if (this.httpServer && this.standaloneServer) {
|
2019-04-24 13:47:28 +00:00
|
|
|
if (!this.smartsocket.options.port) {
|
2020-09-24 18:03:01 +00:00
|
|
|
logger.log('error', 'there should be a port specifed for smartsocket!');
|
2019-04-26 15:35:15 +00:00
|
|
|
throw new Error('there should be a port specified for smartsocket');
|
2019-04-24 13:47:28 +00:00
|
|
|
}
|
2018-03-19 09:00:11 +00:00
|
|
|
this.httpServer.listen(this.smartsocket.options.port, () => {
|
2020-09-24 18:04:11 +00:00
|
|
|
logger.log(
|
|
|
|
'success',
|
|
|
|
`Server started in standalone mode on ${this.smartsocket.options.port}`
|
|
|
|
);
|
2018-03-19 09:00:11 +00:00
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing else to do if express server is set
|
|
|
|
await done.promise;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* closes the server
|
|
|
|
*/
|
2022-01-19 06:01:58 +00:00
|
|
|
public async stop() {
|
|
|
|
if (this.httpServer) {
|
|
|
|
this.httpServer.close();
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 09:00:11 +00:00
|
|
|
}
|