Node.js listens on UDP and forwards connected http clients

I am new to node.js, so forgive ignorance if it's easy.

I want to configure a simple node.js http server that the web client connects to. I also want the node.js server to act as a UDP listener on a separate port where it will receive useful JSON data from another application. I want the node.js server to immediately redirect this useful JSON data to one or more connected web clients.

I got this far from the initial search:

  • 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');
        }
    );
    
  • Initialize the 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);
    
  • Use socket.io to establish a direct connection between the 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 ...

EDIT: ,

+5
1

: http://runnable.com/UXsar5hEezgaAACJ

loopback → 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();
+10

All Articles