将Node.js中的UDP套接字从应用程序传输到HTTP

是否有可能通过NodeJS将来自应用程序的套接字转移到http?

我发送套接字与应用程序(在c + +中)在UDP或TCP(如果不可能在UDP …)到NodeJS。

我从NodeJS的脚本:

var server = dgram.createSocket("udp4"); server.on("message", function (content, rinfo) { console.log("socket: " + content + " from " + rinfo.address + ":" + rinfo.port); }); server.on("listening", function () { }); server.bind(7788); 

到现在为止,这个function,但如何将我的套接字转移到Socket.io例如?

我想发送套接字到Socket.io(例如)将套接字转移到HTTP。 通过使用像这样的函数,但不更新与socket.iobuild立连接:

 io.sockets.on('connection', function (socket) { socket.emit(content); }); 

谢谢你的帮助。

++梅特拉。

下面是一个完整的例子,一个socket.io服务器,一个web服务器发送一个非常简单的页面(它只会将所有消息logging到控制台)和一个UDP套接字侦听消息,并将它们传递给所有连接的客户端:

 var http = require('http'), dgram = require('dgram'), socketio = require('socket.io'); var app = http.createServer(handleRequest), io = socketio.listen(app), socket = dgram.createSocket('udp4'); socket.on('message', function(content, rinfo) { console.log('got message', content, 'from', rinfo.address, rinfo.port); io.sockets.emit('udp message', content.toString()); }); function handleRequest(req, res) { res.writeHead(200, {'content-type': 'text/html'}); res.end("<!doctype html> \ <html><head> \ <script src='/socket.io/socket.io.js'></script> \ <script> \ var socket = io.connect('localhost', {port: 8000}); \ socket.on('udp message', function(message) { console.log(message) }); \ </script></head></html>"); } socket.bind(7788); app.listen(8000); 

更新:io.sockets.emit所示,UDP端口7788上收到的所有消息都发送给所有连接的客户端。 如果你想根据消息中的某些数据或类似的路由来发送它们,你可以使用Socket.IO的“房间”function: io.sockets.of(someRoom).emit 。 在Socket.IO的连接处理程序中,您可以join每个客户端连接到某个房间。