예제 #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();
        }
예제 #2
0
 public static ServerService CreateIntance()
 {
     if (_instance == null)
     {
         lock (syncLocker)
         {
             if (_instance == null)
             {
                 _instance = new ServerService();
             }
         }
     }
     return(_instance);
 }