PHPDaemon\Servers\WebSocket\Protocols\V13::sendFrame PHP Method

sendFrame() public method

Sends a frame.
public sendFrame ( string $data, string $type = null, callable $cb = null ) : boolean
$data string Frame's data.
$type string Frame's type. ("STRING" OR "BINARY")
$cb callable Optional. Callback called when the frame is received by client.
return boolean Success.
    public function sendFrame($data, $type = null, $cb = null)
    {
        if (!$this->handshaked) {
            return false;
        }
        if ($this->finished && $type !== 'CONNCLOSE') {
            return false;
        }
        /*if (in_array($type, ['STRING', 'BINARY']) && ($this->outgoingCompression > 0) && in_array('deflate-frame', $this->extensions)) {
              //$data = gzcompress($data, $this->outgoingCompression);
              //$rsv1 = 1;
          }*/
        $fin = 1;
        $rsv1 = 0;
        $rsv2 = 0;
        $rsv3 = 0;
        $this->write(chr(bindec($fin . $rsv1 . $rsv2 . $rsv3 . str_pad(decbin($this->getFrameType($type)), 4, '0', STR_PAD_LEFT))));
        $dataLength = mb_orig_strlen($data);
        $isMasked = false;
        $isMaskedInt = $isMasked ? 128 : 0;
        if ($dataLength <= 125) {
            $this->write(chr($dataLength + $isMaskedInt));
        } elseif ($dataLength <= 65535) {
            $this->write(chr(126 + $isMaskedInt) . chr($dataLength >> 8) . chr($dataLength & 0xff));
        } else {
            $this->write(chr(127 + $isMaskedInt) . chr($dataLength >> 56) . chr($dataLength >> 48) . chr($dataLength >> 40) . chr($dataLength >> 32) . chr($dataLength >> 24) . chr($dataLength >> 16) . chr($dataLength >> 8) . chr($dataLength & 0xff));
        }
        if ($isMasked) {
            $mask = chr(mt_rand(0, 0xff)) . chr(mt_rand(0, 0xff)) . chr(mt_rand(0, 0xff)) . chr(mt_rand(0, 0xff));
            $this->write($mask . $this->mask($data, $mask));
        } else {
            $this->write($data);
        }
        if ($cb !== null) {
            $this->onWriteOnce($cb);
        }
        return true;
    }