Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException on connect #138

Open
beecomm-noam opened this issue May 4, 2023 · 0 comments
Open

InvalidCastException on connect #138

beecomm-noam opened this issue May 4, 2023 · 0 comments

Comments

@beecomm-noam
Copy link

Hi, i'm getting this weird exception when connecting to the broker:

Unable to cast object of type 'uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPingResp' to type 'uPLibrary.Networking.M2Mqtt.Messages.MqttMsgConnack'.

This one thrown after a connection failure, when trying to reconnect to the broker.
Here is my code:

private void Connect()
{
    try
    {
        Logger.Log(Level.Debug, "Client is trying to connect to the broker...");

        _client.Connect(
            $"adapter.{Settings.branchId}",
            Settings.brokerUsername,
            Settings.brokerPassword,
            false,
            MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
            true,
            $"adapters/{Settings.branchId}/status",
            JsonConvert.SerializeObject(new { status = "DISCONNECTED" }),
            false,
            5
        );

        if (_client.IsConnected)
        {
            Logger.Log(Level.Debug, "Client is connected, sending restoreOrders event...");
            _client.Publish($"restoreOrders/{Settings.branchId}", new byte[] { }, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
        }
        else
        {
            Logger.Log(Level.Debug, "Client is !!! disconnected");
        }
    }
    catch (Exception e)
    {
        Logger.Log(Level.Exception, $"Failed to reconnect Mqtt server: {e.Message}");
        Thread.Sleep(10_000);

        Logger.Log(Level.Exception, "Retrying connection...");
        Connect();
    }
}

Is something wrong in the connection function? or is it an internal error? i havn't seen anything that can throw cast exception in the source.. please help 🙏🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant