[英]Combination of Node.JS(+ Socket.io) and PHP, what about users and changing Socket ID's
[英]What is the equivalent of php's fgetc() for Node.js socket's?
什么是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等價物是什么樣的嗎?
這些文檔有一個很好的例子,說明如何通過網絡連接到服務器。
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中,您嘗試實現的協議使用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
}
}
完全未經測試,因此可能存在錯誤。 這里收集的主要內容是,當數據到達時,您將其緩沖在內存中。 在緩沖區中找到分隔符后,您可以處理該消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.