public function testUnframeMatchesPreFraming() { $string = 'Hello World!'; $framed = $this->version->newFrame($string)->getContents(); $frame = new Frame(); $frame->addBuffer($framed); $this->assertEquals($string, $frame->getPayload()); }
public function send($msg) { if (!$msg instanceof DataInterface) { $msg = new Frame($msg); } $this->getConnection()->send($msg->getContents()); return $this; }
/** * {@inheritdoc} */ public function sendBinary($msg) { if (!$this->WebSocket->closing) { if (!$msg instanceof DataInterface) { $msg = new Frame($msg, true, Frame::OP_BINARY); } $this->getConnection()->send($msg->getContents()); } return $this; }
public function onPong(Frame $frame) { $seq = $frame->getPayload(); if (isset($this->pingRequests[$seq]) && isset($this->pingRequests[$seq]['deferred'])) { $this->pingRequests[$seq]['deferred']->resolve($seq); /** @var TimerInterface $timer */ $timer = $this->pingRequests[$seq]['timer']; $timer->cancel(); unset($this->pingRequests[$seq]); } }
public function onPong(Frame $frame) { $seq = $frame->getPayload(); if (isset($this->pingRequests[$seq]) && isset($this->pingRequests[$seq]['deferred'])) { $this->pingRequests[$seq]['deferred']->resolve(); /** @var TimerInterface $timer */ $timer = $this->pingRequests[$seq]['timer']; $timer->cancel(); unset($this->pingRequests[$seq]); } // all sequence numbers before this one are probably no good anymore // and actually are probably errors }
/** * {@inheritdoc} */ public function close($code = 1000) { if ($code instanceof DataInterface) { $this->send($code); } else { $this->send(new Frame(Frame::encode(sprintf('%016b', $code)), true, Frame::OP_CLOSE)); } $this->getConnection()->close(); }
public function close($code = 1000) { $frame = new Frame(pack('n', $code), true, Frame::OP_CLOSE); $this->_stream->write($frame->getContents()); $this->_stream->end(); }
/** * @override */ public function send($msg) { if (!$this->WebSocket->closing) { if (!$msg instanceof DataInterface) { $msg = new Frame($msg); } $this->connection->send($msg->getContents()); } return $this; }
/** * Parse received data * * @param $response */ private function parseData($response) { if (!$this->connected && isset($response['Sec-Websocket-Accept'])) { if (base64_encode(pack('H*', sha1($this->key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'))) === $response['Sec-Websocket-Accept']) { $this->connected = true; } } if ($this->connected && !empty($response['content'])) { $content = trim($response['content']); $frame = new Frame(); $frame->addBuffer($content); $content = $frame->getPayload(); $content = utf8_encode($content); $data = json_decode($content, true); if (json_last_error() === JSON_ERROR_NONE) { unset($response['status']); unset($response['content']); $this->receiveData($data, $response); } else { echo 'JSON decode error [#' . json_last_error() . ']'; } $overflow = $frame->extractOverflow(); if ($overflow) { $this->parseData(array('content' => $overflow)); } } }
public function testGetUnBufferedPayloadLength() { $this->message->addFrame(Frame::create('The quick brow', false, Frame::OP_TEXT))->addFrame(Frame::create('n fox jumps ov', false, Frame::OP_CONTINUE)); $this->assertEquals(28, $this->message->getPayloadLength()); }
public function testUnMaskPayload() { $string = $this->generateRandomString(); $frame = Frame::create($string)->maskPayload()->unMaskPayload(); $this->assertFalse($frame->isMasked()); $this->assertEquals($string, $frame->getPayload()); }
/** * There was a frame boundary issue when the first 3 bytes of a frame with a payload greater than * 126 was added to the frame buffer and then Frame::getPayloadLength was called. It would cause the frame * to set the payload length to 126 and then not recalculate it once the full length information was available. * * This is fixed by setting the defPayLen back to -1 before the underflow exception is thrown. * * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength * @covers Ratchet\WebSocket\Version\RFC6455\Frame::extractOverflow */ public function testFrameDeliveredOneByteAtATime() { $startHeader = "~"; // header for a text frame of 256 - non-final $framePayload = str_repeat("*", 256); $rawOverflow = "xyz"; $rawFrame = $startHeader . $framePayload . $rawOverflow; $frame = new Frame(); $payloadLen = 256; for ($i = 0; $i < strlen($rawFrame); $i++) { $frame->addBuffer($rawFrame[$i]); try { // payloadLen will $payloadLen = $frame->getPayloadLength(); } catch (\UnderflowException $e) { if ($i > 2) { // we should get an underflow on 0,1,2 $this->fail("Underflow exception when the frame length should be available"); } } if ($payloadLen !== 256) { $this->fail("Payload length of " . $payloadLen . " should have been 256."); } } // make sure the overflow is good $this->assertEquals($rawOverflow, $frame->extractOverflow()); }