简体   繁体   English

node.js侦听UDP并转发到连接的http Web客户端

[英]node.js listen for UDP and forward to connected http web clients

I'm new to node.js, so forgive the ignorance if this is simple. 我是node.js的新手,如果这很简单,请原谅无知。

What I want to do is setup a simple node.js http server to which a web-client connects. 我想要做的是设置一个简单的node.js http服务器,web客户端连接到该服务器。 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. 我还希望node.js服务器在单独的端口上充当UDP侦听器,在该端口上它将从其他应用程序接收JSON有效负载。 I want the node.js server to then forward these JSON payloads immediately to one or more of the connected web-clients. 我希望node.js服务器立即将这些JSON有效负载转发到一个或多个连接的Web客户端。

I got this far from some initial googling around: 我从一些最初的谷歌搜索到这里远:

  1. Create a simple node.js http server that responds with a static html page: 创建一个简单的node.js http服务器,该服务器使用静态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(fs.readFileSync(__dirname + '/index.html')); }).listen(8080, function() { console.log('Listening at: 127.0.0.1 8080'); } ); 
  2. Initialize a UDP server on a separate port: 在单独的端口上初始化UDP服务器:

     //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); 
  3. Use socket.io to establish a live connection between web-client and server: 使用socket.io在Web客户端和服务器之间建立实时连接:

     //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); } ); }); 

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. 我想我的问题是我不知道如何桥接2和3,以便将收到的UDP数据包广播到连接的socket.io客户端。 Or perhaps there's a simpler, more elegant way of doing this? 或者也许有一种更简单,更优雅的方式呢? I found the documentation for socket.io to be lacking... 我发现socket.io的文档缺乏...

EDIT: thanks to the person that fixed the code formatting 编辑:感谢修复代码格式的人

I made a running example for you to get going with: http://runnable.com/UXsar5hEezgaAACJ 我为你做了一个运行的例子: http//runnable.com/UXsar5hEezgaAACJ

For now it's just a loopback client -> socket.io -> udp client -> udp server -> socket.io - > client. 现在它只是一个环回客户端 - > socket.io - > udp客户端 - > udp服务器 - > socket.io - >客户端。

here's the core of it: 这是它的核心:

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();

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

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