diff --git a/README.md b/README.md index 17dbb0e..3548bb6 100644 --- a/README.md +++ b/README.md @@ -142,13 +142,11 @@ $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings) // The password used for authentication when connecting to the broker. ->setPassword(null) - // Whether to use a blocking socket or not. By default, the socket is non-blocking, - // which is required when using subscriptions and/or {@see MqttClient::loop()}. - // In rare cases, it might be required to use a blocking socket though. One such example - // is when sending large messages (e.g. binaries) and the broker has a limited receive buffer. - // - // Note: When using a blocking socket, the MQTT client can get stuck if the socket is broken - // or when the broker does not consume the sent data fast enough. Use with caution. + // Whether to use a blocking socket when publishing messages or not. + // Normally, this setting can be ignored. When publishing large messages with multiple kilobytes in size, + // a blocking socket may be required if the receipt buffer of the broker is not large enough. + // + // Note: This setting has no effect on subscriptions, only on the publishing of messages. ->useBlockingSocket(false) // The connect timeout defines the maximum amount of seconds the client will try to establish diff --git a/src/ConnectionSettings.php b/src/ConnectionSettings.php index a448e16..b78ac1f 100644 --- a/src/ConnectionSettings.php +++ b/src/ConnectionSettings.php @@ -82,13 +82,11 @@ public function getPassword(): ?string } /** - * Whether to use a blocking socket or not. By default, the socket is non-blocking, - * which is required when using subscriptions and/or {@see MqttClient::loop()}. - * In rare cases, it might be required to use a blocking socket though. One such example - * is when sending large messages (e.g. binaries) and the broker has a limited receive buffer. + * Whether to use a blocking socket when publishing messages or not. + * Normally, this setting can be ignored. When publishing large messages with multiple kilobytes in size, + * a blocking socket may be required if the receipt buffer of the broker is not large enough. * - * Note: When using a blocking socket, the MQTT client can get stuck if the socket is broken - * or when the broker does not consume the sent data fast enough. Use with caution. + * Note: This setting has no effect on subscriptions, only on the publishing of messages. * * @param bool $useBlockingSocket * @return ConnectionSettings @@ -97,7 +95,7 @@ public function useBlockingSocket(bool $useBlockingSocket): ConnectionSettings { $copy = clone $this; - $copy->useBlockingModeForSocket = $useBlockingSocket; + $copy->useBlockingSocket = $useBlockingSocket; return $copy; } diff --git a/src/MqttClient.php b/src/MqttClient.php index 4134ad2..7cc0745 100644 --- a/src/MqttClient.php +++ b/src/MqttClient.php @@ -290,7 +290,7 @@ protected function establishSocketConnection(): void } stream_set_timeout($socket, $this->settings->getSocketTimeout()); - stream_set_blocking($socket, $this->settings->shouldUseBlockingSocket()); + stream_set_blocking($socket, false); $this->logger->debug('Socket opened and ready to use.'); @@ -1140,8 +1140,16 @@ 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); + } + $result = @fwrite($this->socket, $data, $length); + if ($this->settings->shouldUseBlockingSocket()) { + socket_set_blocking($this->socket, false); + } + if ($result === false || $result !== $length) { $this->logger->error('Sending data over the socket to the broker failed.'); throw new DataTransferException(