简体   繁体   English

连接到Node.js中的udp套接字

[英]Connecting to a udp socket in Node.js

I'm writing a javascript/node.js program that receives the scores from a HL/Team Fortress 2 server, but I can't seem to receive the udp packets send by the game server. 我正在编写一个javascript / node.js程序,该程序从HL / Team Fortress 2服务器接收分数,但似乎无法收到游戏服务器发送的udp数据包。 (I can receive udp packets transmitted by a UDP test tool) I have found a python library that works but it uses socket.connect() before receiving data from the server. (我可以接收由UDP测试工具传输的udp数据包)我发现了一个可以运行的python库,但是在从服务器接收数据之前,它使用socket.connect()。

Python snipplet (asyncore used): Python代码片段(使用异步):

self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
self.bind(('0.0.0.0', 17015))
self.connect((IP of server, 27015))

data = self.recv(1400)

But in node.js I can't seem to connect to a remote address. 但是在node.js中,我似乎无法连接到远程地址。

My code so far: 到目前为止,我的代码:

var dgram = require('dgram')
var server = dgram.createSocket("udp4");
server.on('message', function (data, rinfo) {
    data = data.toString();
    if (data.startsWith('\xff\xff\xff\xff') && data.endsWith('\n\x00')) {
        console.log(data);
        Logparser(data);
    } else {
        console.log(data);
    }
});
server.bind(17015);

UDP packet captured with wireshark http://pastebin.com/W7i9CV2u Wireshark http://pastebin.com/W7i9CV2u捕获的UDP数据包

You need to use the addMembership() method to join a multicast group, in UDP you don't connect to servers, a connection is never created and there is no guarantee that you will receive any/all packets. 您需要使用addMembership()方法加入多播组,在UDP中,您不连接服务器,从不创建连接,并且不能保证您会收到任何/所有数据包。 You join a multicast group and receive packets with that group-id that reach your network card, the python method name is misleading. 您加入多播组并接收到达您的网卡的具有该组ID的数据包,则python方法名称具有误导性。

http://nodejs.org/api/dgram.html#dgram_socket_addmembership_multicastaddress_multicastinterface http://nodejs.org/api/dgram.html#dgram_socket_addmembership_multicastaddress_multicastinterface

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM