Closed danielstock closed 4 years ago
You need to store that information on your own. For example fill a dictionary whenever a client connects. Or you can access the client status without your message received handler.
You can set the ClientId in the server options. This is the ID which is used for server messages (null by default).
thanks @chkr1011
mqttServer.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate ... i could only find ClientId in the MqttServerClientConnectedEventArgs param.
how to set the ClientId in the server options?
optionsBuilder = new MqttServerOptionsBuilder()
.WithConnectionBacklog(100)
.WithClientId("client001")...
the ClientId is still null in the ApplicationMessageReceivedHandler event
mqttServer.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate ... i could only find ClientId in the MqttServerClientConnectedEventArgs param.
Check out https://github.com/SeppPenner/NetCoreMQTTExampleJsonConfig. In Program.cs, you can find code that reads all properties. (e.g. search for LogMessage
).
Back to the question, you can get the following properties:
Type | Properties | Link |
---|---|---|
Subscription | ClientId, TopicFilter, SessionItems, AcceptSubscription, CloseConnection | https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Server/MqttSubscriptionInterceptorContext.cs |
Publish | ClientId, ApplicationMessage (Check out https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/MqttApplicationMessage.cs), SessionItems, AcceptPublish, CloseConnection | https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Server/MqttApplicationMessageInterceptorContext.cs |
Client Connect | A lot of things, e.g. the endpoint (for IP addresses) and that kind of things. | https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Server/MqttConnectionValidatorContext.cs |
So: When a client connects, store your data in a dictionary (Key = clientId, value = whatever you need) --> When subscription / publish comes, you can access your dictionary in the handler method(s). That's what @chkr1011 already explained.
- how to set the ClientId in the server options? optionsBuilder = new MqttServerOptionsBuilder() .WithConnectionBacklog(100) .WithClientId("client001")... the ClientId is still null in the ApplicationMessageReceivedHandler event
Well, what do you want to achieve there? Do you want to set a client id for a connected client or for the server when it publishes anything?
E.g. each client has a unique client id, when you tell the server to use a client id, this will have no effect unless you publish a message from the server (Not from the client). So you need to set a clientId for each client on client connect. This is described under https://github.com/chkr1011/MQTTnet/wiki/Client#client-options.
Describe your question
i registered two event handlers for my mqttnet server:
for 1, how can i get more client information after the connection, such as ip address, the corresponding login userid... for 2, how can i tell, the message is published by connected client, not the server itself
thanks!
Which project is your question related to?