-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.js
More file actions
executable file
·41 lines (31 loc) · 1.17 KB
/
websocket.js
File metadata and controls
executable file
·41 lines (31 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const koaRouter = require('koa-router');
const interfaceController = require('./controllers/interface.js');
const yapi = require('./yapi.js');
const router = koaRouter();
const { createAction } = require("./utils/commons.js")
let pluginsRouterPath = [];
function addPluginRouter(config) {
if (!config.path || !config.controller || !config.action) {
throw new Error('Plugin Route config Error');
}
let method = config.method || 'GET';
let routerPath = '/ws_plugin/' + config.path;
if (pluginsRouterPath.indexOf(routerPath) > -1) {
throw new Error('Plugin Route path conflict, please try rename the path')
}
pluginsRouterPath.push(routerPath);
createAction(router, "/api", config.controller, config.action, routerPath, method, true);
}
function websocket(app) {
createAction(router, "/api", interfaceController, "solveConflict", "/interface/solve_conflict", "get")
yapi.emitHookSync('add_ws_router', addPluginRouter);
app.ws.use(router.routes())
app.ws.use(router.allowedMethods());
app.ws.use(function (ctx, next) {
return ctx.websocket.send(JSON.stringify({
errcode: 404,
errmsg: 'No Fount.'
}));
});
}
module.exports = websocket