stomp-php icon indicating copy to clipboard operation
stomp-php copied to clipboard

Error receiving bytes messages

Open ghost opened this issue 11 years ago • 2 comments

I was having problems receiving bytes messages for use with googles / chobies protocolbuffers php implementation with this STOMP implementation. Sending to a Java client worked like a charm, but the return to PHP failed trying to deserialize the protocol buffer bytes. When executing $msg = $con->readFrame, it truncated the bytes by doing a trim on the message body removing all whitespace. The solution is the following new function for Stomp.php, whatch this line only stripping "\x00" instead of all whitespace :

$frame = new StompFrame($command, $headers, trim($body, "\x00"));

/** * Read response frame from server for binairy messages * * @return StompFrame False when no frame to read */ public function readFrameBinairy () { if (!$this->hasFrameToRead()) { return false; }
$rb = 1024; $data = ''; $end = false;
do { $read = fread($this->_socket, $rb); if ($read === false) { $this->_reconnect(); return $this->readFrame(); } $data .= $read; if (strpos($data, "\x00") !== false) { $end = true; $data = rtrim($data, "\n"); } $len = strlen($data); } while ($len < 2 || $end == false); list ($header, $body) = explode("\n\n", $data, 2); $header = explode("\n", $header); $headers = array(); $command = null; foreach ($header as $v) { if (isset($command)) { list ($name, $value) = explode(':', $v, 2); $headers[$name] = $value; } else { $command = $v; } } $frame = new StompFrame($command, $headers, trim($body, "\x00")); if (isset($frame->headers['transformation']) && $frame->headers['transformation'] == 'jms-map-json') { require_once 'Stomp/Map.php'; return new StompMessageMap($frame); } else { return $frame; } return $frame; }

Thanks to my colleague Jan Grävell for his excelent PHP knowlledge.

ghost avatar Aug 07 '14 09:08 ghost

I am having a similar problem. I cant vouch for the code above, but I am working with a feed that gzips the content of the messages. Then when the body is trimmed it breaks the gzip encoded strings. So it would be great if there was a way of telling it to not trim the body on some frames.

yanniboi avatar Apr 11 '16 18:04 yanniboi

@yanniboi @ghost please use the current version located at https://github.com/stomp-php/stomp-php

jmglsn avatar Apr 12 '16 09:04 jmglsn