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

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

问题描述

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

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

我使用UDP或TCP的应用程序(用c ++)发送我的套接字(如果在UDP中不可能...)到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);
});

感谢您的帮助。

++ Metra。

推荐答案

这是一个带有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的连接处理程序中,您可以加入将每个客户端加入某个房间。

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套接字从Application传输到HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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