getClient(); $msgArr = []; if ( (empty($type)&&is_numeric($message)) || 'cash' === $type ) { $msgArr['cash'] = $message; $payId AND $msgArr['payid'] = $payId; } else { $msgArr['message'] = $message; } if (!empty($toClientId)) { $topic .= '/'.$toClientId; } $curClientId OR $curClientId = (string)rand(1,999999999); $success = $this->mqttClient->sendConnect($curClientId); if ($success) { $this->mqttClient->sendPublish($topic, json_encode($msgArr), MQTTClient::MQTT_QOS2); $this->mqttClient->sendDisconnect(); } $this->mqttClient->close(); } protected function getClient() { $this->mqttClient = new MQTTClient(env('MQTT_HOST'), env('MQTT_PORT')); $this->mqttClient->setAuthentication(env('MQTT_NAME'), env('MQTT_PASS')); if (env('MQTT_CERT')) { $this->mqttClient->setEncryption(env('MQTT_CERT')); } return $this->mqttClient; } }