mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
Add C Client examples
This commit is contained in:
@@ -59,6 +59,27 @@ nc.close();
|
||||
// Reconnect buffer size is not configurable on NATS Typescript client
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="C" %}
|
||||
```c
|
||||
natsConnection *conn = NULL;
|
||||
natsOptions *opts = NULL;
|
||||
natsStatus s = NATS_OK;
|
||||
|
||||
s = natsOptions_Create(&opts);
|
||||
if (s == NATS_OK)
|
||||
// Set reconnect buffer size in bytes (5 MB)
|
||||
s = natsOptions_SetReconnectBufSize(opts, 5*1024*1024);
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
> _As mentioned throughout this document, each client library may behave slightly differently. Please check the documentation for the library you are using._
|
||||
|
||||
@@ -80,5 +80,25 @@ let nc = await connect({
|
||||
nc.close();
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="C" %}
|
||||
```c
|
||||
natsConnection *conn = NULL;
|
||||
natsOptions *opts = NULL;
|
||||
natsStatus s = NATS_OK;
|
||||
|
||||
s = natsOptions_Create(&opts);
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetAllowReconnect(opts, false);
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
@@ -112,5 +112,46 @@ nc.on('reconnect', (conn, server) => {
|
||||
nc.close();
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="C" %}
|
||||
```c
|
||||
static void
|
||||
disconnectedCB(natsConnection *conn, void *closure)
|
||||
{
|
||||
// Handle disconnect error event
|
||||
}
|
||||
|
||||
static void
|
||||
reconnectedCB(natsConnection *conn, void *closure)
|
||||
{
|
||||
// Handle reconnect event
|
||||
}
|
||||
|
||||
(...)
|
||||
|
||||
natsConnection *conn = NULL;
|
||||
natsOptions *opts = NULL;
|
||||
natsStatus s = NATS_OK;
|
||||
|
||||
s = natsOptions_Create(&opts);
|
||||
|
||||
// Connection event handlers are invoked asynchronously
|
||||
// and the state of the connection may have changed when
|
||||
// the callback is invoked.
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetDisconnectedCB(opts, disconnectedCB, NULL);
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetReconnectedCB(opts, reconnectedCB, NULL);
|
||||
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
@@ -75,5 +75,25 @@ let nc = await connect({
|
||||
nc.close();
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="C" %}
|
||||
```c
|
||||
natsConnection *conn = NULL;
|
||||
natsOptions *opts = NULL;
|
||||
natsStatus s = NATS_OK;
|
||||
|
||||
s = natsOptions_Create(&opts);
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetMaxReconnect(opts, 10);
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
@@ -90,5 +90,27 @@ let nc = await connect({
|
||||
nc.close();
|
||||
```
|
||||
{% endtab %}
|
||||
{% tab title="C" %}
|
||||
```c
|
||||
natsConnection *conn = NULL;
|
||||
natsOptions *opts = NULL;
|
||||
natsStatus s = NATS_OK;
|
||||
const char *servers[] = {"nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"};
|
||||
|
||||
s = natsOptions_Create(&opts);
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetServers(opts, servers, 3);
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetNoRandomize(opts, true);
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
@@ -76,5 +76,26 @@ let nc = await connect({
|
||||
nc.close();
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="C" %}
|
||||
```c
|
||||
natsConnection *conn = NULL;
|
||||
natsOptions *opts = NULL;
|
||||
natsStatus s = NATS_OK;
|
||||
|
||||
s = natsOptions_Create(&opts);
|
||||
if (s == NATS_OK)
|
||||
// Set reconnect interval to 10 seconds (10,000 milliseconds)
|
||||
s = natsOptions_SetReconnectWait(opts, 10000);
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user