-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
80 lines (65 loc) · 2.18 KB
/
server.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
const express = require('express');
const app = express();
const http = require('http').Server(app);
var io = require('socket.io').listen(http);
var fs = require('fs'); //require for file serving
var ejs = require('ejs');
var users = [];
var connections = [];
var vmnumber = 8;
const imgFolder = '/nfs/nfs/imgstream/images/';
const imgList = fs.readdirSync(imgFolder);
http.listen(3000, function() {
console.log('Server running... on port 3000');
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
app.use(express.static(imgFolder));
io.sockets.on('connection', function(socket) {
console.log("start1");
connections.push(socket);
console.log('Connected: %s sockets connected', connections.length);
//send file list to browser
socket.emit('imgList', { message: imgList });
// Diconnect
socket.on('disconnect', function(data) {
connections.splice(connections.indexOf(socket), 1);
console.log('Disconnected %s sockets connected', connections.length);
});
function readMyFile(str) {
fs.readFile(imgFolder + str, function(err, buf) {
//it's impossible to ebed binary data
// console.log("Emit " + str);
socket.emit(str, { image: true, buffer: buf.toString('base64') });
console.log('image file is initialized: ' + imgFolder + str);
});
}
imgList.forEach(file => {
//console.log("send to readMyFile" + file);
readMyFile(file);
})
});
// watch file modification and on modification send the updated version
function watchMyFile(str) {
var watch = require('node-watch');
watch(imgFolder + str, function(evt, filename) {
if (evt === 'update') {
fs.readFile(imgFolder + str, function(err, buf) {
for (var i = 0; i < connections.length; i++) {
//it's impossible to embed binary data
connections[i].emit(str, { image: true, buffer: buf.toString('base64') });
console.log('image updated: ' + __dirname + '/images/' + str );
}
});
}
});
var watchnew = require('node-watch');
watchnew([imgFolder + str], console.log);
}
//set watcher to each file
imgList.forEach(file => {
//console.log(file);
watchMyFile(file);
})
//console.log("flist: " + imgList)