Example #1
0
        void qosManager_MessageReceived(object sender, MqttMessageReceivedEventArgs e)
        {
            if (e.Message == null)
            {
                //a null message means we have disconnected from the broker
                OnConnectionLost(new EventArgs());
                return;
            }

            switch (e.Message.MsgType)
            {
                case MessageType.CONNACK:
                    OnConnected(new EventArgs());
                    break;
                case MessageType.DISCONNECT:
                    break;
                case MessageType.PINGREQ:
                    manager.SendMessage(new MqttPingRespMessage());
                    break;
                case MessageType.PUBACK:
                    MqttPubackMessage puback = (MqttPubackMessage)e.Message;
                    OnPublished(new CompleteArgs(puback.AckID));
                    break;
                case MessageType.PUBCOMP:
                    break;
                case MessageType.PUBLISH:
                    MqttPublishMessage m = (MqttPublishMessage)e.Message;
                    OnPublishArrived(m);
                    break;
                case MessageType.PUBREC:
                    break;
                case MessageType.PUBREL:
                    MqttPubrelMessage pubrel = (MqttPubrelMessage)e.Message;
                    OnPublished(new CompleteArgs(pubrel.AckID));
                    break;
                case MessageType.SUBACK:
                    MqttSubackMessage m1 = (MqttSubackMessage)e.Message;
                    OnSubscribed(new CompleteArgs(m1.AckID));
                    break;
                case MessageType.UNSUBACK:
                    MqttUnsubackMessage m2 = (MqttUnsubackMessage)e.Message;
                    OnUnsubscribed(new CompleteArgs(m2.AckID));
                    break;
                case MessageType.PINGRESP:
                    break;
                case MessageType.UNSUBSCRIBE:
                case MessageType.CONNECT:
                case MessageType.SUBSCRIBE:
                default:
                    throw new Exception("Unsupported Message Type");

            }
        }
Example #2
0
        void qosManager_MessageReceived(object sender, MqttMessageReceivedEventArgs e)
        {
            if (e.Message == null)
            {
                //a null message means we have disconnected from the broker
                OnConnectionLost(new EventArgs());
                return;
            }

            switch (e.Message.MsgType)
            {
                case MessageType.CONNACK:
                    var connack = ((MqttConnackMessage)e.Message);
                    if (connack.Response == MqttConnectionResponse.Accepted)
                        OnConnected(new EventArgs());
                    else
                        OnConnectionLost(new MqttConnackEventArgs(connack.Response));
                    break;
                case MessageType.DISCONNECT:
                    break;
                case MessageType.PINGREQ:
                    manager.SendMessage(new MqttPingRespMessage());
                    break;
                case MessageType.PUBACK:
                    MqttPubackMessage puback = (MqttPubackMessage)e.Message;
                    OnPublished(new CompleteArgs(puback.AckID));
                    break;
                case MessageType.PUBCOMP:
                    break;
                case MessageType.PUBLISH:
                    MqttPublishMessage m = (MqttPublishMessage)e.Message;
                    OnPublishArrived(m);
                    break;
                case MessageType.PUBREC:
                    break;
                case MessageType.PUBREL:
                    MqttPubrelMessage pubrel = (MqttPubrelMessage)e.Message;
                    OnPublished(new CompleteArgs(pubrel.AckID));
                    break;
                case MessageType.SUBACK:
                    MqttSubackMessage m1 = (MqttSubackMessage)e.Message;
                    OnSubscribed(new CompleteArgs(m1.AckID));
                    break;
                case MessageType.UNSUBACK:
                    MqttUnsubackMessage m2 = (MqttUnsubackMessage)e.Message;
                    OnUnsubscribed(new CompleteArgs(m2.AckID));
                    break;
                case MessageType.EXTENDEDACK:
                    MqttExtendedackMessage mm = (MqttExtendedackMessage)e.Message;
                    if (mm.CommondId == 8)        // publish2 ack or publish2alias ack
                        OnPublish2ed(mm);
                    else
                        OnExtendedAckArrived(new ExtendedAckArrivedArgs(mm.AckID, mm.CommondId, mm.Status, mm.Payload));
                    break;
                case MessageType.PINGRESP:
                    break;
                case MessageType.UNSUBSCRIBE:
                case MessageType.CONNECT:
                case MessageType.SUBSCRIBE:
                default:
                    throw new Exception("Unsupported Message Type");

            }
        }