将 node.js 中的 UDP 套接字从应用程序传输到 HTTP [英] Transfer UDP socket in node.js from Application to HTTP

查看:24
本文介绍了将 node.js 中的 UDP 套接字从应用程序传输到 HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 NodeJS 将来自应用程序的 Socket 传输到 http?

Is it possible to transfer a Socket coming from a application to http via NodeJS?

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

I send my socket with a application (in c++) in UDP or TCP(if impossible in UDP...) to NodeJS.

我的 NodeJS 脚本:

My script from 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);

到目前为止,该功能已完成,但是例如如何将我的套接字传输到 Socket.io?

Up to now does that function, but then how to transfer my socket to Socket.io for example?

我想将套接字发送到 Socket.io(例如)以将套接字传输到 HTTP.例如,通过使用这样的函数,但无需重新建立与 socket.io 的连接:

I would like to send the socket to Socket.io (for example) for transfer the socket to HTTP. By using a function like this for example, but without renew a establishing a connection to socket.io :

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

感谢您的帮助.

++ 米特拉.

推荐答案

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

Here's a complete example with a socket.io server, a web server sending out a very simple page (it will just log all messages to console) and an UDP socket listening for messages, passing them to all connected clients:

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 的房间"功能:io.sockets.of(someRoom).emit.在 Socket.IO 的连接处理程序中,您可以将每个客户端 join 到某个房间.

Update: As io.sockets.emit shows, all messages received on the UDP port 7788 are sent to all connected clients. If you want to route them based on some data in the message or similar, you could use Socket.IO's "room" feature: io.sockets.of(someRoom).emit. In the connection handler for Socket.IO, you can join each client to some room.

这篇关于将 node.js 中的 UDP 套接字从应用程序传输到 HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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