mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-30 10:42:48 +08:00
Add several new options to the WebSocket options
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
* [Client] Added a first version of a managed client which will manage the connection, subscription etc. automatically (Thanks to @JTrotta)
|
* [Client] Added a first version of a managed client which will manage the connection, subscription etc. automatically (Thanks to @JTrotta)
|
||||||
* [Client] The session state response from the server is now returned in the _ConnectAsync_ method and also part of the _Connected_ event args
|
* [Client] The session state response from the server is now returned in the _ConnectAsync_ method and also part of the _Connected_ event args
|
||||||
* [Client] Added a _TopicFilterBuilder_ using a fluent API (Namespace Changes!)
|
* [Client] Added a _TopicFilterBuilder_ using a fluent API (Namespace Changes!)
|
||||||
|
* [Client] Added several new options for the WebSocket channel (Thanks to @ChristianRiedl)
|
||||||
* [Server] Added support for WebSockets via ASP.NET Core 2.0 (Thanks to @ChristianRiedl)
|
* [Server] Added support for WebSockets via ASP.NET Core 2.0 (Thanks to @ChristianRiedl)
|
||||||
* [Server] Added support for a custom application message interceptor
|
* [Server] Added support for a custom application message interceptor
|
||||||
</releaseNotes>
|
</releaseNotes>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using MQTTnet.Core.Client;
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -32,6 +33,37 @@ namespace MQTTnet.Implementations
|
|||||||
|
|
||||||
_webSocket = new ClientWebSocket();
|
_webSocket = new ClientWebSocket();
|
||||||
_webSocket.Options.KeepAliveInterval = _options.KeepAlivePeriod;
|
_webSocket.Options.KeepAliveInterval = _options.KeepAlivePeriod;
|
||||||
|
|
||||||
|
if (_options.RequestHeaders != null)
|
||||||
|
{
|
||||||
|
foreach (var requestHeader in _options.RequestHeaders)
|
||||||
|
{
|
||||||
|
_webSocket.Options.SetRequestHeader(requestHeader.Key, requestHeader.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_options.SubProtocols != null)
|
||||||
|
{
|
||||||
|
foreach (var subProtocol in _options.SubProtocols)
|
||||||
|
{
|
||||||
|
_webSocket.Options.AddSubProtocol(subProtocol);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_options.CookieContainer != null)
|
||||||
|
{
|
||||||
|
_webSocket.Options.Cookies = _options.CookieContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_options.TlsOptions?.UseTls == true && _options.TlsOptions?.Certificates != null)
|
||||||
|
{
|
||||||
|
_webSocket.Options.ClientCertificates = new X509CertificateCollection();
|
||||||
|
foreach (var certificate in _options.TlsOptions.Certificates)
|
||||||
|
{
|
||||||
|
_webSocket.Options.ClientCertificates.Add(new X509Certificate(certificate));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
|
await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
|
||||||
RawReceiveStream = new WebSocketStream(_webSocket);
|
RawReceiveStream = new WebSocketStream(_webSocket);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using MQTTnet.Core.Client;
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -32,6 +33,37 @@ namespace MQTTnet.Implementations
|
|||||||
|
|
||||||
_webSocket = new ClientWebSocket();
|
_webSocket = new ClientWebSocket();
|
||||||
_webSocket.Options.KeepAliveInterval = _options.KeepAlivePeriod;
|
_webSocket.Options.KeepAliveInterval = _options.KeepAlivePeriod;
|
||||||
|
|
||||||
|
if (_options.RequestHeaders != null)
|
||||||
|
{
|
||||||
|
foreach (var requestHeader in _options.RequestHeaders)
|
||||||
|
{
|
||||||
|
_webSocket.Options.SetRequestHeader(requestHeader.Key, requestHeader.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_options.SubProtocols != null)
|
||||||
|
{
|
||||||
|
foreach (var subProtocol in _options.SubProtocols)
|
||||||
|
{
|
||||||
|
_webSocket.Options.AddSubProtocol(subProtocol);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_options.CookieContainer != null)
|
||||||
|
{
|
||||||
|
_webSocket.Options.Cookies = _options.CookieContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_options.TlsOptions?.UseTls == true && _options.TlsOptions?.Certificates != null)
|
||||||
|
{
|
||||||
|
_webSocket.Options.ClientCertificates = new X509CertificateCollection();
|
||||||
|
foreach (var certificate in _options.TlsOptions.Certificates)
|
||||||
|
{
|
||||||
|
_webSocket.Options.ClientCertificates.Add(new X509Certificate(certificate));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None).ConfigureAwait(false);
|
await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None).ConfigureAwait(false);
|
||||||
|
|
||||||
RawReceiveStream = new WebSocketStream(_webSocket);
|
RawReceiveStream = new WebSocketStream(_webSocket);
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
namespace MQTTnet.Core.Client
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace MQTTnet.Core.Client
|
||||||
{
|
{
|
||||||
public class MqttClientWebSocketOptions : BaseMqttClientOptions
|
public class MqttClientWebSocketOptions : BaseMqttClientOptions
|
||||||
{
|
{
|
||||||
public string Uri { get; set; }
|
public string Uri { get; set; }
|
||||||
|
|
||||||
|
public IDictionary<string, string> RequestHeaders { get; set; }
|
||||||
|
|
||||||
|
public ICollection<string> SubProtocols { get; set; }
|
||||||
|
|
||||||
|
public CookieContainer CookieContainer { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user