public function testParseFrameTransformsToFrameByDefault() { $body = 'var'; $msg = "CMD\nheader1:value1\n\n\n" . $body . ""; $parser = new Parser(); $parser->addData($msg); $parser->parse(); $result = $parser->getFrame(); $this->assertInstanceOf('\\Stomp\\Frame', $result); $this->assertEquals('var', $result->body); $this->assertEquals('value1', $result->headers['header1']); }
public function testParseFrameTransformsToFrameZeroByteContent() { $body = "varvar2"; $msg = "CMD\nheader1:value1\ncontent-length:" . strlen($body) . "\n\n" . $body . ""; $parser = new Parser(); $parser->addData($msg); $parser->parse(); $result = $parser->getFrame(); $this->assertInstanceOf('\\Stomp\\Frame', $result); $this->assertEquals($body, $result->body); $this->assertEquals('value1', $result->headers['header1']); }
/** * Read any newline left in the data to read. * * Newlines will not be added to the parser, if this method encounters a different character or result, * it'll add that to the parser's data buffer and abort. */ private function gobbleNewLines() { // Only test the stream, return immediately if nothing is left while ($this->connectionHasDataToRead(0, 0) && ($data = @fread($this->connection, 1)) !== false) { // If its not a newline, it indicates a new messages has been added, // so add that to the data-buffer of the parser. if ($data !== "\n" && $data !== "\r") { $this->parser->addData($data); break; } } }
/** * Try to read a frame from the server. * * @return Frame|false when no frame to read * @throws ConnectionException * @throws ErrorFrameException */ public function readFrame() { if ($this->parser->hasBufferedFrames()) { return $this->parser->getFrame(); } if (!$this->hasDataToRead()) { return false; } do { $read = @fread($this->connection, 1024); if ($read === false || $read === '') { throw new ConnectionException('Was not possible to read data from stream.', $this->activeHost); } $this->parser->addData($read); } while (!$this->parser->parse()); $frame = $this->parser->getFrame(); if ($frame->isErrorFrame()) { throw new ErrorFrameException($frame); } return $frame; }