Beispiel #1
0
 public MqttClient(string server, string clientId)
 {
     Server         = server;
     ClientId       = clientId;
     conn           = new MqttConnection();
     conn.Recv     += DecodeMessage;
     connResetEvent = new AutoResetEvent(false);
 }
Beispiel #2
0
 public MqttClient(string server, string clientId = "")
 {
     Server = server;
     if (string.IsNullOrEmpty(clientId))
     {
         clientId = Guid.NewGuid().ToString("N");
     }
     ClientId       = clientId;
     conn           = new MqttConnection();
     conn.Recv     += DecodeMessage;
     connResetEvent = new AutoResetEvent(false);
 }
Beispiel #3
0
        void DecodeMessage(byte[] buffer)
        {
            var msg = MqttMessage.DecodeMessage(buffer);

            Debug.WriteLine("onRecv:{0}", msg.FixedHeader.MessageType);
            switch (msg.FixedHeader.MessageType)
            {
            case MessageType.CONNACK:
                var connAckMsg = (ConnAckMessage)msg;
                if (connAckMsg.ReturnCode == MqttConnectReturnCode.BrokerUnavailable ||
                    connAckMsg.ReturnCode == MqttConnectReturnCode.IdentifierRejected ||
                    connAckMsg.ReturnCode == MqttConnectReturnCode.UnacceptedProtocolVersion ||
                    connAckMsg.ReturnCode == MqttConnectReturnCode.NotAuthorized ||
                    connAckMsg.ReturnCode == MqttConnectReturnCode.BadUsernameOrPassword)
                {
                    ConnectionState = ConnectionState.Disconnecting;
                    Dispose();
                    conn            = null;
                    ConnectionState = ConnectionState.Disconnected;
                }
                else
                {
                    ConnectionState = ConnectionState.Connected;
                }
                connResetEvent.Set();
                break;

            case MessageType.PUBLISH:
                var    pubMsg = (PublishMessage)msg;
                string topic  = pubMsg.TopicName;
                var    data   = pubMsg.Payload;
                if (pubMsg.FixedHeader.Qos == Qos.AtLeastOnce)
                {
                    var ackMsg = new PublishAckMessage();
                    ackMsg.MessageIdentifier = pubMsg.MessageIdentifier;
                    conn.SendMessage(ackMsg);
                }
                OnMessageReceived(topic, data);
                break;

            case MessageType.PUBACK:
                var pubAckMsg = (PublishAckMessage)msg;
                Debug.WriteLine("PUBACK MessageIdentifier:" + pubAckMsg.MessageIdentifier);
                break;

            case MessageType.PUBREC:
                break;

            case MessageType.PUBREL:
                break;

            case MessageType.PUBCOMP:
                break;

            case MessageType.SUBSCRIBE:
                break;

            case MessageType.SUBACK:
                break;

            case MessageType.UNSUBSCRIBE:
                break;

            case MessageType.UNSUBACK:
                break;

            case MessageType.PINGREQ:
                conn.SendMessage(new PingRespMessage());
                break;

            case MessageType.DISCONNECT:
                Disconnect();
                break;
            }
        }