diff --git a/clients/c/cosmopolite.c b/clients/c/cosmopolite.c index b7a9568..120d927 100644 --- a/clients/c/cosmopolite.c +++ b/clients/c/cosmopolite.c @@ -254,6 +254,18 @@ void cosmo_subscribe(cosmo *instance, const json_t *subject, const json_int_t me cosmo_send_command(instance, cosmo_command("subscribe", arguments)); } +void cosmo_send_message(cosmo *instance, const json_t *subject, json_t *message) { + char sender_message_id[COSMO_UUID_SIZE]; + cosmo_generate_uuid(sender_message_id); + char *encoded = json_dumps(message, JSON_ENCODE_ANY); + json_t *arguments = json_pack("{sOssss}", + "subject", subject, + "message", encoded, + "sender_message_id", sender_message_id); + cosmo_send_command(instance, cosmo_command("sendMessage", arguments)); + free(encoded); +} + cosmo *cosmo_create(const char *base_url, const char *client_id) { curl_global_init(CURL_GLOBAL_DEFAULT); srandomdev(); diff --git a/clients/c/cosmopolite.h b/clients/c/cosmopolite.h index 6b49ee8..599f076 100644 --- a/clients/c/cosmopolite.h +++ b/clients/c/cosmopolite.h @@ -29,5 +29,6 @@ void cosmo_destroy(cosmo *instance); json_t *cosmo_subject(const char *name, const char *readable_only_by, const char *writeable_only_by); void cosmo_subscribe(cosmo *instance, const json_t *subject, const json_int_t messages, const json_int_t last_id); +void cosmo_send_message(cosmo *instance, const json_t *subject, json_t *message); #endif diff --git a/clients/c/test.c b/clients/c/test.c index 1bce181..f8ddf76 100644 --- a/clients/c/test.c +++ b/clients/c/test.c @@ -8,6 +8,9 @@ int main(int argc, char *argv[]) { cosmo *instance = cosmo_create("https://playground.cosmopolite.org/cosmopolite", client_id); json_t *subject = cosmo_subject("foobar", NULL, NULL); cosmo_subscribe(instance, subject, -1, 0); + json_t *message = json_string("test from C"); + cosmo_send_message(instance, subject, message); + json_decref(message); json_decref(subject); sleep(120); cosmo_destroy(instance);