private void DoClientPublishWorkflow(MqttNetEventArgs args) { var publishMsg = (MqttPublishMessage)args.Message; switch (publishMsg.QualityOfService) { // Just fire event and we're done case QualityOfService.AtMostOnce: OnPublishReceived(args); break; // Must send a PubAck message case QualityOfService.AtLeastOnce: var pubAck = new MqttPublishAckMessageBuilder { MessageId = publishMsg.MessageId, }; SendMessageAsync(pubAck, null, args.ClientUid); OnPublishReceived(args); break; // Must send a PubRec message case QualityOfService.ExactlyOnce: var pubRec = new MqttPublishReceivedMessageBuilder { MessageId = publishMsg.MessageId, }; SendMessageAsync(pubRec, publishMsg, args.ClientUid); break; } }
public void CanCreateFromMessageBuilder() { int id = 42; var msgBuilder = new MqttPublishAckMessageBuilder() { MessageId = id }; var msg = msgBuilder.GetMessage() as MqttPublishAckMessage; Assert.AreEqual(id, msg.MessageId); }
public void WriteAsync(SocketEventArgs args) { SentMessages.Add(args.MessageToSend.MessageType); args.Complete(); // Mock a server that does not send appropriate response if (DoNotRespond) { return; } // This section mocks the expected response behavior from an MQTT broker switch (args.MessageToSend.MessageType) { case MessageType.Connect: var conAck = new MqttConnectAckMessageBuilder(); MessageReceived(new MqttNetEventArgs { Message = conAck.GetMessage(), ClientUid = args.ClientUid }); break; case MessageType.Subscribe: var subMsg = args.MessageToSend as IMqttIdMessage; var subAck = new MqttSubscribeAckMessageBuilder { MessageId = subMsg.MessageId }; MessageReceived(new MqttNetEventArgs { Message = subAck.GetMessage(), ClientUid = args.ClientUid }); break; case MessageType.Unsubscribe: var unsubMsg = args.MessageToSend as IMqttIdMessage; var unsubAck = new MqttUnsubscribeAckMessageBuilder { MessageId = unsubMsg.MessageId }; MessageReceived(new MqttNetEventArgs { Message = unsubAck.GetMessage(), ClientUid = args.ClientUid }); break; case MessageType.PingReq: var pingResp = new MqttPingResponseMessageBuilder(); MessageReceived(new MqttNetEventArgs { Message = pingResp.GetMessage(), ClientUid = args.ClientUid }); break; case MessageType.Publish: var publishMsg = args.MessageToSend as IMqttIdMessage; // Mock publish response behavior if (args.MessageToSend.QualityOfService == QualityOfService.AtLeastOnce) { var msgRcv = new MqttPublishAckMessageBuilder { MessageId = publishMsg.MessageId }; MessageReceived(new MqttNetEventArgs { Message = msgRcv.GetMessage(), ClientUid = args.ClientUid }); } else if (args.MessageToSend.QualityOfService == QualityOfService.ExactlyOnce) { var msgRcv = new MqttPublishReceivedMessageBuilder() { MessageId = publishMsg.MessageId }; MessageReceived(new MqttNetEventArgs { Message = msgRcv.GetMessage(), ClientUid = args.ClientUid }); } break; case MessageType.PubRec: var pubRec = args.MessageToSend as IMqttIdMessage; var pubRel1 = new MqttPublishReleaseMessageBuilder { MessageId = pubRec.MessageId }; MessageReceived(new MqttNetEventArgs { Message = pubRel1.GetMessage(), ClientUid = args.ClientUid }); break; case MessageType.PubRel: var pubRel2 = args.MessageToSend as IMqttIdMessage; var pubComp = new MqttPublishCompleteMessageBuilder { MessageId = pubRel2.MessageId }; MessageReceived(new MqttNetEventArgs { Message = pubComp.GetMessage(), ClientUid = args.ClientUid }); break; } }