public function testUnframeMatchesPreFraming()
 {
     $string = 'Hello World!';
     $framed = $this->version->newFrame($string)->getContents();
     $frame = new Frame();
     $frame->addBuffer($framed);
     $this->assertEquals($string, $frame->getPayload());
 }
示例#2
0
 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]);
     }
 }
示例#3
0
 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
 }
示例#4
0
 /**
  * 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));
         }
     }
 }
示例#5
0
 public function testExtractOverflow()
 {
     $string1 = $this->generateRandomString();
     $frame1 = Frame::create($string1);
     $string2 = $this->generateRandomString();
     $frame2 = Frame::create($string2);
     $cat = new Frame();
     $cat->addBuffer($frame1->getContents() . $frame2->getContents());
     $this->assertEquals($frame1->getContents(), $cat->getContents());
     $this->assertEquals($string1, $cat->getPayload());
     $uncat = new Frame();
     $uncat->addBuffer($cat->extractOverflow());
     $this->assertEquals($string1, $cat->getPayload());
     $this->assertEquals($string2, $uncat->getPayload());
 }
示例#6
0
 /**
  * @covers Ratchet\WebSocket\Version\RFC6455\Frame::unMaskPayload
  */
 public function testUnMaskPayload()
 {
     $string = $this->generateRandomString();
     $frame = new Frame($string);
     $frame->maskPayload()->unMaskPayload();
     $this->assertFalse($frame->isMasked());
     $this->assertEquals($string, $frame->getPayload());
 }