-
Notifications
You must be signed in to change notification settings - Fork 2
/
livePhotoServer.js
155 lines (115 loc) · 3.59 KB
/
livePhotoServer.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Include the http module (built-in to NodeJS)
var http = require('http');
// Include Express to pick up web requests
var express = require('express'),
// Start Express up
app = express();
// Create a new server using our express app
var server = http.createServer(app);
//set path to static files
app.use(express.static(__dirname + '/public'));
// Include File System Module
var fs = require('fs');
// Start a new websocket on the server's address
var io = require('socket.io').listen(server);
// Create an array to store open socket connections with
var sockets = [];
// Create an array to store the name of images we've already displayed
var images = [];
// Import the directory watching module
var watch = require('node-watch');
// An interval to make sure we don't send out the link prior to upload completion
var checkCompletionInterval;
/*
* Start watching the image directory for changes.
* When we detect a change, notify the clients
*/
watch('public/images', function(filename) {
// Find the new image that triggered this
var newImg = findNewImageName();
// If we do have a new image (and it was just deleted or something)
if (newImg) {
// Print out its name to the terminal
console.log("New Image Name: " + newImg);
// If we have an interval going, cancel it
if (checkCompletionInterval) clearInterval(checkCompletionInterval);
// Send the image out to all of the clients in 500 ms
checkCompletionInterval = setInterval(sendImgToClients, 500, newImg);
// Add it to the list of displayed images
images.push(newImg);
}
});
/*
Send the image to each client web socket
*/
function sendImgToClients(newImg) {
// For each open socket
for (var i = 0; i < sockets.length; i++) {
// Send the message to the socket
sockets[i].emit('newImage', {newImage : "/images/" + newImg});
}
// Clear the completion interval
clearInterval(checkCompletionInterval);
}
/*
* If we get a request to our root route, send the html page back
*/
app.get('/', function(req, res) {
// Read the html file into a text buffer
fs.readFile(__dirname + '/public/livePhoto.html', 'utf8', function(err, text){
// Send out the buffer
res.send(text);
});
})
/*
* Create new socket connections
* and handle teardowns
*/
io.sockets.on('connection', function (socket) {
// If there is a socket
if (socket) {
// Set our global socket to it for later
sockets.push(socket);
}
// When a socket disconnects
socket.on('disconnect', function () {
// Remove that socket from our array
delete sockets[socket];
});
});
/*
Scan the array of images until we
find one we haven't accounted for yet.
*/
function findNewImageName() {
// Grab the list if images in the directory
var files = fs.readdirSync('public/images');
// Iterate through the images
for (var i = 0; i < files.length; i++) {
// If the image name isn't in our array of account for images
// it's the new one, so return it.
if (images.indexOf(files[i]) == -1 && files[i].indexOf('.DS') == -1) {
return files[i];
}
}
}
/*
Add the pre-existing images
to our image array so that
we don't confuse it with a new one.
*/
function logExistingImages() {
// Grab all the current images
var files = fs.readdirSync('public/images');
// For each file in the directory
for (var i = 0; i < files.length; i++) {
// Add it to the array
images.push(files[i]);
}
}
// Catalog all existing images
logExistingImages();
// Start the server on port 8080
server.listen(8080);
//Write out the port just for fun
console.log("listening on port 8080");