160 lines
4.7 KiB
C#
160 lines
4.7 KiB
C#
using MQTTnet;
|
|
using MQTTnet.Protocol;
|
|
using Proto;
|
|
using Proto.Extensions;
|
|
using System.Reflection;
|
|
|
|
namespace MyProtoActor
|
|
{
|
|
internal class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
var system = new ActorSystem();
|
|
var props = Props.FromProducer(() => new MyMQTTSuper("111")).WithChildSupervisorStrategy(
|
|
new OneForOneStrategy((PID pid, Exception ex) =>
|
|
{
|
|
return SupervisorDirective.Restart;
|
|
}, 10, new TimeSpan(0, 0, 5))
|
|
);
|
|
|
|
PID pid = system.Root.SpawnNamed(props, "MyAAA");
|
|
system.Root.RequestAsync<StartMessage>(pid, new StartMessage() { Text = "aaaaaaaaaa" }).GetAwaiter().GetResult();
|
|
//system.Root.Send(pid, new StartMessage() { Text = "aaaaaaaaaa" });
|
|
|
|
////PID pid1= PID.FromAddress(system.Address, "MQTT");
|
|
|
|
//Console.WriteLine("Hello, World!");
|
|
//Console.ReadLine();
|
|
//Console.WriteLine(1111111111111);
|
|
//system.Root.Send(pid, new StartMessage() { Text = "aaaaaaaaaa" });
|
|
Console.ReadLine();
|
|
}
|
|
}
|
|
public record StartMessage
|
|
{
|
|
public string? Text { get; set; }
|
|
}
|
|
public class MyMQTTSuper : IActor
|
|
{
|
|
public string Name { get; set; }
|
|
public MyMQTTSuper(string name)
|
|
{
|
|
Name = name;
|
|
}
|
|
public Task ReceiveAsync(IContext context)
|
|
{
|
|
PID pid = null;
|
|
var child = context.Children;
|
|
var MSG = context.Message;
|
|
if (MSG is Proto.Started && (child == null || child.Count == 0))
|
|
{
|
|
var props = Props.FromProducer(() => new MyMQTTServer());
|
|
PID pid111 = context.SpawnNamed(props, "MQTT");
|
|
//context.Watch(pid111);
|
|
//context.Send(pid111, new StartMessage { Text = "started" });
|
|
}
|
|
else
|
|
{
|
|
pid = child.FirstOrDefault();
|
|
|
|
if (pid != null)
|
|
{
|
|
context.Forward(pid);
|
|
}
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
public class MyMQTTServer : IActor
|
|
{
|
|
public MyMQTTServer()
|
|
{
|
|
|
|
InitializeMqttClient();
|
|
}
|
|
public Task ReceiveAsync(IContext context)
|
|
{
|
|
var msg = context.Message;
|
|
Console.WriteLine("1次");
|
|
if (msg is Proto.Started)
|
|
{
|
|
_mqttClient.ConnectAsync(_options).GetAwaiter().GetResult();
|
|
//int a = 3;
|
|
//int b = 0;
|
|
//int c = a / b;
|
|
var usa = msg as Proto.Started;
|
|
|
|
var sss= context.Sender;
|
|
context.Respond(new StartMessage { Text = "started" });
|
|
}
|
|
if (msg is StartMessage)
|
|
{
|
|
}
|
|
if (msg.ToString().Equals("start"))
|
|
{
|
|
int a = 3;
|
|
int b = 0;
|
|
int c = a / b;
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public static IMqttClient? _mqttClient;
|
|
public MqttClientOptions? _options;
|
|
private async Task InitializeMqttClient()
|
|
{
|
|
try
|
|
{
|
|
var factory = new MQTTnet.MqttClientFactory();
|
|
_mqttClient = factory.CreateMqttClient();
|
|
|
|
|
|
var IP = "";
|
|
int Port = 1883;
|
|
var UserName = "";
|
|
var PWD = "";
|
|
string clientId = "IOTConsumer#" + Guid.NewGuid().ToString("N");
|
|
|
|
_options = new MqttClientOptionsBuilder()
|
|
.WithTcpServer(IP, Port)
|
|
.WithClientId(clientId)
|
|
.WithCredentials(UserName, PWD)
|
|
.WithWillQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce)
|
|
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
|
|
.Build();
|
|
|
|
// 注册事件
|
|
_mqttClient.ConnectedAsync += OnConnectedAsync;
|
|
_mqttClient.DisconnectedAsync += OnDisconnectedAsync;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
|
|
private async Task OnConnectedAsync(MqttClientConnectedEventArgs arg)
|
|
{
|
|
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
private async Task OnDisconnectedAsync(MqttClientDisconnectedEventArgs e)
|
|
{
|
|
|
|
// 如果不是正常断开,启动重连
|
|
if (e.Reason != MqttClientDisconnectReason.NormalDisconnection)
|
|
{
|
|
|
|
}
|
|
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
}
|
|
}
|