node.js 侦听 UDP 并转发到连接的 http Web 客户端 [英] node.js listen for UDP and forward to connected http web clients

查看:74
本文介绍了node.js 侦听 UDP 并转发到连接的 http Web 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 node.js 的新手,所以如果这很简单,请原谅我的无知.

I'm new to node.js, so forgive the ignorance if this is simple.

我想要做的是设置一个简单的 node.js http 服务器,一个 web 客户端连接到它.我还希望 node.js 服务器在单独的端口上充当 UDP 侦听器,在该端口上它将从其他应用程序接收 JSON 有效负载.我希望 node.js 服务器立即将这些 JSON 有效负载转发到一个或多个连接的网络客户端.

What I want to do is setup a simple node.js http server to which a web-client connects. I also want the node.js server to act as a UDP listener on a separate port, on which it will receive JSON payloads from some other application. I want the node.js server to then forward these JSON payloads immediately to one or more of the connected web-clients.

我从最初的谷歌搜索中得到了这么多:

I got this far from some initial googling around:

  1. 创建一个简单的 node.js http 服务器,以静态 html 页面进行响应:

  1. Create a simple node.js http server that responds with a static html page:

//Initialize the HTTP server on port 8080, serve the index.html page
var server = http.createServer(function(req, res) {
    res.writeHead(200, { 
        'Content-type': 'text/html'});
        res.end(fs.readFileSync(__dirname + '/index.html'));
    }).listen(8080, function() {
        console.log('Listening at: 127.0.0.1 8080');
    }
);

  • 在单独的端口上初始化 UDP 服务器:

  • Initialize a UDP server on a separate port:

    //Initialize a UDP server to listen for json payloads on port 3333
    var srv = dgram.createSocket("udp4");
    srv.on("message", function (msg, rinfo) {
      console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
      io.sockets.broadcast.emit('message', 'test');
      //stream.write(msg);
      //socket.broadcast.emit('message',msg);
    });
    
    srv.on("listening", function () {
      var address = srv.address();
      console.log("server listening " + address.address + ":" + address.port);
    });
    
    srv.bind(5555);
    

  • 使用 socket.io 在 Web 客户端和服务器之间建立实时连接:

  • Use socket.io to establish a live connection between web-client and server:

    //this listens for socket messages from the client and broadcasts to all other clients
    var io = require('socket.io').listen(server);
    io.sockets.on('connection', function (socket) {
        socket.on('message', function (msg) {
          console.log('Message Received: ', msg.data.skeletons[0] ? msg.data.skeletons[0].skeleton_id : '');
          socket.broadcast.emit('message', msg);
        }
      );
    });
    

  • 我想我的问题是我不知道如何桥接 2 和 3,以将接收到的 UDP 数据包广播到连接的 socket.io 客户端.或者也许有一种更简单、更优雅的方式来做到这一点?我发现缺少 socket.io 的文档...

    I guess my problem is I don't know how to bridge 2 and 3, to get the received UDP packets broadcasted to the connected socket.io clients. Or perhaps there's a simpler, more elegant way of doing this? I found the documentation for socket.io to be lacking...

    感谢修复代码格式的人

    推荐答案

    我为你做了一个运行示例:http://runnable.com/UXsar5hEezgaAACJ

    I made a running example for you to get going with: http://runnable.com/UXsar5hEezgaAACJ

    现在它只是一个环回客户端 -> socket.io -> udp 客户端 -> udp 服务器 -> socket.io -> 客户端.

    For now it's just a loopback client -> socket.io -> udp client -> udp server -> socket.io - > client.

    这是它的核心:

    var http = require('http');
    var fs = require('fs');
    var html = fs.readFileSync(__dirname + '/index.html');
    
    //Initialize the HTTP server on port 8080, serve the index.html page
    var server = http.createServer(function(req, res) {
      res.writeHead(200, { 
        'Content-type': 'text/html'
      });
      res.end(html);
    }).listen( process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP, function() {
      console.log('Listening');
    });
    
    var io = require('socket.io').listen(server);
    
    io.set('log level', 0);
    
    io.sockets.on('connection', function (socket) {
      socket.emit('message', 'connected');
      socket.on('message', function (data) {
        console.log(data);
        var address = srv.address();
        var client = dgram.createSocket("udp4");
        var message = new Buffer(data);
        client.send(message, 0, message.length, address.port, address.address, function(err, bytes) {
          client.close();
        });
      });
    });
    
    var dgram = require('dgram');
    
    //Initialize a UDP server to listen for json payloads on port 3333
    var srv = dgram.createSocket("udp4");
    srv.on("message", function (msg, rinfo) {
      console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
      io.sockets.emit('message', 'udp');
    });
    
    srv.on("listening", function () {
      var address = srv.address();
      console.log("server listening " + address.address + ":" + address.port);
    });
    
    srv.on('error', function (err) {
      console.error(err);
      process.exit(0);
    });
    
    srv.bind();
    

    这篇关于node.js 侦听 UDP 并转发到连接的 http Web 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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