-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
77 lines (59 loc) · 2.14 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
var io = require("socket.io"),
http = require("http"),
fs = require("fs");
var videoFilePath = "/tmp/web_cam_data/1.jpg"
var htmlBody = fs.readFileSync("public/index.html","utf-8");
var clients = [];
function encodFileBase64(path) {
try {
var imageContent = fs.readFileSync(path);
return new Buffer(imageContent).toString('base64');
} catch (err) {
return ''
}
}
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-type":"text/html"});
//imageB64Encoded = encodFileBase64(videoFilePath);
//response.end(htmlBody.replace('##anchor##', imageB64Encoded));
response.end(htmlBody);
});
server.listen(8080);
var sockvar = io.listen(server);
sockvar.set('log level', 1);
sockvar.sockets.on('connection', function(socket){
console.log(" event is 'connection'");
clients.push({"socketId":socket.id, "isTransmiting":false});
imageB64Encoded = encodFileBase64(videoFilePath);
socket.emit('message', {'message': imageB64Encoded});
socket.on('disconnect', function() {
console.log(" event is 'disconnect'");
for (var i=0; i<clients.length; i++) {
if (clients[i].socketId == socket.id) {
clients.splice(i,1);
}
}
});
socket.on('imageHasBeenLoaded', function () {
console.log(' event is imageHasBeenLoaded');
for (var i=0; i < clients.length; i++) {
if (clients[i].socketId == socket.id) {
clients[i].isTransmiting = false;
}
}
});
});
fs.watch(videoFilePath, function (event, filename) {
console.log(' event is: ' + event);
//console.log(event);
imageB64Encoded = encodFileBase64(videoFilePath);
if (imageB64Encoded.length !=0) {
for (var i=0; i<clients.length; i++) {
if (clients[i].isTransmiting == false) {
clients[i].isTransmiting = true;
clientId = clients[i].socketId;
sockvar.sockets.sockets[clientId].emit('message', {'message': imageB64Encoded});
}
}
}
});