i am using MQTTCore plugin to establish and mqtt connection to my broker, but i have this error :
LogMQTTCore: Error: Error connecting socket to: 195.221.233.36:1883: SE_EISCONN, Connection state: Connected
Couldn’t connect to MQTT server: There was a socket error.
and log that :
LogTemp: Error: [MQTT] Connection failed. Code: 7
despite that i have test the borker in terminal with mosquitto_sub, and it work fine. here is the code i am usgin for the mqtt:
if (IMQTTCoreModule *MQTTModule = &FModuleManager::LoadModuleChecked(“MQTTCore”))
{
FMQTTURL URL;
URL.Host = TEXT("i put my server here");
URL.Port = 1883;
// Create MQTT client
MQTTClient = MQTTModule->GetOrCreateClient(URL, true); // force new client
if (MQTTClient.IsValid())
{
MQTTClient->OnConnect().AddLambda(\[this\](EMQTTConnectReturnCode ReturnCode)
{ OnMQTTConnected(ReturnCode); });
if (!MQTTClient->IsConnected())
{
MQTTClient->Connect(true);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("\[MQTT\] Client already connected"));
}
}
else
{
UE_LOG(LogTemp, Error, TEXT("Failed to create MQTT client"));
}
}
void OnMQTTConnected(EMQTTConnectReturnCode ReturnCode)
{
if (!MQTTClient.IsValid())
{
UE_LOG(LogTemp, Error, TEXT("\[MQTT\] Client is null"));
return;
}
if (ReturnCode == EMQTTConnectReturnCode::Accepted)
{
UE_LOG(LogTemp, Log, TEXT("\[MQTT\] Successfully connected"));
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("\[MQTT\] Connected"));
// Example: publish a message
FString Topic = TEXT("topic/hello");
FString Payload = TEXT("Hello from Unreal MQTT!");
MQTTClient->Publish(Topic, Payload, EMQTTQualityOfService::Once, false);
}
else
{
UE_LOG(LogTemp, Error, TEXT("\[MQTT\] Connection failed. Code: %d"), (int32)ReturnCode);
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("\[MQTT\] Connection failed"));
}
}