forked from ChangWang9/LawGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (54 loc) · 1.6 KB
/
Copy pathserver.js
File metadata and controls
64 lines (54 loc) · 1.6 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const fs = require('fs');
const path = require('path');
const WebSocket = require('ws');
const filePath = path.join(__dirname, 'agentscope', 'runs', 'TEST', 'logging.chat');
let lastSize = 0;
// 创建 WebSocket 服务器,使用3000端口
const wss = new WebSocket.Server({ port: 3000 });
// 读取文件新内容的函数
function readNewContent(ws, currentSize) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('读取文件失败:', err);
return;
}
// 只获取新增的内容
const newContent = data.slice(lastSize);
if (newContent) {
console.log('发送新内容:', newContent);
ws.send(newContent);
lastSize = data.length;
}
});
}
wss.on('connection', (ws) => {
console.log('客户端已连接');
// 首先发送现有的文件内容
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('读取文件失败:', err);
return;
}
if (data) {
console.log('发送初始内容');
ws.send(data);
lastSize = data.length;
}
// 使用 watchFile 替代 watch,监控文件大小变化
fs.watchFile(filePath, { interval: 1000 }, (curr, prev) => {
if (curr.size > lastSize) {
console.log('检测到文件变化');
readNewContent(ws, lastSize);
}
});
});
// 当连接关闭时,停止监听文件
ws.on('close', () => {
console.log('客户端断开连接');
fs.unwatchFile(filePath);
//清空data
lastSize = 0;
fs.writeFileSync(filePath, '');
});
});
console.log('WebSocket 服务器已在端口3000上运行');