-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathws-server.js
More file actions
139 lines (120 loc) · 3.86 KB
/
ws-server.js
File metadata and controls
139 lines (120 loc) · 3.86 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const http = require('http');
const WebSocket = require('ws');
const fs = require('fs');
const path = require('path');
const PORT = process.env.WS_PORT || 8889;
const SETTINGS_FILE = path.join(__dirname, 'global-settings.json');
// Global settings storage
let globalSettings = {};
// Load settings from file
function loadSettings() {
try {
if (fs.existsSync(SETTINGS_FILE)) {
const data = fs.readFileSync(SETTINGS_FILE, 'utf8');
globalSettings = JSON.parse(data);
console.log('Loaded global settings from file');
}
} catch (e) {
console.log('Failed to load settings, starting fresh');
globalSettings = {};
}
}
// Save settings to file
function saveSettings() {
try {
fs.writeFileSync(SETTINGS_FILE, JSON.stringify(globalSettings, null, 2));
} catch (e) {
console.log('Failed to save settings:', e.message);
}
}
// Broadcast helper
function broadcast(data, exclude) {
wss.clients.forEach((client) => {
if (client !== exclude && client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
}
loadSettings();
const server = http.createServer((req, res) => {
// Add CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
if (req.url === '/settings' && req.method === 'GET') {
// Return all current settings
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(globalSettings));
} else if (req.url === '/settings' && req.method === 'POST') {
// Update settings
let body = '';
req.on('data', chunk => body += chunk);
req.on('end', () => {
try {
const updates = JSON.parse(body);
Object.assign(globalSettings, updates);
saveSettings();
// Broadcast changes to all WebSocket clients
broadcast(JSON.stringify({
type: 'settings_update',
settings: globalSettings
}));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
} catch (e) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
});
} else {
res.writeHead(404);
res.end('Not found');
}
});
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws, req) => {
console.log('Client connected to settings sync');
// Send current settings to new client
ws.send(JSON.stringify({
type: 'settings_update',
settings: globalSettings
}));
ws.on('message', (message) => {
try {
const data = JSON.parse(message.toString());
if (data && data.type === 'settings_change') {
console.log('Received settings change:', data);
// Update global settings
if (data.key) {
const oldValue = globalSettings[data.key];
if (data.newValue === null) {
delete globalSettings[data.key];
} else {
globalSettings[data.key] = data.newValue;
}
saveSettings();
console.log('Updated global settings, broadcasting to', wss.clients.size - 1, 'other clients');
// Broadcast to all other clients
broadcast(JSON.stringify({
type: 'settings_update',
settings: globalSettings
}), ws);
}
}
} catch (e) {
console.log('Invalid message:', e);
}
});
ws.on('close', () => {
console.log('Client disconnected from settings sync');
});
});
server.listen(PORT, () => {
console.log(`Global settings server listening on http://0.0.0.0:${PORT}`);
console.log(`WebSocket server listening on ws://0.0.0.0:${PORT}`);
});