Comments (6)
@chkr1011 : Great, I allready added a handler (= simple callback for topics) and RPC experimental feature for my needs. Every topic level is processed by a dispatcher. Maybe you can use some ideas of my MqttLib.
Server side sample (registration of RPC stubs) :
var rootDispatcher = new MqttDispatcher("smarthome");
mqttServer.ApplicationMessageReceived += rootDispatcher.OnMessageReceived;
rootDispatcher.AddHandler("command", async(frag, msg) => {
string value = Encoding.UTF8.GetString(msg.Payload);
return await devices.Command(frag[2], frag[3], frag[4], value);
}, MqttQualityOfServiceLevel.AtLeastOnce);
var rpcDispatcher = new MqttRpcDispatcher(mqttServer, "rpcrequest", new MqttRpcJsonSerializer(), logger);
rootDispatcher.AddDispatcher(rpcDispatcher);
rpcDispatcher.AddFunction<SensorCommand,bool>("testfunc", TestFunc);
rpcDispatcher.AddFunction<SensorCommand,bool>("command", async(cmd) => {
return await devices.Command(cmd.DeviceName, cmd.SensorName, cmd.Name, cmd.Value);
});
rpcDispatcher.AddFunction<string, string>("ping", (cmd) => { return Task.FromResult(cmd); });
static Task<bool> TestFunc (SensorCommand cmd)
{
return Task.FromResult(true);
}
Client side sample :
_mqttClient = new MqttClient(new MqttClientAdapterFactory(), new MqttLoggerEx(logger));
_rootDispatcher = new MqttClientDispatcher("smarthome");
_mqttClient.ApplicationMessageReceived += _rootDispatcher.OnMessageReceived;
_rootDispatcher.AddHandler("sensortype", ProcessSensorType);
_rootDispatcher.AddHandler("sensor", ProcessSensor, subscribeAllSensors ?
MqttQualityOfServiceLevel.AtLeastOnce : (MqttQualityOfServiceLevel?)null);
_rootDispatcher.AddHandler("event", ProcessEvent, MqttQualityOfServiceLevel.AtLeastOnce);
_mqttRpc = new MqttClientRpc(_mqttClient, "rpcreply",null, _serializer,
MqttQualityOfServiceLevel.AtLeastOnce, _logger);
_rootDispatcher.AddDispatcher(_mqttRpc);
public async Task<bool> Command<T>(string deviceName, string sensorName, string name, T value)
{
SensorCommand cmd = new SensorCommand { DeviceName = deviceName, SensorName = sensorName, Name = name, Value = value.ToString() };
bool rc = await _mqttRpc.Call<bool, SensorCommand>("command", cmd);
return rc;
}
public async Task<string> Ping()
{
string str = await _mqttRpc.Call<string, string>("ping", "1234");
return str;
}
from mqttnet.
im using a dictionary with int to taskCompletionSource similar to the MqttPacketDispatcher so i can send request with an id and wait for the response with the same id. that way i get call semantic.
By the way what is the essential difference between SignalR and the MQTT AspNet Core integration ?
signalr uses websockets or some other transport, a HubProtocol and Groups to filter who receives what. im planning to do a lib that glues this library and signalr together once signalr leaves alpha. im not yet shure how to emulate hubprotocol. im thinking of either send all messages to one hub or dispatch them according to their topic. for me the main advantage of signalr is it provides a more fleshed out programming model and scaleout, here we just have an eventhandler
from mqttnet.
I had also such an idea. Also creating a standalone MQTT server based on this library running in Windows IoT Core or on a PC. Like a product and also available at the store. My idea was to make the config all via MQTT messages. So that the server will have one admin credential with default values and then you can use messages for writing configuration etc. But finally I had one security flaw because everyone can subscribe to the RPC-Response-Messages and also the RPC-Request messages and thus sniff these messages. Does someone of you have an idea to fix this? One idea is to deny subscriptions to those message types if the client is not a client which uses the Admin credentials.
from mqttnet.
deny subscribe based on credentials and only allow with tls is all you can do i guess. mqtt isnt really build with security in mind anyone can subscribe and publish anything (with default mechanics)
from mqttnet.
As long as there is no progress I will close this ticket. I still have this on my plate but due to several open security flaws I would not implement it in this library and on a higher level instead.
from mqttnet.
I reopened this ticket because this topic is now relevant for me as well 😄
My idea is to add a new project (and also nuget) to this repository called "MQTTnet.Extensions" which contains some implementations like the storage for the server which is based on JSON etc. So that in a UWP app only 5 lines are required to start a fully featured MQTT server.
Another topic for this lib is the RPC feature. I usually use "PubSubClient" for my ESP8266 code so that the solution must be usable with that library as well. My ideas are as follows:
- The extension provides an async method like "ExecuteAsync" which takes a custom payload (byte[] at low level but overloads can use JSON.net for serialization of real classes for request and response).
- When called, the extension will create a topic like "$RPC/[clientId]/[custom method name/[guid]" which can be subscribed from the client like "$RPC/[clientId]/#".
- The server already allows denying of subscriptions. This means that the server will only allow subscribing to "own" RPC topics (ClientId is in topic).
- Same for the response. Nobody can subscribe to a response topic.
- The client will read everything from the payload and perform an operation. Then the client will send the reply with the exact same topic to the server.
- The server only accepts the response from the same client (truncated if different one).
- Like the PacketAwaiter a waiting TaskCompletionSource will be set (identified by the guid in the topic) with the response.
- Timeouts etc. can be specified for the TaskCompletionSource.
- No changes for MQTTnet core are required because the extension only uses already available features.
@ChristianRiedl What do you think?
from mqttnet.
Related Issues (20)
- Issue with Establishing Secure WebSocket Connection to AWS IoT MQTT Server on .NET Framework 4.x
- Requesting the last data from mqtt broker HOT 1
- mqttnet is missing NuGet package README file
- mqttnet.extensions.managedclient is missing NuGet package README file HOT 1
- Get the ClientID of Sender client HOT 1
- How do i authorize whit Username and Password? HOT 1
- Trying to run this sample code(https://github.com/dotnet/MQTTnet) from visual studio 2022 but getting many errors HOT 1
- Not able to run the sample code HOT 2
- Not able to establish tls connection with azure event grid mqtt server HOT 2
- 链接频繁断开【异常断线触发重连】错误信息:Not allowed to connect while connect/disconnect is pending.
- How do you specify the protocol version which MqttServer supports? HOT 1
- Missing assemblies when building the solution HOT 1
- Gg HOT 1
- Triggering continues disconnection in MQTT server HOT 1
- Security alerts on MQTTnet.AspNetCore HOT 1
- First connection attempt of ManagedClient is always failing
- Auto-reconnect documenation and samples are a bit confusing
- 订阅高并发topic场景会存在内存泄漏 HOT 4
- 客户端收到的消息不是最新的 The message received by the client is not the latest HOT 3
- I am trying MQTTNet managed client in of my POC but its very slow.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mqttnet.