什么是php';s fgetc()用于Node.js套接字';s

什么是php';s fgetc()用于Node.js套接字';s,php,node.js,Php,Node.js,php中fgetc()函数的Node.js等价物是什么?我如何将其应用于插座 我正在使用此php脚本的node.js端口: 基本上,它使用套接字连接到基于战地2的游戏服务器。我看到的功能是: protected function read($bare = false) { $delim = $bare ? "\n" : "\x04"; for($buffer = ''; ($char = fgetc($this->socket)) != $delim; $buffer .

php中fgetc()函数的Node.js等价物是什么?我如何将其应用于插座

我正在使用此php脚本的node.js端口:

基本上,它使用套接字连接到基于战地2的游戏服务器。我看到的功能是:

protected function read($bare = false) {
    $delim = $bare ? "\n" : "\x04";
    for($buffer = ''; ($char = fgetc($this->socket)) != $delim; $buffer .= $char);
    return trim($buffer);
}
它应该一次直接从套接字(从我收集的内容)抓取一个字符的第一行,直到“\n”。我假设输出用于获取加密盐。该函数在socket connect事件中被调用,作为生成登录所需加密密码的代码的一部分。有人能告诉我这个函数的Node.js等价物是什么样子的吗?

有一个很好的例子说明如何通过网络连接到服务器

var net = require('net');
var client = net.connect({port: 8124},
    function() { //'connect' listener
  console.log('client connected');
  client.write('world!\r\n');
});
client.on('data', function(data) {
  console.log(data.toString());
  client.end();
});
client.on('end', function() {
  console.log('client disconnected');
});
只需更改
数据
事件处理程序,以缓冲传入的数据,直到收到所需的信息

要做到这一点,您需要知道如何使用


下面是一个具体的示例,说明如何缓冲流中的数据并解析出由特定字符分隔的消息。我注意到,在链接的PHP中,您尝试使用EOT(0x04)字符实现delimts消息的协议

var net = require('net');


var max = 1024 * 1024 // 1 MB, the maximum amount of data that we will buffer (prevent a bad server from crashing us by filling up RAM)
    , allocate = 4096; // how much memory to allocate at once, 4 kB (there's no point in wasting 1 MB of RAM to buffer a few bytes)
    , buffer=new Buffer(allocate) // create a new buffer that allocates 4 kB to start
    , nread=0 // how many bytes we've buffered so far
    , nproc=0 // how many bytes in the buffer we've processed (to avoid looping over the entire buffer every time data is received)
    , client = net.connect({host:'example.com', port: 8124}); // connect to the server

client.on('data', function(chunk) {
    if (nread + chunk.length > buffer.length) { // if the buffer is too small to hold the data
        var need = Math.min(chunk.length, allocate); // allocate at least 4kB
        if (nread + need > max) throw new Error('Buffer overflow'); // uh-oh, we're all full - TODO you'll want to handle this more gracefully

        var newbuf = new Buffer(buffer.length + need); // because Buffers can't be resized, we must allocate a new one
        buffer.copy(newbuf); // and copy the old one's data to the new one
        buffer = newbuf; // the old, small buffer will be garbage collected
    }

    chunk.copy(buffer, nread); // copy the received chunk of data into the buffer
    nread += chunk.length; // add this chunk's length to the total number of bytes buffered

    pump(); // look at the buffer to see if we've received enough data to act
});

client.on('end', function() {
    // handle disconnect
});


client.on('error', function(err) {
    // handle errors
});


function find(byte) { // look for a specific byte in the buffer
    for (var i = nproc; i < nread; i++) { // look through the buffer, starting from where we left off last time
        if (buffer.readUInt8(i, true) == byte) { // we've found one
            return i;
        }
    }
}
function slice(bytes) { // discard bytes from the beginning of a buffer
    buffer = buffer.slice(bytes); // slice off the bytes
    nread -= bytes; // note that we've removed bytes
    nproc = 0; // and reset the processed bytes counter
}

function pump() {
    var pos; // position of a EOT character

    while ((pos = find(0x04)) >= 0) { // keep going while there's a EOT (0x04) somewhere in the buffer
        if (pos == 0) { // if there's more than one EOT in a row, the buffer will now start with a EOT
            slice(1); // discard it
            continue; // so that the next iteration will start with data
        }
        process(buffer.slice(0,pos)); // hand off the message
        slice(pos+1); // and slice the processed data off the buffer
    }
}

function process(msg) { // here's where we do something with a message
    if (msg.length > 0) { // ignore empty messages
        // here's where you have to decide what to do with the data you've received
        // experiment with the protocol
    }
}
var net=require('net');
var max=1024*1024//1 MB,我们将缓冲的最大数据量(通过填充RAM防止坏服务器崩溃)
,分配=4096;//一次分配多少内存,4KB(浪费1MB内存来缓冲几个字节是没有意义的)
,buffer=new buffer(allocate)//创建一个新的缓冲区,该缓冲区分配4 kB来启动
,nread=0//到目前为止我们缓冲了多少字节
,nproc=0//我们在缓冲区中处理了多少字节(以避免每次接收数据时在整个缓冲区上循环)
,client=net.connect({host:'example.com',port:8124});//连接到服务器
client.on('data',函数(块){
if(nread+chunk.length>buffer.length){//如果缓冲区太小,无法容纳数据
var need=Math.min(chunk.length,allocate);//分配至少4kB
如果(nread+need>max)抛出新错误(‘缓冲区溢出’);//哦,我们都满了-要做的事你会想更优雅地处理这个问题
var newbuf=newbuffer(Buffer.length+need);//由于无法调整缓冲区的大小,我们必须分配一个新的缓冲区
copy(newbuf);//并将旧数据复制到新数据
buffer=newbuf;//旧的、小的缓冲区将被垃圾收集
}
copy(buffer,nread);//将接收到的数据块复制到缓冲区中
nread+=chunk.length;//将此块的长度添加到缓冲的总字节数中
pump();//查看缓冲区,看看是否已收到足够的数据来执行操作
});
client.on('end',function()){
//手柄断开
});
client.on('error',函数(err){
//处理错误
});
函数find(byte){//在缓冲区中查找特定字节
对于(var i=nproc;i=0){//当缓冲区中某处有EOT(0x04)时,继续运行
如果(pos==0){//如果一行中有多个EOT,缓冲区现在将以一个EOT开始
切片(1);//丢弃它
continue;//以便下一次迭代将从数据开始
}
进程(buffer.slice(0,pos));//传递消息
切片(pos+1);//并将处理后的数据从缓冲区中切片
}
}
函数进程(msg){//这里我们用消息做一些事情
如果(msg.length>0){//忽略空消息
//在这里,您必须决定如何处理收到的数据
//实验协议
}
}

完全未经测试,因此可能存在错误。这里要收集的主要内容是,当数据到达时,将其缓冲在内存中。一旦在缓冲区中找到分隔符,您就可以处理该消息。

当您进行移植时,您可能会发现这很有帮助:谢谢您的回复。我甚至不知道我明白那是什么意思。没有太多的文档。但再次感谢!phpjs是javascript中的所有php函数。将该库复制到nodejs。fgetc端口可能有您可能要查找的代码。这有点超过三个角,但可能会有帮助。您还可以直接将PHP代码移植到js,因为您在js.Buffer中拥有所有PHP函数!好啊这是一个很好的开始。我实际上是在看溪流,想弄明白。缓冲器在某处掠过我的视线,但没有粘住。我会仔细看看。谢谢另一个感谢您的快速回复:)@user1287536:是的,这是一个超级类,包括网络连接、文件读取器、加密和(反)压缩。他们的
数据
事件(通常)发出
缓冲区
s。也来看看。在这个答案中,我正在缓冲gzip的输出,但它与缓冲网络流非常相似。这是有意义的。我看到你的缓冲区代码在那里。当然,这给了我一个如何将其应用于代码的想法。谢谢事实上,我可能会回到原点。我正在检查“data”事件中返回的数据,除了常规文本之外,我什么也看不到。没有任何东西像盐或盐。我所看到的缓冲区只是一个存储所有数据块的地方。我需要以某种方式从流本身获取一条信息。php中的fgetc()函数读取流,而不是缓冲区……或者我可能只是有点困惑