88 lines
5.0 KiB
JavaScript
88 lines
5.0 KiB
JavaScript
|
"use strict";
|
||
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||
|
if (k2 === undefined) k2 = k;
|
||
|
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
||
|
}) : (function(o, m, k, k2) {
|
||
|
if (k2 === undefined) k2 = k;
|
||
|
o[k2] = m[k];
|
||
|
}));
|
||
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||
|
}) : function(o, v) {
|
||
|
o["default"] = v;
|
||
|
});
|
||
|
var __importStar = (this && this.__importStar) || function (mod) {
|
||
|
if (mod && mod.__esModule) return mod;
|
||
|
var result = {};
|
||
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||
|
__setModuleDefault(result, mod);
|
||
|
return result;
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.SocketServer = void 0;
|
||
|
const plugins = __importStar(require("./smartsocket.plugins"));
|
||
|
// used in case no other server is supplied
|
||
|
const http = __importStar(require("http"));
|
||
|
const smartsocket_logging_1 = require("./smartsocket.logging");
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
async setExternalServer(serverType, serverArg) {
|
||
|
await serverArg.startedPromise;
|
||
|
this.httpServer = serverArg.httpServer;
|
||
|
}
|
||
|
/**
|
||
|
* gets the server for socket.io
|
||
|
*/
|
||
|
getServerForSocketIo() {
|
||
|
if (this.httpServer) {
|
||
|
return this.httpServer;
|
||
|
}
|
||
|
else {
|
||
|
this.httpServer = new http.Server();
|
||
|
this.standaloneServer = true;
|
||
|
return this.httpServer;
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* starts listening to incoming sockets:
|
||
|
*/
|
||
|
async start() {
|
||
|
const done = plugins.smartpromise.defer();
|
||
|
// handle http servers
|
||
|
// in case an external server has been set "this.standaloneServer" should be false
|
||
|
if (this.httpServer && this.standaloneServer) {
|
||
|
if (!this.smartsocket.options.port) {
|
||
|
smartsocket_logging_1.logger.log('error', 'there should be a port specifed for smartsocket!');
|
||
|
throw new Error('there should be a port specified for smartsocket');
|
||
|
}
|
||
|
this.httpServer.listen(this.smartsocket.options.port, () => {
|
||
|
smartsocket_logging_1.logger.log('success', `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
|
||
|
*/
|
||
|
async stop() { }
|
||
|
}
|
||
|
exports.SocketServer = SocketServer;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRzZXJ2ZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHNvY2tldC5jbGFzc2VzLnNvY2tldHNlcnZlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUEsK0RBQWlEO0FBRWpELDJDQUEyQztBQUMzQywyQ0FBNkI7QUFHN0IsK0RBQStDO0FBRS9DOzs7R0FHRztBQUNILE1BQWEsWUFBWTtJQU92QixZQUFZLG1CQUFnQztRQUo1QyxrQ0FBa0M7UUFDMUIscUJBQWdCLEdBQUcs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