两个node.js服务器之间的文件/数据传输 [英] File/Data transfer between two node.js servers
问题描述
我正在尝试建立一个小型系统,当一个服务器(也就是传感器)将数据文件传输到另一个服务器(也就是服务器)时(两个都运行node.js应用程序),当它看到另一个服务器可用时。
I am trying to set up a small system having one server (aka sensor) transfer data files to another server (aka server) (both running node.js apps) when it sees that the other is available.
理想情况下,服务器应该从传感器监听连接,当建立连接时,传感器会将所有可用的数据文件传输到服务器,然后关闭连接。
The server should ideally be listening for a connection from the sensor, when connection is made, the sensor will transfer all available data files to the server, then close the connection.
我一直在玩一个名为delivery.js的图书馆( https ://github.com/liamks/Delivery.js ),在阅读文档时看起来很有希望,但是,作为一名新手程序员,我很难理解为什么我对这个例子的尝试不起作用。
I have been playing around with a library called delivery.js (https://github.com/liamks/Delivery.js), it looks promising when reading through the documentation, however, being a novice js programmer, I am struggling to understand why my attempt at the example is not working.
我在传感器上使用以下代码:
I am using the following code on the sensor:
var io = require('socket.io'),
dl = require('delivery'),
fs = require('fs');
var socket = io.connect('http://192.168.0.14:5001');
socket.on('connect', function() {
log( "Sockets connected" );
delivery = dl.listen( socket );
delivery.connect();
delivery.on('delivery.connect',function(delivery){
delivery.send({
name: 'sample-image.jpg',
path : './sample-image.jpg'
});
delivery.on('send.success',function(file){
console.log('File sent successfully!');
});
});
});
运行示例代码的服务器:
And the server running the example code:
var io = require('socket.io').listen(5001),
dl = require('delivery');
io.sockets.on('connection', function(socket){
var delivery = dl.listen(socket);
delivery.on('delivery.connect',function(delivery){
delivery.send({
name: 'sample-image.jpg',
path : './sample-image.jpg'
});
delivery.on('send.success',function(file){
console.log('File successfully sent to client!');
});
});
});
服务器代码运行良好并创建侦听通信的套接字。但是,传感器代码会抛出错误:
The server code runs fine and creates the socket listening for communication. However, the sensor code throws error:
/Users/Oliver/Desktop/socket/delivery/test-delivery-client.js:5
var socket = io.connect('http://192.168.0.14:5001');
^
TypeError: Object #<Object> has no method 'connect'
at Object.<anonymous> (/Users/Oliver/Desktop/socket/delivery/test-delivery-client.js:5:17)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:349:32)
at Function.Module._load (module.js:305:12)
at Function.Module.runMain (module.js:490:10)
at startup (node.js:123:16)
at node.js:1029:3
如果有人能够解释我出错的地方并说明如何满足我的需求,那就太棒了。在此先感谢。
If anyone could explain where i am going wrong and indicate how to get my needs met, that would be great. Thanks in advance.
推荐答案
您使用的只是服务器 socket.io ,它只能接受传入的连接。要连接到另一台服务器,您需要 socket.io-client 软件包。这将为您提供通常在浏览器中使用的连接功能,现在您可以从node.js本身使用它。
What you are using is just the server socket.io which can only accept incoming connections. To connect to another server you need socket.io-client package. This will give you the connect function like one you usually use in the browser, now you can use it from node.js itself.
这篇关于两个node.js服务器之间的文件/数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!