|
|
@ -1,23 +1,24 @@ |
|
|
|
const app = require('express')(); |
|
|
|
const bodyParser = require('body-parser'); |
|
|
|
const express = require("express"); |
|
|
|
const app = express(); |
|
|
|
const server = require('http').createServer(app); |
|
|
|
const io = require('socket.io')(server, { |
|
|
|
cors: { |
|
|
|
origin: "http://127.0.0.1:3000", |
|
|
|
origin: "http://127.0.0.1:8000", |
|
|
|
methods: ["GET", "POST"], |
|
|
|
} |
|
|
|
}); |
|
|
|
const redisAdapter = require('socket.io-redis'); |
|
|
|
const axios = require('axios'); |
|
|
|
const redis = require("redis"); |
|
|
|
const subscriber = redis.createClient({host: '127.0.0.1', port: 6379, password: 'root'}); |
|
|
|
|
|
|
|
//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 })); |
|
|
|
app.use(express.urlencoded({ extended: false })); |
|
|
|
|
|
|
|
// parse application/json
|
|
|
|
app.use(bodyParser.json()); |
|
|
|
app.use(express.json()); |
|
|
|
|
|
|
|
// server main route
|
|
|
|
app.get('/', (req, res) => { |
|
|
@ -25,48 +26,26 @@ app.get('/', (req, res) => { |
|
|
|
}); |
|
|
|
|
|
|
|
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) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
socket.join('room'); |
|
|
|
// this is just for testing.
|
|
|
|
setTimeout(() => { |
|
|
|
io.to('room').emit('room-event', {index: 'users', id: undefined}); |
|
|
|
}, 10*1000); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
console.log('message',req.body.data.title); |
|
|
|
io.to('room-' + req.params.room).emit('room-event', req.body.data.title); |
|
|
|
res.send('done'); |
|
|
|
}); |
|
|
|
subscriber.on("message", function (channel, message) { |
|
|
|
io.to('room').emit('server-msg', message); |
|
|
|
}); |
|
|
|
|
|
|
|
server.listen(3030, () => { |
|
|
|
console.log('listening on *:3030'); |
|
|
|
}); |
|
|
|
|
|
|
|
subscriber.subscribe("test-channel"); |