smartsocket/dist/smartsocket.classes.socketserver.js

86 lines
5.2 KiB
JavaScript

"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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2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