File manager - Edit - /home/gzbnyc5/pty-app/realstate/node_app/lib/server.js
Back
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _http = require('http'); var _http2 = _interopRequireDefault(_http); var _https = require('https'); var _https2 = _interopRequireDefault(_https); var _express = require('express'); var _express2 = _interopRequireDefault(_express); var _socket = require('socket.io'); var _socket2 = _interopRequireDefault(_socket); var _redis = require('redis'); var _redis2 = _interopRequireDefault(_redis); var _cors = require('cors'); var _cors2 = _interopRequireDefault(_cors); var _config = require('../config/config.json'); var _config2 = _interopRequireDefault(_config); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _net = require('net'); var _uniqid = require('uniqid'); var _uniqid2 = _interopRequireDefault(_uniqid); var _underscore = require('underscore'); var _underscore2 = _interopRequireDefault(_underscore); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var app = (0, _express2.default)(); app.use((0, _cors2.default)()); app.use(function (req, res, next) { // list of allowed domains var allowed = ["https://localhost:3000", "https://localhost:8000", "https://new.gzbnyc.com", "https://gzbnyc.com"], allowedIndex = allowed.indexOf(req.get('host')); // res.header("Access-Control-Allow-Origin", allowed[allowedIndex]); res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Credentials", "true"); next(); }); var server = _https2.default.createServer({ key: _fs2.default.readFileSync('/home/gzbnyc5/ssl/keys/' + getMostRecentFileName('/home/gzbnyc5/ssl/keys/')), cert: _fs2.default.readFileSync('/home/gzbnyc5/ssl/certs/' + getMostRecentFileName('/home/gzbnyc5/ssl/certs/')) }, app); // const server = http.createServer(); var socketIo = (0, _socket2.default)(server, { origins: '*:*' }); var redisSub = _redis2.default.createClient(); var connections = { clients: {} }; // Return only base file name without dir function getMostRecentFileName(dir) { var files = _fs2.default.readdirSync(dir); // use underscore for max() return _underscore2.default.max(files, function (f) { var fullpath = _path2.default.join(dir, f); // ctime = creation time is used // replace with mtime for modification time return _fs2.default.statSync(fullpath).ctime; }); } redisSub.on('message', function (channel, message) { console.log('Redis Pub/Sub 2: ', channel, message); var message = JSON.parse(message); if (channel == 'notifications') { if (typeof connections.clients[message.receiver_id] == 'undefined') return false; connections.clients[message.receiver_id].forEach(function (e, i) { socketIo.to(e).emit('user:notifications', message); }); } }); redisSub.subscribe('notifications'); app.get('/', function (req, res, next) { res.send('Hello World!'); }); socketIo.on('connection', function (socket) { // Validate if received connection is sending the user id console.log('Connection received'); if (!socket.handshake.query.hasOwnProperty('userId')) { console.log('User ID not found. Ignoring socket'); return; } else { console.log('User ID received. Emiting welcome message'); socket.emit('welcome', { message: 'Welcome' }); } // attach the user to the socket. var clientId = parseInt(socket.handshake.query.userId); var socketId = socket.id; if (clientId > 0) { if (!connections.clients.hasOwnProperty(clientId)) { connections.clients[clientId] = [socketId]; } else { connections.clients[clientId].push(socketId); } } // console.log(connections.clients); // // // socket.on('hi', function(data) { // // socket.broadcast.emit('hi', data); // // }); // Disconnect socket.on('disconnect', function () { console.log('User disconnected', clientId); // broadcast that this user is offline. var conns = connections.clients[clientId], socket_idx = conns.indexOf(socket.id), closing_socket = conns.splice(socket_idx, 1); // detach the client socket from the list of active connections if (conns.length == 0) { delete connections.clients[clientId]; } }); }); var Notification = function () { function Notification() { _classCallCheck(this, Notification); } _createClass(Notification, [{ key: 'init', value: function init() { server.listen(process.env.PORT || _config2.default.port, function () { console.log('Listening on ', process.env.PORT || _config2.default.port); }); } }]); return Notification; }(); var x = new Notification(); x.init();
| ver. 1.4 |
Github
|
.
| PHP 8.2.31 | Generation time: 0 |
proxy
|
phpinfo
|
Settings