3d-interface/socket.js

81 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-06-09 17:40:58 +02:00
var fs = require('fs');
var sleep = require('sleep');
module.exports = function(io) {
io.on('connection', function(socket) {
2015-06-10 11:24:59 +02:00
var index = 0;
var path;
var vIndex = 0;
var fIndex = 0;
console.log(socket.conn.remoteAddress + " connected !");
2015-06-09 17:40:58 +02:00
socket.on('disconnect', function() {
2015-06-10 11:24:59 +02:00
console.log(socket.conn.remoteAddress + " disconnected !");
2015-06-09 17:40:58 +02:00
});
socket.on("request", function(res) {
console.log('Asking for static/data/spheres/' + res + '.obj');
2015-06-10 11:24:59 +02:00
path = 'static/data/spheres/' + res + '.obj.obj';
socket.emit('ok');
});
socket.on('next', function() {
fs.readFile(path, function(err, data) {
2015-06-09 17:40:58 +02:00
var lines = data.toString('utf-8').split("\n");
2015-06-10 11:24:59 +02:00
var line = lines[index];
/// while (line && line.length === 0) {
/// line = lines[++index];
/// }
if (!line) {
socket.emit('finished');
return;
}
2015-06-09 17:40:58 +02:00
2015-06-10 11:24:59 +02:00
if (line[0] === 'v') {
var arr = line.split(" ");
arr[0] = vIndex++;
arr[1] = parseFloat(arr[1]);
arr[2] = parseFloat(arr[2]);
arr[3] = parseFloat(arr[3]);
socket.emit('vertex', arr);
index++;
return;
} else if (line[0] === 'f') {
fIndex++;
var arr = line.split(" ");
arr.shift();
arr[0]--;
arr[1]--;
arr[2]--;
if (arr[3]) {
arr[3]--;
fIndex++;
2015-06-09 17:40:58 +02:00
}
2015-06-10 11:24:59 +02:00
socket.emit('face', arr);
index++;
return;
2015-06-09 17:40:58 +02:00
}
2015-06-10 11:24:59 +02:00
socket.emit('none');
2015-06-09 17:40:58 +02:00
// socket.disconnect();
});
});
});
}