What is the Node.js equivalent of the fgetc() function in php? And how would I apply it to a socket?
I’m working on a node.js port of this php script:
http://code.google.com/p/bf2php/source/browse/trunk/rcon/BF2RConBase.class.php
Basically it uses sockets to connect to Battlefield 2 based game servers. The function I’m looking at is:
protected function read($bare = false) {
$delim = $bare ? "\n" : "\x04";
for($buffer = ''; ($char = fgetc($this->socket)) != $delim; $buffer .= $char);
return trim($buffer);
}
Its supposed to grab the first line directly from the socket (from what I gather) one character at a time up til the ‘\n’. I’m assuming the output is used for grabbing an encryption salt. The function is called in the socket connect event as part of the code that generates the encrypted password needed to login. Can anyone show me what a Node.js equivalent of this function might look like?
The docs have an excellent example of how to connect to a server over the network.
Simply change the
dataevent handler to buffer incoming data until you’ve recieved the information you want.To do that, you’ll want to know how to use a
Buffer.Here’s a concrete example of how to buffer data from a stream and parse out messages delimited by a specific character. I notice in the linked PHP that the protocol you’re trying to implement delimts messages with a EOT (0x04) character.
Completely untested, so there’s likely errors. The main thing to gather here is that as data arrives, you buffer it in memory. Once you find a delimiter character in your buffer, you can process the message.