nxkit
Version:
This is a collection of tools, independent of any other libraries
122 lines (121 loc) • 5.04 kB
JavaScript
;
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2015, xuewen.chu
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of xuewen.chu nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL xuewen.chu BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
const _server_1 = require("./_server");
const upgrade_1 = require("./ws/upgrade");
const service_1 = require("./service");
const static_service_1 = require("./static_service");
require("./http_service");
__export(require("./_server"));
/**
* @class Server Impl
*/
class ServerIMPL extends _server_1.Server {
//Handle http and websocket and http-heartbeat request
initializ(server) {
//http
server.on('request', async (req, res) => {
if (this.interceptRequest(req, res))
return;
var url = decodeURI(req.url || ''); // 解码
var info = this.router.find(url); // 通过url查找目标服务信息
var name = info.service;
var cls = service_1.default.get(name);
if (this.printLog) {
console.log(url);
}
if (cls) {
if (!util_1.default.equalsClass(static_service_1.StaticService, cls)) {
console.error(name + ' not the correct type, http request');
cls = static_service_1.StaticService;
}
}
else {
cls = static_service_1.StaticService;
}
try {
var ser = new cls(req, res);
var ok = ser.requestAuth(info); // 认证请求的合法性
if (ok instanceof Promise) {
req.pause();
if (!await ok)
return req.socket.destroy(); // 立即断开连接
req.resume();
}
else if (!ok) {
return req.socket.destroy();
}
}
catch (err) {
console.error(err);
return req.socket.destroy();
}
req.on('data', function () { });
ser.action(info);
});
// upgrade websocket, create web socket connection
server.on('upgrade', (req, socket, upgradeHead) => {
if (this.printLog) {
console.log(`Web socket upgrade ws://${req.headers.host}${req.url}`);
}
upgrade_1.default(req, upgradeHead);
});
server.on('error', (err) => {
console.log(err);
console.log('Server Error ---------------');
});
this.addEventListener('Startup', () => {
this.m_checkIntervalId = setInterval(() => {
var time = Date.now();
for (var conv of Object.values(this.m_ws_conversations)) {
if (conv.keepAliveTime * 2 + conv.lastPacketTime < time) {
conv.close(); // disconnect
}
}
}, 3e4 /*30s*/);
});
server.on('close', () => {
clearInterval(this.m_checkIntervalId);
this.m_isRun = false;
this.trigger('Stop', {});
});
}
}
exports.ServerIMPL = ServerIMPL;
exports.default = {
ServerIMPL: ServerIMPL,
setShared: _server_1.default.setShared,
get shared() { return _server_1.default.shared; },
};