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.

62 lines
1.7 KiB

4 years ago
4 years ago
4 years ago
  1. const app = require('express')();
  2. const bodyParser = require('body-parser');
  3. const server = require('http').createServer(app);
  4. const io = require('socket.io')(server);
  5. const redisAdapter = require('socket.io-redis');
  6. const axios = require('axios');
  7. //setup redis
  8. io.adapter(redisAdapter({host: '127.0.0.1', port: 6379, password: 'root'}));
  9. // parse application/x-www-form-urlencoded
  10. app.use(bodyParser.urlencoded({ extended: false }));
  11. // parse application/json
  12. app.use(bodyParser.json());
  13. // server main route
  14. app.get('/', (req, res) => {
  15. res.sendFile(__dirname + '/static/index.html');
  16. });
  17. io.on('connection', async (socket) => {
  18. const auth = socket.handshake.query.auth;
  19. const business = socket.handshake.query.business;
  20. const AuthStr = 'Bearer '.concat(auth);
  21. if (auth !== undefined) {
  22. axios
  23. .get('http://127.0.0.1:80/user/v1/businesses/'+business, {
  24. headers: { Authorization: AuthStr }
  25. })
  26. .then((res) => {
  27. // console.log(res.data)
  28. if (res.status === 200) {
  29. socket.join('room-'+business);
  30. }
  31. })
  32. .catch((error) => {
  33. console.error(error)
  34. })
  35. }
  36. });
  37. // app.get('/:room/:event', function (req, res) {
  38. // console.log(req.query);
  39. // // io.to(req.params.room).emit(req.params.event, req.params.message);
  40. // res.send('done');
  41. // });
  42. app.post('/emit/:room', function (req, res) {
  43. // console.log(req.body);
  44. io.to('room-' + req.params.room).emit('room-event', req.body.data.title);
  45. res.send('done');
  46. });
  47. server.listen(3030, () => {
  48. console.log('listening on *:3000');
  49. });