简体   繁体   English

如何将从UDP接收的数据传输到node.js中的网页

[英]how to transfer data received from UDP to the webpage in node.js

I'm running the two apps from here . 我正在从这里运行这两个应用程序。 So we have a client: 因此,我们有一个客户:

var PORT = 5007 ;
  var dgram = require('dgram');
  var client = dgram.createSocket('udp4');

  client.on('listening', function () {
      var address = client.address();
      console.log('UDP Client listening on ' + address.address + ":" + address.port);
      client.setBroadcast(true)
      client.setMulticastTTL(128); 
      client.addMembership('224.1.1.1');
  });

  client.on('message', function (message, remote) {   
      console.log('A: Epic Command Received. Preparing Relay.');
      console.log('B: From: ' + remote.address + ':' + remote.port +' - ' + message);
  });

  client.bind(PORT);

and a server: 和一台服务器:

var PORT = 5007 ;
  var dgram = require('dgram');
  var client = dgram.createSocket('udp4');

  client.on('listening', function () {
      var address = client.address();
      console.log('UDP Client listening on ' + address.address + ":" + address.port);
      client.setBroadcast(true)
      client.setMulticastTTL(128); 
      client.addMembership('224.1.1.1');
  });

  client.on('message', function (message, remote) {   
      console.log('A: Epic Command Received. Preparing Relay.');
      console.log('B: From: ' + remote.address + ':' + remote.port +' - ' + message);
  });

  client.bind(PORT);

It works pretty well, when I run it in two separates consoles - I see the transfer is going and the messages appear on both sites. 当我在两个单独的控制台中运行它时,它运行得非常好-我看到传输正在进行,并且消息出现在两个站点上。 Now, how can I display/transfer all this data to any webpage? 现在,如何将所有这些数据显示/传输到任何网页? Thanks! 谢谢!

Well it depends on what type of data it is combined with if you want it to be real-time or served upon request. 好吧,这取决于将其组合为哪种类型的数据,如果您希望它们是实时的或根据请求提供。

If you want it to be served upon request, you'd have to store the data somewhere so it can be accessed at a later point. 如果您希望根据要求提供数据,则必须将数据存储在某个地方,以便以后可以访问。 Either on the filesystem or in something like MongoDB's GridFS , or either directly in a database (as I said, depending on what type of data it is) 要么在文件系统上,要么在MongoDB的GridFS之的文件中 ,或者直接在数据库中(如我所说,取决于它是什么类型的数据)

If you want it to be streamed real-time to connected web clients you'd have to use something like Socket.io or WebRTC . 如果希望将其实时流式传输到连接的Web客户端,则必须使用Socket.ioWebRTC之类的东西。 The difference between the two are significant. 两者之间的差异非常明显。 Socket.io is simple to work with but requires a man-in-the-middle server whereas WebRTC (Web Real-Time Communications) is directly peer-to-peer in the browser and a lot harder to work with. Socket.io使用起来很简单,但是需要一个中间人服务器,而WebRTC(Web实时通信)在浏览器中是直接对等的,使用起来更加困难。 WebRTC is fairly new and isn't available in older browser versions, so I'd suggest going with Socket.io or similar in this case. WebRTC是相当新的,并且在较旧的浏览器版本中不可用,因此在这种情况下,建议使用Socket.io或类似版本。

Hopefully you get a better overview on the possibilities you have. 希望您对所拥有的可能性有更好的了解。 Either way you need to catch the data somewhere between the two clients (as shown in your client-server example) 无论哪种方式,您都需要在两个客户端之间的某个地方捕获数据(如您的客户端-服务器示例所示)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM