【发布时间】:2018-07-06 06:45:44
【问题描述】:
我是 nodejs 的新手,我正在尝试实现从服务器到 Web 客户端的文件传输。说,服务器的工作是截取屏幕截图并将其发送到Web客户端我已经通过express实现了服务器和客户端之间的连接,并且我能够检查客户端是否连接,因为我使用的是express和socket。 io 我希望通过 'socket.broadcast.emit' 发送捕获的图像
这是我正在使用的代码:
sources.forEach(function (source) {
if(source.name === "Entire screen" || source.name === "Screen 1" ){
if(screenShotPath === ''){
screenShotPath = path.join(os.tmpdir(),'screenshot.jpeg');
}
console.log(screenShotPath);
io.sockets.on('connection', function(socket){
console.log('A new client is conencted');
socket.broadcast.emit('img', source.thumbnail.toPNG);
})
fs.writeFile(screenShotPath,source.thumbnail.toPNG(), function (err) {
if(err) return console.log(err.message);
shell.openExternal("file://"+screenShotPath);
var message = 'Saved SS to ' + screenShotPath;
screenshotMsg.textContent = message;
});
}
});
这行得通吗?有人可以告诉我如何接收发送的流吗? 这是发送图像并在 Web 客户端中接收图像的方式,还是有更简单的方法来实现此功能?
还有。有没有办法在两台服务器之间发送图像?假设一台服务器正在截屏,我必须将此图像保存在另一个服务器系统中,有没有办法做到这一点?
【问题讨论】: