/// <summary> /// mqtt客户端 /// </summary> /// <param name="serverUri">mqtt://mymqtt.com</param> /// <param name="clientId">客户端id</param> /// <param name="persistence">持久化方式</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="UriFormatException"></exception> /// <exception cref="NotSupportedException"></exception> /// <exception cref="MqttException"></exception> public MqttClient(string serverUri, string clientId, MqttPersistence persistence) : base(IntPtr.Zero, true) { if (string.IsNullOrEmpty(serverUri)) { throw new ArgumentNullException("serverUri"); } if (string.IsNullOrEmpty(clientId)) { throw new ArgumentNullException("clientId"); } if (persistence == MqttPersistence.User) { var message = string.Format("不支持的持久化方式:{0}.{1}", typeof(MqttPersistence).Name, persistence); throw new NotSupportedException(message); } var uri = new Uri(serverUri); const int mqttDefaultPort = 1883; var tcpUri = string.Format("tcp://{0}:{1}", uri.Host, uri.Port > 0 ? uri.Port : mqttDefaultPort); var er = MQTTAsync.MQTTAsync_create(ref this.handle, tcpUri, clientId, persistence, IntPtr.Zero); this.EnsureSuccessCode(er); this.InitClientCallbacks(); }
public static extern MqttError MQTTAsync_create( ref IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string serverURI, [MarshalAs(UnmanagedType.LPStr)] string clientId, MqttPersistence persistence_type, IntPtr persistence_context);