public function testSendAndReceiveByteMessage() { $frame = new Frame(); $frame->setCommand('testing'); $frame->setHeader('testing', 1); $frame->setBody('hello ' . Frame::END_OF_FRAME . ' world'); $client = new Client(); $client->addConnection('tcp', 'localhost', '11232', '\\ZendQueueTest\\Stomp\\Mock'); $client->send($frame); $this->assertTrue($client->canRead()); $test = $client->receive(); $this->assertEquals('testing', $test->getCommand()); $this->assertEquals(1, $test->getHeader('testing')); $this->assertEquals('hello ' . Frame::END_OF_FRAME . ' world', $test->getBody()); }
/** * Reads in a frame from the socket or returns false. * * @return \ZendQueue\Stomp\StompFrame|false * @throws \ZendQueue\Exception */ public function read() { $this->ping(); $response = ''; // as per protocol COMMAND is 1st \n terminated then headers also \n terminated // COMMAND and header block are seperated by a blank line. // read command and headers while (($line = @fgets($this->_socket)) !== false) { $response .= $line; if (rtrim($line) === '') { break; } } $this->_checkSocketReadTimeout(); // to differenciate between a byte message and // non-byte message, check content-length header $headers = Frame::extractHeaders($response); if (!isset($headers[Frame::CONTENT_LENGTH])) { // read till we hit the end of frame marker do { $chunk = @fgets($this->_socket); if ($chunk === false || strlen($chunk) === 0) { $this->_checkSocketReadTimeout(); break; } if (substr($chunk, -2) === Frame::END_OF_FRAME) { // add the chunk above to the result before returning $response .= $chunk; break; } $response .= $chunk; } while (feof($this->_socket) === false); } else { // we have a content-length header set $contentLength = $headers[Frame::CONTENT_LENGTH] + 2; $current_pos = ftell($this->_socket); $chunk = ''; for ($read_to = $current_pos + $contentLength; $read_to > $current_pos; $current_pos = ftell($this->_socket)) { $chunk = fread($this->_socket, $read_to - $current_pos); if ($chunk === false || strlen($chunk) === 0) { $this->_checkSocketReadTimeout(); break; } $response .= $chunk; // Break if the connection ended prematurely if (feof($this->_socket)) { break; } } } if ($response === '') { return false; } // we already have headers, prevent extracting the headers again $frame = $this->createFrame(); $frame->setCommand(Frame::extractCommand($response))->setHeaders($headers)->setBody(Frame::extractBody($response)); return $frame; }
public function testByteMessageAutoContentLengthOn() { $frame = new Frame(); $frame->setCommand('TESTING'); $frame->setAutoContentLength(true); $frame->setBody('hello ' . Frame::END_OF_FRAME . ' world'); $connection = new ConnectionSocketOverload(); $connection->setSocket($this->_socket); $connection->write($frame); rewind($this->_socket); $test = $connection->read(); $this->assertEquals('hello ' . Frame::END_OF_FRAME . ' world', $test->getBody()); }