Browse Source

fix handle sdp messages via http

afrokick 6 years ago
parent
commit
7224de69b7
3 changed files with 32 additions and 40 deletions
  1. 1 1
      src/api/index.js
  2. 1 1
      src/api/middleware/auth/index.js
  3. 30 38
      src/api/v1/calls/index.js

+ 1 - 1
src/api/index.js

@@ -14,5 +14,5 @@ app.get('/', (req, res, next) => {
   res.send(publicContent);
 });
 
-app.use('/:key', authMiddleware, require('./v1/public'));
+app.use('/:key', require('./v1/public'));
 app.use('/:key/:id/:token', authMiddleware, jsonParser, require('./v1/calls'));

+ 1 - 1
src/api/middleware/auth/index.js

@@ -10,7 +10,7 @@ module.exports = (req, res, next) => {
   }
 
   if (!id) {
-    return next();
+    return res.sendStatus(401);
   }
 
   const client = realm.getClientById(id);

+ 30 - 38
src/api/v1/calls/index.js

@@ -1,42 +1,34 @@
 const express = require('express');
-// const realm = require('../../../realm');
+const realm = require('../../../services/realm');
+const messageHandler = require('../../../messageHandler');
 
 const app = module.exports = express.Router();
 
-// const handle = (req, res, next) => {
-//   var id = req.params.id;
-
-//   let client;
-//   if (!(client = realm.getClientById(id))) {
-//     if (req.params.retry) {
-//       res.sendStatus(401);
-//       return;
-//     } else {
-//       // Retry this request
-//       req.params.retry = true;
-//       setTimeout(handle, 25, req, res);
-//       return;
-//     }
-//   }
-
-//   // Auth the req
-//   if (client.token && req.params.token !== client.token) {
-//     res.sendStatus(401);
-//   } else {
-//     self._handleTransmission(key, {
-//       type: req.body.type,
-//       src: id,
-//       dst: req.body.dst,
-//       payload: req.body.payload
-//     });
-//     res.sendStatus(200);
-//   }
-// };
-
-// app.post('/:key/:id/:token/offer', jsonParser, handle);
-
-// app.post('/:key/:id/:token/candidate', jsonParser, handle);
-
-// app.post('/:key/:id/:token/answer', jsonParser, handle);
-
-// app.post('/:key/:id/:token/leave', jsonParser, handle);
+const handle = (req, res, next) => {
+  const { id } = req.params;
+
+  if (!id) return next();
+
+  const client = realm.getClientById(id);
+
+  const { type, dst, payload } = req.body;
+
+  const message = {
+    type,
+    src: id,
+    dst,
+    payload
+  };
+
+  messageHandler(client, message);
+
+  res.sendStatus(200);
+};
+
+app.post('/offer', handle);
+
+app.post('/candidate', handle);
+
+app.post('/answer', handle);
+
+app.post('/leave', handle);