gpt4 book ai didi

php - Node.js 套接字的 php 的 fgetc() 等价物是什么?

转载 作者:搜寻专家 更新时间:2023-10-31 21:39:31 24 4
gpt4 key购买 nike

PHP 中的 fgetc() 函数在 Node.js 中的等效项是什么?我如何将它应用于套接字?

我正在处理此 php 脚本的 node.js 端口: http://code.google.com/p/bf2php/source/browse/trunk/rcon/BF2RConBase.class.php

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

protected function read($bare = false) {
$delim = $bare ? "\n" : "\x04";
for($buffer = ''; ($char = fgetc($this->socket)) != $delim; $buffer .= $char);
return trim($buffer);
}

它应该直接从套接字(从我收集的)中获取第一行,一次一个字符,直到'\n'。我假设输出用于获取加密盐。该函数在套接字连接事件中被调用,作为生成登录所需的加密密码的代码的一部分。谁能告诉我这个函数的 Node.js 等价物是什么样子的?

最佳答案

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

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');
});

只需更改 data 事件处理程序以缓冲传入数据,直到您收到所需的信息。

为此,您需要了解如何使用 Buffer .


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

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
}
}

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

关于php - Node.js 套接字的 php 的 fgetc() 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12789183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com