-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_script.js
More file actions
78 lines (61 loc) · 2 KB
/
Copy pathserver_script.js
File metadata and controls
78 lines (61 loc) · 2 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
// This file runs from 'node server_script.js' on the
// command line
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const socketIo = require('socket.io');
const { diffChars } = require('diff');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const wss = new WebSocket.Server({ server });
// Initial SVG content
let currentSVG = `
<svg width="800" height="600">
<rect width="100%" height="100%" fill="white"/>
</svg>
`;
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
io.on('connection', (socket) => {
const connectTime = new Date().toISOString();
console.log(connectTime, ': A user connected'); // why (ISO maybe?) "${dateTime}" does not print?
socket.on('sendRequest', () => {
// Notify the administrator
io.emit('notification', 'User has sent a request!');
console.log('Notification sent to all clients');
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
wss.on('connection', (ws) => {
console.log('Client connected');
// Send initial SVG to the client
ws.send(JSON.stringify({ type: 'initialSVG', data: currentSVG }));
ws.on('message', (message) => {
const data = JSON.parse(message);
if (data.type === 'adminUpdate') {
const oldSVG = currentSVG;
currentSVG = data.svg;
// Compute the difference between old and new SVG
const differences = diffChars(oldSVG, currentSVG);
// Broadcast the diff to all clients
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'svgDiff', data: differences }));
}
});
}
});
ws.on('close', () => {
console.log('Client disconnected');
});
});
ws.on('close', () => {
console.log('Client disconnected');
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server is running on port ${PORT}`));