You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

72 lines
2.0 KiB

const app = require('express')();
const bodyParser = require('body-parser');
const server = require('http').createServer(app);
const io = require('socket.io')(server, {
cors: {
origin: "http://127.0.0.1:3000",
methods: ["GET", "POST"],
}
});
const redisAdapter = require('socket.io-redis');
const axios = require('axios');
//setup redis
io.adapter(redisAdapter({host: '127.0.0.1', port: 6379, password: 'root'}));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
// server main route
app.get('/', (req, res) => {
res.sendFile(__dirname + '/static/index.html');
});
io.on('connection', async (socket) => {
const auth = socket.handshake.query.auth;
const business = socket.handshake.query.business;
const AuthStr = 'Bearer '.concat(auth);
console.log('user conencted : ', auth);
if (auth !== undefined) {
axios
.get('http://127.0.0.1:80/user/v1/businesses/'+business, {
headers: { Authorization: AuthStr }
})
.then((res) => {
// console.log(res.data)
if (res.status === 200) {
console.log('user joined to,', 'room-'+business);
socket.join('room-'+business);
}
})
.catch((error) => {
console.error(error)
})
}
});
// app.get('/:room/:event', function (req, res) {
// console.log(req.query);
// // io.to(req.params.room).emit(req.params.event, req.params.message);
// res.send('done');
// });
app.post('/emit/:room', function (req, res) {
// console.log(req.body);
console.log('room is :','room-' + req.params.room);
console.log('message',req.body.data);
io.to('room-' + req.params.room).emit('room-event', req.body.data);
// console.log(req.body.data);
// res.json(req.body.data)
res.send('done');
});
server.listen(3030, () => {
console.log('listening on *:3030');
});