mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
39 lines
1.4 KiB
C#
39 lines
1.4 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using MQTTnet.Adapter;
|
|
using MQTTnet.AspNetCore.Client.Tcp;
|
|
using MQTTnet.Formatter;
|
|
using System;
|
|
using System.Net;
|
|
using MQTTnet.Client;
|
|
using MQTTnet.Diagnostics;
|
|
|
|
namespace MQTTnet.AspNetCore.Client
|
|
{
|
|
public sealed class MqttClientConnectionContextFactory : IMqttClientAdapterFactory
|
|
{
|
|
public IMqttChannelAdapter CreateClientAdapter(MqttClientOptions options, MqttPacketInspector packetInspector, IMqttNetLogger logger)
|
|
{
|
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
|
|
|
switch (options.ChannelOptions)
|
|
{
|
|
case MqttClientTcpOptions tcpOptions:
|
|
{
|
|
var endpoint = new DnsEndPoint(tcpOptions.Server, tcpOptions.GetPort());
|
|
var tcpConnection = new TcpConnection(endpoint);
|
|
|
|
var formatter = new MqttPacketFormatterAdapter(options.ProtocolVersion, new MqttBufferWriter(4096, 65535));
|
|
return new MqttConnectionContext(formatter, tcpConnection);
|
|
}
|
|
default:
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|