php-mqtt/client

message lenth too long ,turns out Resource temporarily unavailable

luoxingit opened this issue · 1 comments

my change
new function

    /**
     * Send data
     *
     * @param string $packet
     * @param int    $packet_size
     * @return int
     */
    public function write($packet, $packet_size)
    {
        if (!$this->socket || !is_resource($this->socket)) return false;
        #add
        do { $packet = substr($packet, fwrite($this->socket, $packet)); } while (!empty($packet));

        If (!empty($packet)) {
            return false;
        } else {
            return $packet_size;
        }
        #endadd

        #disabled
        #return fwrite($this->socket, $packet, $packet_size);
    }
    
     protected function writeToSocket(string $data, ?int $length = null): void
    {

        $calculatedLength = strlen($data);
        $length           = min($length ?? $calculatedLength, $calculatedLength);
        if ($this->settings->shouldUseBlockingSocket()) {
            socket_set_blocking($this->socket, true);
        }
       //change to new function
       // $result = @fwrite($this->socket, $data, $length);
        $result = $this->write($data, $length);
        if ($this->settings->shouldUseBlockingSocket()) {
            socket_set_blocking($this->socket, false);
        }
        if ($result === false || $result !== $length) {
            $errorCode = swoole_errno();
            $errorMsg = swoole_strerror($errorCode);
            if ($errorCode !== 0) {
                echo "Socket error: [$errorCode] $errorMsg\n";
            }
            $this->logger->error('Sending data over the socket to the broker failed.');
            throw new DataTransferException(
                DataTransferException::EXCEPTION_TX_DATA,
                'Sending data over the socket failed. Has it been closed?'
            );
            $errorCode = socket_last_error($this->socket);
            $errorMsg = socket_strerror($errorCode);
            echo "Error writing to socket: $error_msg (code $errorCode)\n";
        }

        $this->bytesSent += $length;

        $this->logger->debug('Sent data over the socket: {data}', ['data' => $data]);

        // After writing successfully to the socket, the broker should have received a new message from us.
        // Because we only need to send a ping if no other messages are delivered, we can safely reset the ping timer.
        $this->lastPingAt = microtime(true);
    }

What exactly are you asking or suggesting? Please provide more details regarding your problem and setup