2017-05-24 22:50:02 -07:00
|
|
|
module.exports = function(app) {
|
2017-05-25 00:59:22 -07:00
|
|
|
var http = require('http').Server(app);
|
|
|
|
var io = require('socket.io')(http);
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var lbryApi = require('../helpers/lbryApi.js');
|
|
|
|
|
|
|
|
function sendTheImage(socket, filePath){
|
|
|
|
fs.readFile(filePath, function(err, buff){
|
|
|
|
if (err) {
|
2017-05-26 00:34:25 -07:00
|
|
|
console.log("socket: fs err:", err);
|
2017-05-25 00:59:22 -07:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
//console.log("buff", buff);
|
2017-05-26 00:34:25 -07:00
|
|
|
socket.emit('claim-send', { image: true, buffer: buff.toString('base64') });
|
|
|
|
console.log('socket: the image file has been sent via sockets');
|
2017-05-25 00:59:22 -07:00
|
|
|
});
|
|
|
|
}
|
2017-05-24 22:50:02 -07:00
|
|
|
|
|
|
|
io.on('connection', function(socket){
|
|
|
|
console.log('a user connected');
|
|
|
|
|
2017-05-25 00:59:22 -07:00
|
|
|
// serve an image file from the server
|
2017-05-26 00:34:25 -07:00
|
|
|
socket.on('claim-request', function(query){
|
2017-05-24 22:50:02 -07:00
|
|
|
// 1. retrieve the image from lbry via daemon
|
2017-05-26 00:34:25 -07:00
|
|
|
console.log("socket: received claim request for:", query)
|
|
|
|
if (query.indexOf("/") === -1){
|
|
|
|
var promise = lbryApi.getClaimBasedOnNameOnly(query)
|
|
|
|
} else {
|
|
|
|
var uri = query.replace("/", "#");
|
|
|
|
var promise = lbryApi.getClaimBasedOnUri(uri)
|
|
|
|
}
|
2017-05-25 00:59:22 -07:00
|
|
|
promise.then(function(data){
|
2017-05-26 00:34:25 -07:00
|
|
|
console.log("socket: claim-request - success:", data)
|
2017-05-25 00:59:22 -07:00
|
|
|
// 3. serve the image back once it is retrieved
|
|
|
|
sendTheImage(socket, data);
|
2017-05-26 00:34:25 -07:00
|
|
|
return;
|
2017-05-25 00:59:22 -07:00
|
|
|
})
|
|
|
|
.catch(function(error){
|
2017-05-26 00:34:25 -07:00
|
|
|
console.log("socket: claim-request - error:", error)
|
|
|
|
// handle the error
|
|
|
|
socket.emit("claim-update", error);
|
2017-05-25 00:59:22 -07:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
2017-05-24 22:50:02 -07:00
|
|
|
// 2. emit updates as the image is being retrieved
|
2017-05-26 00:34:25 -07:00
|
|
|
socket.emit("claim-update", "We are getting your claim for " + query);
|
2017-05-24 22:50:02 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
// handle disconnect
|
|
|
|
socket.on('disconnect', function(){
|
|
|
|
console.log('user disconnected');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return http;
|
|
|
|
}
|