shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

How to make it work for old code? #69

Closed OpenJarvisAI closed 6 months ago

OpenJarvisAI commented 9 months ago
image

for this very basic one, got error, how to resolve ?

shamblett commented 9 months ago

Sorry but without any more context as to what you are doing code snippets likle this mean nothing. It looka as though some of your imorts are wrong but seeing as you dont show any of this its impossible to know.

OpenJarvisAI commented 9 months ago

@shamblett Hi,

I migrate mqtt_client to mqtt5_client, some APIs are changed, but since all code are based on yoour work, I think you must known the old interface right?

the unsubscribe has been changed into unsubscribtTopicString and I edit it. Correct me if am wrong.

How now, there seems not MqtttClietnPayloadBuilder any longer,

how to send the bits now?

 @override
  void sendPayload(String payload, String channel) {
    final builder = MqttClientPayloadBuilder();
    builder.addUTF8String(payload);
    kLog("sending payload client? $_client");
    if (_client != null) {
      _client!.publishMessage(channel, MqttQos.atLeastOnce, builder.payload!);
    } else {
      // do with disconnected
    }
  }

this is how old does.

vindolin commented 9 months ago

MqttClientPayloadBuilder was renamed to MqttPayloadBuilder.

I ported one project to the Mqtt5 version and had to change that to make it work again.

shamblett commented 9 months ago

Yes, there are small differences between the clients, please read the API docs fully in cases like you describe above.

lucasjinreal commented 9 months ago

Hello, I recently upgrade Matt_client to mqtt5_client, also can not connect to server.

On my side, the server didn't changed, I forget previous using which protocol version, but my server supports version 5.1

I don't know why it can not handshake now.

Can anyone help?

2023-11-19T14:53:35.564+0800    ERROR   server/client.go:273    connection lost {"client_id": "", "remote_addr": "120.229.159.63:6830", "error": "operation error: Code = 81, reasonString: "}
github.com/DrmagicE/gmqtt/server.(*client).setError.func1
        /root/go/pkg/mod/github.com/!drmagic!e/gmqtt@v0.4.1/server/client.go:273
sync.(*Once).doSlow
        /root/go/src/sync/once.go:74
sync.(*Once).Do
        /root/go/src/sync/once.go:65
github.com/DrmagicE/gmqtt/server.(*client).setError
        /root/go/pkg/mod/github.com/!drmagic!e/gmqtt@v0.4.1/server/client.go:271
github.com/DrmagicE/gmqtt/server.(*client).readLoop.func1
        /root/go/pkg/mod/github.com/!drmagic!e/gmqtt@v0.4.1/server/client.go:393
github.com/DrmagicE/gmqtt/server.(*client).readLoop
        /root/go/pkg/mod/github.com/!drmagic!e/gmqtt@v0.4.1/server/client.go:408
github.com/DrmagicE/gmqtt/server.(*client).serve.func1
        /root/go/pkg/mod/github.com/!drmagic!e/gmqtt@v0.4.1/server/client.go:1451
2023-11-19T14:53:40.590+0800    ERROR   server/client.go:273    connection lost {"client_id": "", "remote_addr": "120.229.159.63:6833", "error": "operation error: Code = 81, reasonString: "}
lucasjinreal commented 9 months ago

this my flutter client log:

flutter: 2023-11-19 14:53:40.476217 -- MqttSynchronousServerConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false
flutter: 2023-11-19 14:53:40.476564 -- MqttSynchronousServerConnectionHandler::internalConnect - insecure TCP selected
flutter: 2023-11-19 14:53:40.476955 -- MqttSynchronousServerConnectionHandler::internalConnect - calling connect
flutter: 2023-11-19 14:53:40.477287 -- MqttNormalConnection::connect- entered
flutter: 2023-11-19 14:53:40.501105 -- MqttServerConnection::_startListening
flutter: 2023-11-19 14:53:40.501650 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete
flutter: 2023-11-19 14:53:40.501848 -- MqttSynchronousServerConnectionHandler::internalConnect sending connect message
flutter: 2023-11-19 14:53:40.502009 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.connect
MessageType = MqttMessageType.connect Duplicate = false Retain = false Qos = atMostOnce Size = 63
ProtocolName = MQTT
ProtocolVersion = 5
ConnectFlags = CleanStart=false, WillFlag=false, WillQos=atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true
KeepAlive = 0
Properties = No properties setWill topic = lastwills
User name = friday
Password = 1195889656
flutter: 2023-11-19 14:53:40.502701 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
flutter: 2023-11-19 14:53:40.502922 -- MqttSynchronousServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
flutter: 2023-11-19 14:53:40.515040 -- MqttConnectionBase::_onDone - calling disconnected callback
flutter: 2023-11-19 14:53:40.515295 -- MqttClient::internalDisconnect - not invoking disconnect, no connection handler
flutter: 2023-11-19 14:53:45.505468 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
flutter: 2023-11-19 14:53:45.506232 -- MqttSynchronousServerConnectionHandler::internalConnect failed
flutter: [KLOG] Exception: mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message (Missing Connection Acknowledgement?
shamblett commented 9 months ago

I don't know where your first log is from but its not the client, its showing -

2023-11-19T14:53:40.590+0800    ERROR   server/client.go:273    connection lost {"client_id": "", "remote_addr": "120.229.159.63:6833", "error": "operation error: Code = 81, reasonString: "}

You need to look up what operation error 81 means in your go code. The client log is still showing you are not receiving a connect acknowledgement message same as before.

What are you setting your username and password to? If you are not using them just use connect(), i.e. don't set them to '' and try and use them.

lucasjinreal commented 9 months ago

thanks. I changed into a new golang mutt broker, and now it works.

But still, I found the android client will drop the connection while iOS does not which is very weird.

shamblett commented 9 months ago

Yes, this is something to do with the Dart/flutter implementation on these platforms, not the client.

lucasjinreal commented 9 months ago

hi, I got very weird from server, seems there is something I missed config with mqtt5client.

keepAlivePeriod

flutter: 2023-11-19 21:00:39.307910 -- MqttServerConnection::_ondata - added incoming data message stream length is 8, message stream position is 0
flutter: 2023-11-19 21:00:39.307973 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 32
flutter: 2023-11-19 21:00:39.308141 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.connectAck
MessageType = MqttMessageType.connectAck Duplicate = false Retain = false Qos = atMostOnce Size = 6
Session Present = true
Connect Reason Code = success
Session Expiry Interval = 0
Receive Maximum = 1024
Maximum QoS = 2
Retain Available = false
Maximum Packet Size = 0
Assigned client Identifier = null
Topic Alias Maximum = 0
Reason String = null
Wildcard Subscription Available = true
Subscription Identifiers Available = true
Shared Subscription Available = true
broker Keep Alive = 0
Response Information = null
broker Reference = null
Authentication Method = null
Properties = Identifier : receiveMaximum, value : 1024

why the broker Keep Alive = 0 is 0 here? its too small

So my log is connected and then quickly disconnected:

MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
Message Identifier = 12
Subscription identifier = 0
Properties = No properties set
Topic = messages/ada15c74-8af6-4336-8a9e-1ce3d98af343, Option = Maximum Qos = atLeastOnce
No Local = false
Retain As Published = true
Retain Handling = sendRetained
flutter: 2023-11-19 21:00:39.303290 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
flutter: [KLOG] [JION ROOM] !!!! ******** join roomId: messages/ada15c74-8af6-4336-8a9e-1ce3d98af343
flutter: 2023-11-19 21:00:39.303436 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
Message Identifier = 13
Subscription identifier = 0
Properties = No properties set
Topic = events/ada15c74-8af6-4336-8a9e-1ce3d98af343, Option = Maximum Qos = atLeastOnce
No Local = false
Retain As Published = true
Retain Handling = sendRetained
flutter: 2023-11-19 21:00:39.303596 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
flutter: [KLOG] [JION ROOM] !!!! ******** join roomId: events/ada15c74-8af6-4336-8a9e-1ce3d98af343
flutter: 2023-11-19 21:00:39.305762 -- MqttConnectionBase::_onDone - calling disconnected callback
flutter: [KLOG] !!! got disconnected!! [Disconnected] Disconnected!!
flutter: [AppData] conn.. ConnectionState.disconnected false
flutter: 2023-11-19 21:00:39.307721 -- MqttServerConnection::_onData - Message Received Started <<<
flutter: 2023-11-19 21:00:39.307812 -- MqttServerConnection::_ondata - adding incoming data, data length is 8, message stream length is 0, message stream position is 0
flutter: 2023-11-19 21:00:39.307910 -- MqttServerConnection::_ondata - added incoming data message stream length is 8, message stream position is 0
flutter: 2023-11-19 21:00:39.307973 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 32
flutter: 2023-11-19 21:00:39.308141 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.connectAck
MessageType = MqttMessageType.connectAck Duplicate = false Retain = false Qos = atMostOnce Size = 6
Session Present = true
Connect Reason Code = success
Session Expiry Interval = 0
Receive Maximum = 1024
Maximum QoS = 2
Retain Available = false
Maximum Packet Size = 0
Assigned client Identifier = null
Topic Alias Maximum = 0
Reason String = null
Wildcard Subscription Available = true
Subscription Identifiers Available = true
Shared Subscription Available = true
broker Keep Alive = 0
Response Information = null
broker Reference = null
Authentication Method = null
Properties = Identifier : receiveMaximum, value : 1024
flutter: 2023-11-19 21:00:39.308278 -- MqttServerConnection::_onData - message available event fired
flutter: 2023-11-19 21:00:39.308348 -- MqttServerConnection::_onData - Message Received Ended <<<
flutter: 2023-11-19 21:00:39.308445 -- MqttConnectionHandlerBase::_connectAckProcessor
flutter: 2023-11-19 21:00:39.308522 -- MqttConnectionHandlerBase::_connectAckProcessor - state = connected
flutter: [KLOG] [onConnected] Connected, is this connected? null
flutter: [KLOG] [broadcast status] client is null, disconnected...
flutter: 2023-11-19 21:00:39.308697 -- MqttConnectionHandlerBase:: cancelling connect timer
flutter: [AppData] conn.. ConnectionState.disconnected false
flutter: 2023-11-19 21:00:39.308918 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is connected with return code of success and a disconnection origin of none
flutter: 2023-11-19 21:00:39.308992 -- MqttSynchronousServerConnectionHandler::internalConnect exited with state Connection status is connected with return code of success and a disconnection origin of none
flutter: [KLOG] [broadcast status] client is null, disconnected...
shamblett commented 9 months ago

From the MQTT5 spec -

Keep Alive value of 0 has the effect of turning off the Keep Alive mechanism. If Keep Alive is 0 the Client is not obliged to send MQTT Control Packets on any particular schedule.

When received from the broker. So this should be having no effect, why did you thinlk it did?

If you need to know the meanings of any of these flags or behaviour of the MQTT5 protocol please consult the spec.

lucasjinreal commented 9 months ago

Hi, i am not very famillar about mqtt protocol spec, but from my normal understanding, the client should keepAlive by default with some seconds value I think? I have set the keepAlive params, but seems server didn't take it.

So when connected, imediately disconnected.

shamblett commented 9 months ago

No, you dont need to use the keepalive mechanism in MQTT, please resd the spec. If you want to use kleepalive then read the clients API docs in for this area.

I dont know what you mean by 'server didnt talke it'. Your only recourse here is to examine your broker logs to see why it is disconneting the client. The client cant tell you this.

Try your testing witj a mosquito broker, you can find a url for.one in the examples.

OpenJarvisAI commented 9 months ago

@shamblett Hi, I got this from server:

time=2023-11-20T22:05:39.822+08:00 level=INFO msg=unsubscribed hook=my-custom-events-hook client=android_client_iDxic_usrItug3Lj2c5 filters=[]
time=2023-11-20T22:05:39.822+08:00 level=WARN msg="" listener=t1 error=EOF
time=2023-11-20T22:05:42.144+08:00 level=WARN msg="client keepalive is below minimum recommended value and may exhibit connection instability" client="" keepalive=0 recommended=5
time=2023-11-20T22:05:42.144+08:00 level=INFO msg="--> client connected" hook=my-custom-events-hook client=android_client_iDxic_usrItug3Lj2c5
time=2023-11-20T22:05:42.147+08:00 level=WARN msg="client keepalive is below minimum recommended value and may exhibit connection instability" client="" keepalive=0 recommended=5
time=2023-11-20T22:05:42.147+08:00 level=INFO msg="--> client connected" hook=my-custom-events-hook client=android_client_iDxic_usrItug3Lj2c5
time=2023-11-20T22:05:42.147+08:00 level=INFO msg="--> client disconnected" hook=my-custom-events-hook client=android_client_iDxic_usrItug3Lj2c5 expire=true error="read tcp 172.18.171.245:1883->113.89.11.200:20164: use of closed network connection"
time=2023-11-20T22:05:42.147+08:00 level=WARN msg="" listener=t1 error="read tcp 172.18.171.245:1883->113.89.11.200:20164: use of closed network connection"

So, how to tell is server disconnected client or vice verse?

shamblett commented 9 months ago

In the log above I can see 2 client connected events followed by a disconnect. Why are you connecting twice?

lucasjinreal commented 9 months ago

@shamblett No, it's a reconnect action I think, disconnenct and client will auto try reconnect.

shamblett commented 9 months ago

Yes, but why did it disconnect in the first place? It shows 2 connects before the disconnect at almost the same time with the same client id, the disconnect came later, please explain why you are connecting twice.

time=2023-11-20T22:05:42.144+08:00 level=INFO msg="--> client connected" hook=my-custom-events-hook client=android_client_iDxic_usrItug3Lj2c5
time=2023-11-20T22:05:42.147+08:00 level=WARN msg="client keepalive is below minimum recommended value and may exhibit connection instability" client="" keepalive=0 recommended=5
time=2023-11-20T22:05:42.147+08:00 level=INFO msg="--> client connected" hook=my-custom-events-hook client=android_client_iDxic_usrItug3Lj2c5
lucasjinreal commented 9 months ago

Hello, sir, I think you were right, there seems 2 connected and then a disconnect.

But from client side, how to make sure there isn't 2 connect happen, it really hard since only can judge by if _client == null, there might be a race trace in somewhere. Is there a way to make it just connect once?

shamblett commented 9 months ago

How you handle your code is up to you, checking for a null client is not the way to do this, check the connection status of the client, if it is connected do not try and reconnect until you have disconnected.

lucasjinreal commented 9 months ago

Hi, I have double check my code, there still can not found 2 times connect happen.

Still might be somewhere wrong with the lib, I also saw other people got same problems which still don't know why

shamblett commented 9 months ago

Theres nothing wrong with the lib thousands of people use this and they are not reporting double connections.

If you think its a lib fault you are free to look at the code yourself and try and find it.

lucasjinreal commented 9 months ago

Hi, can u tell me why it might be connects twice, even I just have one single place did the connect?

shamblett commented 9 months ago

Becaise its being called twice in quickk succesion by the lookld of it. Update your code to add a simple counter that is incremented when connect is called see what value yo get.

vindolin commented 9 months ago

Hi, can u tell me why it might be connects twice, even I just have one single place did the connect?

Can you provide a "Minimal, Reproducible Example" of your code?