Created
November 11, 2024 11:24
-
-
Save brasizza/2f4010fccb4923ea3ceed5d534bf0f91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/node | |
const express = require('express'); | |
const { createClient } = require("redis"); | |
const { createAdapter } = require("@socket.io/redis-adapter"); | |
var app = require('express')(); | |
var server = require('http').Server(app); | |
const PATH_SOCKET = process.env.PATH_SOCKET; | |
var strPath = (PATH_SOCKET == null) ? '/socket-io/socket.io' : PATH_SOCKET+'/socket-io/socket.io'; | |
var strRestPath = (PATH_SOCKET == null) ? '/socket-io/notify' : PATH_SOCKET+'/socket-io/notify'; | |
var strRestPathRoot = (PATH_SOCKET == null) ? '/' : PATH_SOCKET+'/'; | |
console.log(strPath); | |
console.log(strRestPath); | |
var io = require('socket.io')(server, { | |
path: strPath, | |
origins: '*:*', | |
allowEIO3: false, // false by default | |
pingInterval: 60000, | |
pingTimeout: 60000, | |
upgrade: true, | |
}); | |
const cors = require('cors'); | |
const USING_REDIS = process.env.USING_REDIS; | |
if (USING_REDIS === 'true') { | |
const pubClient = createClient({ host: "redis-server", port: 6379 , password: '0RyHacxiCRGIn538SLRpC4t3clSaDnxr'}); | |
const subClient = pubClient.duplicate(); | |
console.log("USANDO SERVIDOR DE REDIS"); | |
io.adapter(createAdapter(pubClient, subClient)); | |
pubClient.on("error", (err) => { | |
console.log(err.message); | |
}); | |
subClient.on("error", (err) => { | |
console.log(err.message); | |
}); | |
}else{ | |
console.log("SEM SERVIDOR DE REDIS"); | |
} | |
io.of('/').adapter.on('error', function(error){ | |
console.log('error: ', error); | |
}); | |
io.sockets.on('connection', function (socket) { | |
console.log(socket.client.id + ' conectado com ip ' + socket.conn.remoteAddress); | |
socket.on('message_cloud', function (data) { | |
try { | |
var obj = JSON.parse(data); | |
console.log(obj); | |
const channel = obj.channel; | |
io.emit(channel, data); | |
} catch (e) { | |
} | |
}); | |
socket.on('disconnect', function (data) { | |
console.log(socket.client.id + ' desconectado com ip ' + socket.conn.remoteAddress + ':' + data); | |
}); | |
socket.on('reconnect', function (data) { | |
console.log(socket.client.id + ' reconectando com ip ' + socket.conn.remoteAddress + ':' + data); | |
}); | |
socket.on('pong', function (data) { | |
io.emit('pong', data); | |
}); | |
socket.on('ping', function (data) { | |
io.emit('pong', data); | |
}); | |
}); | |
const SOCKET_PORT = process.env.SOCKET_PORT; | |
if (SOCKET_PORT == undefined) { | |
throw new Error('VARIAVEL SOCKET_PORT VAZIA!'); | |
} | |
console.log("RUNNING ON PORT " + SOCKET_PORT); | |
app.use(cors()); | |
app.options('*', cors({ | |
credentials: true, | |
origin: true | |
})); | |
server.listen(SOCKET_PORT, '0.0.0.0'); | |
app.use(express.json()) | |
app.use(express.urlencoded({ | |
extended: true | |
})) | |
app.get(strRestPathRoot, function (_, res) { | |
res.writeHead(200); | |
res.end("OK"); | |
}); | |
function isEmpty(obj) { | |
for (var prop in obj) { | |
if (obj.hasOwnProperty(prop)) | |
return false; | |
} | |
return JSON.stringify(obj) === JSON.stringify({}); | |
} | |
app.post(strRestPath, function (req, res) { | |
if (isEmpty(req.body)) { | |
console.log('Object missing'); | |
return; | |
} | |
if (req.body.redirect) { | |
if (req.body.redirect == 'comanda-v2') { | |
if (req.body.versao != null) { | |
var request = require('request'); | |
var options = { | |
'method': 'POST', | |
'url': 'https://api.viapp.com.br/conta-v2/comanda', | |
'headers': { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
form: req.e | |
}; | |
request(options, function (error, _) { | |
if (error) { | |
console.log(error); | |
} | |
console.log('emitindo o ' + req.body.metodo) | |
io.sockets.emit(req.body.metodo, req.body); | |
}); | |
} else { | |
var request = require('request'); | |
var options = { | |
'method': 'POST', | |
'url': 'https://api.viapp.com.br/conta/v2/comanda/' + req.body.cnpj + '/' + req.body.cod_fidel + '/firebase', | |
'headers': { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
form: req.body | |
}; | |
request(options, function (error, _) { | |
if (error) { | |
console.log(error); | |
} | |
console.log('emitindo o ' + req.body.metodo) | |
io.sockets.emit(req.body.metodo, req.body); | |
}); | |
} | |
} else { | |
console.log('emitindo o ' + req.body.metodo) | |
io.sockets.emit(req.body.metodo, req.body); | |
} | |
} else { | |
console.log('emitindo o ' + req.body.metodo) | |
io.sockets.emit(req.body.metodo, req.body); | |
} | |
res.writeHead(200); | |
res.end(JSON.stringify(req.body)); | |
}); | |
function sendHeartbeat() { | |
setTimeout(sendHeartbeat, 10000); | |
io.sockets.emit('ping', { | |
beat: Math.random() | |
}); | |
} | |
io.sockets.on('message_cloud', function (data) { | |
try { | |
console.log(data); | |
var obj = JSON.parse(data); | |
console.log(obj); | |
const channel = obj.channel; | |
io.emit(channel, data); | |
} catch (e) { | |
console.log(e); | |
} | |
}); | |
setTimeout(sendHeartbeat, 10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment