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:
@@ -88,5 +88,28 @@ let nc = await connect({
|
||||
});
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="C" %}
|
||||
```c
|
||||
natsConnection *conn = NULL;
|
||||
natsOptions *opts = NULL;
|
||||
natsStatus s = NATS_OK;
|
||||
|
||||
s = natsOptions_Create(&opts);
|
||||
if (s == NATS_OK)
|
||||
// Pass the credential file this way if the file contains both user JWT and seed.
|
||||
// Otherwise, if the content is split, the first file is the user JWT, the second
|
||||
// contains the seed.
|
||||
s = natsOptions_SetUserCredentialsFromFiles(opts, "path_to_creds_file", NULL);
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
@@ -105,5 +105,45 @@ let nc = await connect({
|
||||
});
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="C" %}
|
||||
```c
|
||||
static natsStatus
|
||||
sigHandler(
|
||||
char **customErrTxt,
|
||||
unsigned char **signature,
|
||||
int *signatureLength,
|
||||
const char *nonce,
|
||||
void *closure)
|
||||
{
|
||||
// Sign the given `nonce` and return the signature as `signature`.
|
||||
// This needs to allocate memory. The length of the signature is
|
||||
// returned as `signatureLength`.
|
||||
// If an error occurs the user can return specific error text through
|
||||
// `customErrTxt`. The library will free this pointer.
|
||||
|
||||
return NATS_OK;
|
||||
}
|
||||
|
||||
(...)
|
||||
|
||||
natsConnection *conn = NULL;
|
||||
natsOptions *opts = NULL;
|
||||
natsStatus s = NATS_OK;
|
||||
const char *pubKey = "my public key......";
|
||||
|
||||
s = natsOptions_Create(&opts);
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetNKey(opts, pubKey, sigHandler, NULL);
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
@@ -203,6 +203,28 @@ let nc = await connect({
|
||||
});
|
||||
```
|
||||
{% 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_SetCertificatesChain(opts, "client-cert.pem", "client-key.pem");
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetCATrustedCertificates(opts, "rootCA.pem");
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
## Connecting with the TLS Protocol
|
||||
|
||||
@@ -69,6 +69,26 @@ end
|
||||
let nc = await connect({url: server.nats, token: "mytoken"});
|
||||
```
|
||||
{% 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_SetToken(opts, "mytoken");
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
## Connecting with a Token in the URL
|
||||
@@ -134,5 +154,25 @@ let url = `nats://:mytoken@127.0.0.1:${port}`;
|
||||
let nc = await connect({url: url});
|
||||
```
|
||||
{% 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_SetURL(opts, "nats://mytoken@127.0.0.1:4222");
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
@@ -93,6 +93,26 @@ end
|
||||
let nc = await connect({url: server.nats, user: "myname", pass: "password"});
|
||||
```
|
||||
{% 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_SetUserInfo(opts, "myname", "password");
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
(...)
|
||||
|
||||
// Destroy objects that were created
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
## Connecting with a User/Password in the URL
|
||||
@@ -172,5 +192,25 @@ let url = `nats://myname:password@127.0.0.1:${port}`;
|
||||
let nc = await connect({url: url});
|
||||
```
|
||||
{% 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_SetURL(opts, "nats://myname:password@127.0.0.1:4222");
|
||||
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