-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
112 lines (97 loc) · 2.88 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var fs = require('fs')
, http = require('http')
, express = require("express")
, ejs = require('ejs')
, path = require('path')
, socketio = require('socket.io');
//创建一个静态网页服务器,用来加载网页资源
app = express();
app.use(express.static(__dirname+'/'));
var allData="";
var judge = false; //判断是否存入文件
var server = http.createServer(app,function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(8080, function() {
console.log('Listening at: http://localhost:8080');
});
socketio.listen(server).on('connection', function (socket) {
socket.on('message', function (msg) {
//把消息压入数组中
if (judge) {
allData = allData + msg+"\n";
}
//把消息广播出去
socket.broadcast.emit('message', msg);
});
});
//开始录制
app.get('/begin', function(req, res){
judge = true;
res.send('begin');
});
//显示存的播放文件
app.get('/showFiles',function (req,res) {
var filesname = ls(__dirname+'/recordSave');
var appPath = __dirname+'/show.html';
var str = fs.readFileSync(appPath,'utf8');
var ret = ejs.render(str,{files:filesname});
res.send(ret);
});
//播放对应的文件
app.get('/play',function (req,res) {
var fileName = __dirname+ '/recordSave/'+req.query.filename;
var filedata = readFileToArray(fileName);
var appPath = __dirname+'/play.html';
var str = fs.readFileSync(appPath,'utf8');
var ret = ejs.render(str,{alldata:filedata});
res.send(ret);
});
//把消息存入文件中
app.get('/save', function(req, res){
//根据时间来为文件命名
// var filename = new Date().getTime();
var filename = req.param('filename');
var filepath = __dirname+"/recordSave/"+filename+".txt";
//以写入模式打开文件,不存在者创建文件
fs.open(filepath,'w',function (err,fd){
if (err) {
console.error(err);
return;
}
});
//写入数据
fs.writeFile(filepath,allData,function (err) {
if (err) throw err;
});
judge = false; //把当前置为没有存入状态
allData=""; //清空数据
res.send('文件保存成功');
});
//读取文件中的内容到数组中并返回
function readFileToArray(filepath) {
var dataArray=[];
var data = fs.readFileSync(filepath,"utf8");
var alldata = (data.toString()).split('\n');
for (var i = 0 ; i < alldata.length ; i++) {
alldata[i] = alldata[i].replace( /\r|\n/g, "" ); //替换到里面的换行符
dataArray.push(alldata[i]);
}
return dataArray;
}
//遍历文件夹下的文件
function ls(filepath) {
var filesname =[];
var files = fs.readdirSync(filepath);
for(fn in files)
{
var fname = filepath+path.sep+files[fn];
var stat = fs.lstatSync(fname);
if(stat.isDirectory() != true)
{
console.log(files[fn]);
filesname.push(files[fn]);
}
}
return filesname;
}