两个 node.js 服务器之间的文件/数据传输 [英] File/Data transfer between two node.js servers

查看:67
本文介绍了两个 node.js 服务器之间的文件/数据传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个小型系统,当它看到另一台服务器(又名传感器)可用时,将数据文件传输到另一台服务器(又名服务器)(都运行 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),在阅读文档时看起来很有希望,但是,作为新手 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆