UNPKG

yekonga-server

Version:
275 lines (221 loc) 8.63 kB
// @ts-nocheck /*global Yekonga, global.serverLibrary */ const moment = global.serverLibrary.moment; const path = global.serverLibrary.path; const { setupMaster, setupWorker } = serverLibrary.socketSticky; const { createAdapter, setupPrimary } = serverLibrary.socketClusterAdapter; const Middleware = require(path.join(__dirname, 'middleware')); var ioAccount = null; var ioSync = null; class Socket { constructor() { this.namespace = null; this.roomId = null; this.socket = Yekonga.socketServer; this.connect = Yekonga.socketServer; } of(namespace) { this.namespace = namespace; return this; } to(id) { this.roomId = id; return this; } room(id) { return this.to(id); } emit(event, data) { try { var io = Yekonga.socketServer; if (this.namespace) io = io.of(this.namespace); if (this.roomId) io = io.to(this.roomId); io.emit(event, data); } catch (error) { console.error('Socket.emit', error.message); } } on(event, callback) { var io = Yekonga.socketServer; if (this.namespace) io = io.of(this.namespace); io.on(event, callback); } get broadcast() { return Yekonga.socketServer.broadcast; } } module.exports = function(server) { Yekonga.socketServer = new global.serverLibrary.socketServer(server, { origin: '*', cors: { origin: '*', } }); if(!(serverLibrary.cluster.isPrimary || serverLibrary.cluster.isMaster)) { Yekonga.socketServer.adapter(createAdapter()); setupWorker(Yekonga.socketServer); } console.info('socket is process master?', `${serverLibrary.cluster.isMaster}`); if(Yekonga.Config.ports.redis) { const redisClient = global.serverLibrary.createClient({ url: `redis://localhost:${Yekonga.Config.ports.redis}` }); const subClient = redisClient.duplicate(); redisClient.connect().then(() => { Yekonga.socketServer.adapter(global.serverLibrary.createAdapter(redisClient, subClient)); }); } Yekonga.socketSystem = Yekonga.socketServer.of('/system'); Yekonga.pushNotification = Yekonga.socketServer.of('/pushNotification'); // Namespace Yekonga.socketServer.use(async function(socket, next) { socket.Yekonga = {} socket.headers = socket.handshake.headers; await Middleware.setYekonga(socket, {}, null); await Middleware.init(socket, {}, null); await Middleware.setToken(socket, {}, null); await Middleware.authorization(socket, {}, null); // console.log('socket ======> ', socket); if(typeof next == 'function') { next(); } }).on('connection', function(socket) { // console.log(`socket connected`); socket.on('subscribe', function(room) { // console.log('subscribe', room); socket.join(room); }) socket.on('unsubscribe', function(room) { // console.log('unsubscribe', room); socket.leave(room); }) socket.on('run-on-server', function(content) { console.log('data', content); }); socket.on('run-on-client', function(content) { console.log('data', content); }); socket.on('run-on-desktop', function(content) { console.log('data', content); }); socket.on('graphql-request', function({ room, listener, body, headers }) { var routeApi = (Yekonga.Config.graphql && Yekonga.Config.graphql.apiRoute) ? Yekonga.Config.graphql.apiRoute : `graphql`; Yekonga.Helper.request('post', { url: `http://127.0.0.1:${Yekonga.Config.ports.server}${routeApi}`, body: body, headers: headers, json: (typeof body == 'object') ? true : false }).then( (response) => { var responseBody = response.data; var content = (responseBody && responseBody.data) ? responseBody.data : responseBody; if (content) { Yekonga.Socket.to(room).emit('graphql-response', { listener: listener, body: content, }); } }, (error) => { console.error(error); } ); }); }); // Namespace Yekonga.socketSystem.use(function(socket, next) { if (!Yekonga.Auth) { if (socket.handshake.query && socket.handshake.query.token) { Middleware.setToken(socket.handshake); } else { // next(new Error('Authentication error')); } } next(); }).on('connection', function(socket) { // console.log(`socket.of('/system') connected`); socket.on('subscribe', function(room) { // console.log('subscribe', room); socket.join(room); }) socket.on('unsubscribe', function(room) { // console.log('unsubscribe', room); socket.leave(room); }) socket.on('run-on-server', function(content) { console.log('data', content); }); socket.on('run-on-client', function(content) { console.log('data', content); }); socket.on('run-on-desktop', function(content) { console.log('data', content); }); socket.on('confirm', function(content) { console.log('data', content); }); }); // Namespace Yekonga.pushNotification.use(function(socket, next) { if (!Yekonga.Auth) { if (socket.handshake.query && socket.handshake.query.token) { // Middleware.setToken(socket.handshake); } else { // next(new Error('Authentication error')); } } next(); }).on('connection', function(socket) { // console.log(`socket.of('/pushNotification') connected`); socket.on('subscribe', function(room) { // console.log('subscribe', room); socket.join(room); }) socket.on('unsubscribe', function(room) { // console.log('unsubscribe', room); socket.leave(room); }) socket.on('disconnect', function(room) { // console.log('unsubscribe', room); socket.leave(room); }) socket.on('acknowledge', async function(id) { console.log('acknowledge', id); await Yekonga.Model.PushNotification.update({ status: "delivered" }, { pushNotificationId: id }, null, true); }); }); if (typeof Yekonga.setPeer == 'function') { Yekonga.setPeer(Yekonga.socketServer); } if (typeof Yekonga.setChat == 'function') { Yekonga.setChat(Yekonga.socketServer); } setInterval(() => { processPushNotifications(); }, 5000); } async function processPushNotifications() { let timestamp = Yekonga.Helper.getMoment().subtract(5, 'minutes').format('YYYY-MM-DD HH:mm:ss'); let list = await Yekonga.Model.PushNotification.find({ status: "waiting" }, true); let sentList = await Yekonga.Model.PushNotification.find({ status: "sent", updatedAt: { greaterThan: timestamp } }, true); // console.log(list); for (const row of list) { row.id = row.pushNotificationId; await Yekonga.Model.PushNotification.update({ status: "sent", updatedAt: timestamp }, { pushNotificationId: row.pushNotificationId }, null, true); if (row.userId) { Yekonga.pushNotification.to(`${row.userId}`).emit('notification', row); } else if (row.deviceId) { Yekonga.pushNotification.to(row.deviceId).emit('notification', row); } else { Yekonga.pushNotification.emit('notification', row); } } for (const row of sentList) { } } Object.defineProperty(Yekonga, "Socket", { get: function() { return new Socket(); } });