-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (51 loc) · 1.91 KB
/
app.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/** *********************************************
* Copyright (C) 2017 All rights reserved.
***********************************************
* Filename: app.js
* Author: miaoyou.gmy
* Date: 2017-03-04
* Description:
*
* Modification:
*
***********************************************/
'use strict'
const fs = require('fs');
let Gaze = require('gaze').Gaze; // https://github.com/shama/gaze
let WebSocketServer = require('websocket').server; // https://github.com/theturtle32/WebSocket-Node
let clientPool = require('./app/model/dp-connectclientpool')
let mockMessageUtil = require('./app/model/dp-mockmessage')
module.exports = function (app) {
app.on('server', function (server) {
let wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false,
});
wsServer.on('request', (req) => {
clientPool.addNewClient(req);
})
})
// config
app.mockfileMap = new Map();
app.theraConfig = new Map();
// register responder
app.clientPool = clientPool;
let gaze = new Gaze([], {'interval': 1, 'mode': 'watch', 'debounceDelay': 1000});
gaze.on('changed', function (filepath) {
setTimeout(function(){
fs.readFile(filepath, 'utf8', function (err, data) {
if (err) {
throw err;
} else if (app.mockfileMap.get(filepath) && data) {
var mockModel = app.mockfileMap.get(filepath)
mockModel.data.mockList.forEach((element) => {
if (filepath === element.file) {
clientPool.sendAllClientMessage(JSON.stringify(mockMessageUtil.createMockDataMessageObject(filepath, element.api, element.path, data)))
}
})
}
})
},500);
});
app.gazeWather = gaze;
};