mock平台

websocket.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const koaRouter = require('koa-router');
  2. const interfaceController = require('./controllers/interface.js');
  3. const yapi = require('./yapi.js');
  4. const router = koaRouter();
  5. const { createAction } = require("./utils/commons.js")
  6. let pluginsRouterPath = [];
  7. function addPluginRouter(config) {
  8. if (!config.path || !config.controller || !config.action) {
  9. throw new Error('Plugin Route config Error');
  10. }
  11. let method = config.method || 'GET';
  12. let routerPath = '/ws_plugin/' + config.path;
  13. if (pluginsRouterPath.indexOf(routerPath) > -1) {
  14. throw new Error('Plugin Route path conflict, please try rename the path')
  15. }
  16. pluginsRouterPath.push(routerPath);
  17. createAction(router, "/api", config.controller, config.action, routerPath, method, true);
  18. }
  19. function websocket(app) {
  20. createAction(router, "/api", interfaceController, "solveConflict", "/interface/solve_conflict", "get")
  21. yapi.emitHookSync('add_ws_router', addPluginRouter);
  22. app.ws.use(router.routes())
  23. app.ws.use(router.allowedMethods());
  24. app.ws.use(function (ctx, next) {
  25. return ctx.websocket.send(JSON.stringify({
  26. errcode: 404,
  27. errmsg: 'No Fount.'
  28. }));
  29. });
  30. }
  31. module.exports = websocket