我是node.js的新手,所以如果这很简单,请原谅我的无知。
我想做的是设置一个简单的node.js http服务器,其中一个web客户端连接。我还希望node.js服务器在单独的端口上充当UDP侦听器,在此端口上它将接收来自其他应用程序的JSON有效负载。我希望node.js服务器然后将这些JSON有效负载立即转发到一个或多个连接的web客户端。
我得到了这么远的一些最初的谷歌周围:
-
创建一个简单的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'); } );
-
在单独的端口上初始化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);
-
使用套接字。在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); } ); });
我想我的问题是我不知道如何桥接2和3,得到接收到的UDP数据包广播到连接的套接字。io客户。或者也许有一种更简单、更优雅的方法?我找到了socket的文档。缺少IO…
编辑:感谢修复代码格式的人
我做了一个运行示例供您使用:http://runnable.com/UXsar5hEezgaAACJ
现在它只是一个环回客户端->套接字。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();