Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dotNET Platform
MQTTnet
提交
14eb2028
MQTTnet
项目概览
dotNET Platform
/
MQTTnet
12 个月 前同步成功
通知
0
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MQTTnet
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
14eb2028
编写于
6月 18, 2022
作者:
C
Christian
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add new samples.
上级
a3c7f9a2
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
128 addition
and
27 deletion
+128
-27
Samples/Client/Client_Connection_Samples.cs
Samples/Client/Client_Connection_Samples.cs
+55
-7
Samples/Program.cs
Samples/Program.cs
+9
-3
Samples/Server/Server_Simple_Samples.cs
Samples/Server/Server_Simple_Samples.cs
+64
-17
未找到文件。
Samples/Client/Client_Connection_Samples.cs
浏览文件 @
14eb2028
...
...
@@ -111,13 +111,21 @@ public static class Client_Connection_Samples
.
WithTls
(
o
=>
{
// The used public broker sometimes has invalid certificates. This sample accepts all
// certificates. This should not be used in live environments.
o
.
CertificateValidationHandler
=
_
=>
true
;
// The default value is determined by the OS. Set manually to force version.
o
.
SslProtocol
=
SslProtocols
.
Tls12
;
})
.
Build
();
await
mqttClient
.
ConnectAsync
(
mqttClientOptions
,
CancellationToken
.
None
);
using
(
var
timeout
=
new
CancellationTokenSource
(
5000
))
{
await
mqttClient
.
ConnectAsync
(
mqttClientOptions
,
timeout
.
Token
);
Console
.
WriteLine
(
"The MQTT client is connected."
);
Console
.
WriteLine
(
"The MQTT client is connected."
);
}
}
}
...
...
@@ -159,19 +167,59 @@ public static class Client_Connection_Samples
.
WithTls
(
o
=>
{
o
.
SslProtocol
=
SslProtocols
.
Tls12
;
// The default value is determined by the OS. Set manually to force version.
// The used public broker sometimes has invalid certificates. This sample accepts all
// certificates. This should not be used in live environments.
o
.
CertificateValidationHandler
=
_
=>
true
;
})
.
Build
();
// In MQTTv5 the response contains much more information.
var
response
=
await
mqttClient
.
ConnectAsync
(
mqttClientOptions
,
CancellationToken
.
None
);
Console
.
WriteLine
(
"The MQTT client is connected."
);
using
(
var
timeout
=
new
CancellationTokenSource
(
5000
))
{
var
response
=
await
mqttClient
.
ConnectAsync
(
mqttClientOptions
,
timeout
.
Token
);
Console
.
WriteLine
(
"The MQTT client is connected."
);
response
.
DumpToConsole
();
response
.
DumpToConsole
();
}
}
}
public
static
async
Task
Inspect_Certificate_Validation_Errors
()
{
/*
* This sample prints the certificate information while connection. This data can be used to decide whether a connection is secure or not
* including the reason for that status.
*/
var
mqttFactory
=
new
MqttFactory
();
using
(
var
mqttClient
=
mqttFactory
.
CreateMqttClient
())
{
var
mqttClientOptions
=
new
MqttClientOptionsBuilder
().
WithTcpServer
(
"mqtt.fluux.io"
,
8883
)
.
WithTls
(
o
=>
{
o
.
CertificateValidationHandler
=
eventArgs
=>
{
eventArgs
.
Certificate
.
Subject
.
DumpToConsole
();
eventArgs
.
Certificate
.
GetExpirationDateString
().
DumpToConsole
();
eventArgs
.
Chain
.
ChainPolicy
.
RevocationMode
.
DumpToConsole
();
eventArgs
.
Chain
.
ChainStatus
.
DumpToConsole
();
eventArgs
.
SslPolicyErrors
.
DumpToConsole
();
return
true
;
};
})
.
Build
();
// In MQTTv5 the response contains much more information.
using
(
var
timeout
=
new
CancellationTokenSource
(
5000
))
{
await
mqttClient
.
ConnectAsync
(
mqttClientOptions
,
timeout
.
Token
);
}
}
}
public
static
async
Task
Ping_Server
()
{
/*
...
...
Samples/Program.cs
浏览文件 @
14eb2028
...
...
@@ -39,10 +39,16 @@ Console.WriteLine();
try
{
var
task
=
selectedSampleMethod
.
Invoke
(
null
,
null
)
as
Task
;
task
?.
Wait
();
if
(
selectedSampleMethod
.
Invoke
(
null
,
null
)
is
Task
task
)
{
await
task
;
}
}
catch
(
Exception
exception
)
{
Console
.
WriteLine
(
exception
.
ToString
());
}
\ No newline at end of file
}
Console
.
WriteLine
();
Console
.
WriteLine
(
"Press Enter to exit."
);
Console
.
ReadLine
();
\ No newline at end of file
Samples/Server/Server_Simple_Samples.cs
浏览文件 @
14eb2028
...
...
@@ -9,27 +9,66 @@ namespace MQTTnet.Samples.Server;
public
static
class
Server_Simple_Samples
{
public
static
async
Task
Force_Disconnecting_Client
()
{
/*
* This sample will disconnect a client.
*
* See _Run_Minimal_Server_ for more information.
*/
using
(
var
mqttServer
=
await
StartMqttServer
())
{
// Let the client connect.
await
Task
.
Delay
(
TimeSpan
.
FromSeconds
(
5
));
// Now disconnect the client (if connected).
var
affectedClient
=
(
await
mqttServer
.
GetClientsAsync
()).
FirstOrDefault
(
c
=>
c
.
Id
==
"MyClient"
);
if
(
affectedClient
!=
null
)
{
await
affectedClient
.
DisconnectAsync
();
}
}
}
public
static
async
Task
Publish_Message_From_Broker
()
{
/*
* This sample will publish a message directly at the broker.
*
* See _Run_Minimal_Server_ for more information.
*/
using
(
var
mqttServer
=
await
StartMqttServer
())
{
// Create a new message using the builder as usual.
var
message
=
new
MqttApplicationMessageBuilder
().
WithTopic
(
"HelloWorld"
).
WithPayload
(
"Test"
).
Build
();
// Now inject the new message at the broker.
await
mqttServer
.
InjectApplicationMessage
(
new
InjectedMqttApplicationMessage
(
message
)
{
SenderClientId
=
"SenderClientId"
});
}
}
public
static
async
Task
Run_Minimal_Server
()
{
/*
* This sample starts a simple MQTT server which will accept any TCP connection.
*/
var
mqttFactory
=
new
MqttFactory
();
// The port for the default endpoint is 1883.
// The default endpoint is NOT encrypted!
// Use the builder classes where possible.
var
mqttServerOptions
=
new
MqttServerOptionsBuilder
()
.
WithDefaultEndpoint
()
.
Build
();
var
mqttServerOptions
=
new
MqttServerOptionsBuilder
().
WithDefaultEndpoint
().
Build
();
// The port can be changed using the following API (not used in this example).
new
MqttServerOptionsBuilder
()
.
WithDefaultEndpoint
()
.
WithDefaultEndpointPort
(
1234
)
.
Build
();
new
MqttServerOptionsBuilder
().
WithDefaultEndpoint
().
WithDefaultEndpointPort
(
1234
).
Build
();
using
(
var
mqttServer
=
mqttFactory
.
CreateMqttServer
(
mqttServerOptions
))
{
await
mqttServer
.
StartAsync
();
...
...
@@ -50,13 +89,7 @@ public static class Server_Simple_Samples
* See _Run_Minimal_Server_ for more information.
*/
var
mqttFactory
=
new
MqttFactory
();
var
mqttServerOptions
=
new
MqttServerOptionsBuilder
()
.
WithDefaultEndpoint
()
.
Build
();
using
(
var
mqttServer
=
mqttFactory
.
CreateMqttServer
(
mqttServerOptions
))
using
(
var
mqttServer
=
await
StartMqttServer
())
{
// Setup connection validation before starting the server so that there is
// no change to connect without valid credentials.
...
...
@@ -88,4 +121,18 @@ public static class Server_Simple_Samples
await
mqttServer
.
StopAsync
();
}
}
static
async
Task
<
MqttServer
>
StartMqttServer
()
{
var
mqttFactory
=
new
MqttFactory
();
// Due to security reasons the "default" endpoint (which is unencrypted) is not enabled by default!
var
mqttServerOptions
=
mqttFactory
.
CreateServerOptionsBuilder
().
WithDefaultEndpoint
().
Build
();
var
server
=
mqttFactory
.
CreateMqttServer
(
mqttServerOptions
);
await
server
.
StartAsync
();
return
server
;
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录