Document Directory:
1) Client Connection
In CZGL.AliIoTClient, there are two methods to connect to Alibaba Cloud IoT server:
public CZGL.AliIoTClient.ConnectCode ConnectIoT(string[] SubTopic,
[byte[] QOS = null],
[ushort keepAlivePeriod = 60])
public System.Threading.Tasks.Task<CZGL.AliIoTClient.ConnectCode>
ConnectIoTAsync(string[] topics,
[byte[] QOS = null],
[ushort keepAlivePeriod = 60])
Parameter description and return values:
Parameter Name | Type | Description |
---|---|---|
SubTopic | string[] | A list of topics to subscribe to. Only after subscribing to the topic can the server push messages to this topic. |
QOS | byte[] | Each topic has a configured QOS. If left empty, it will set QOS=0x00 for each topic. Note that QOS can only be 0, 1, or 2, so using byte is most suitable. |
keepAlivePeriod | ushort | The keep-alive period for monitoring. MQTT communication requires clients to provide timely feedback at intervals to prove their availability. If this period is exceeded, the server will consider the client as offline. |
Return Value | ConnectCode | This is the status code indicating the result of the connection. It is an enumeration type. Even if the connection fails due to issues such as key errors or network disconnections, no exception will be triggered; instead, a status code will be returned. |
Each topic has a QOS attribute, and the length of SubTopic should match the length of QOS, with corresponding index positions.
Meaning of QOS:
- QOS = 0 , At most once
- QOS = 1, At least once
- QOS = 2, Exactly once
ConnectCode:
When the client attempts to establish a connection with the server, it may succeed or fail. At this point, it returns specific connection status information. The ConnectCode enumeration is as follows:
Enum Name | Enum Value | Description |
---|---|---|
conn_accepted | 0x00 | Connection successful |
conn_refused_prot_vers | 0x01 | Protocol version |
conn_refused_ident_rejected | 0x02 | Authentication denied |
conn_refused_server_unavailable | x03 | Server unavailable (403/404 etc.) |
conn_refused_username_password | 0x04 | Username or password incorrect |
conn_refused_not_authorized | 0x05 | Not authorized |
unknown_error | 0x06 | Other unknown errors |
Example:
var code = client.ConnectIoT(topics, null, 60);
Console.WriteLine("Connection status: " + code);
2) Disconnect
public bool ConnectIoTClose()
Disconnecting will completely release the AliIoTClientJson object, not just go offline. To reconnect, please create a new object;
Example:
client.ConnectIoT(topics,null,60);
3) Check Status
Check whether the client maintains a connection with the server:
public bool isConnected { get; }
Example:
Console.WriteLine("Is the connection with the server maintained? " + client.isConnected);
文章评论