Wrench\Socket\Socket::send PHP Method

send() public method

public send ( string $data ) : boolean | integer
$data string Binary data to send down the socket
return boolean | integer The number of bytes sent or false on error
    public function send($data)
    {
        if (!$this->isConnected()) {
            throw new SocketException('Socket is not connected');
        }
        $length = strlen($data);
        if ($length == 0) {
            return 0;
        }
        for ($i = $length; $i > 0; $i -= $written) {
            $written = @fwrite($this->socket, substr($data, -1 * $i));
            if ($written === false) {
                return false;
            } elseif ($written === 0) {
                return false;
            }
        }
        return $length;
    }

Usage Example

示例#1
0
 /**
  * @param Socket $socket
  * @return boolean
  */
 public function sendToSocket(Socket $socket)
 {
     $success = true;
     foreach ($this->frames as $frame) {
         $success = $success && $socket->send($frame->getFrameBuffer()) !== false;
     }
     return $success;
 }