Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            MqttServerService mqttServerService = new MqttServerService();
            IMqttServer       mqttServer        = mqttServerService.CreateMqttServer();

            mqttServer.ApplicationMessageReceived += MqttServer_ApplicationMessageReceived;
            mqttServer.ClientConnected            += MqttServer_ClientConnected;
            mqttServer.ClientDisconnected         += MqttServer_ClientDisconnected;
            mqttServer.ClientSubscribedTopic      += MqttServer_ClientSubscribedTopic;
            mqttServer.ClientUnsubscribedTopic    += MqttServer_ClientSubscribedTopic;

            ServerService serverService = new ServerService();
            Server        server        = new Server();

            server.ServerName = Guid.NewGuid().ToString();
            server.mqttServer = mqttServer;
            serverService.AddServer(server);

            mqttServerService.StartMqttServer(mqttServer, CreateOptions());
            Console.WriteLine("发送多少次? ");
            string key = Console.ReadLine();

            for (int i = 0; i < Convert.ToInt32(key); i++)
            {
                var applicationMessage = new MqttApplicationMessageBuilder()
                                         .WithTopic("A/B/C")
                                         .WithPayload(Encoding.UTF8.GetBytes(i.ToString()))
                                         .WithExactlyOnceQoS();
                mqttServerService.BroadCast(mqttServer, applicationMessage);
            }

            Console.ReadLine();
        }
Ejemplo n.º 2
0
 public static MqttServerService CreateInstance()
 {
     if (_instance == null)
     {
         lock (lockObject)
         {
             if (_instance == null)
             {
                 _instance = new MqttServerService();
             }
         }
     }
     return(_instance);
 }
Ejemplo n.º 3
0
 private void Form1_Load(object sender, EventArgs e)
 {
     mqttServerService = MqttServerService.CreateInstance();
     mqttServerService.OnMqttConnectNotify += MqttServerService_OnMqttConnectNotify;
     mqttServerService.OnMqttMessageNotify += MqttServerService_OnMqttMessageNotify;
 }