void ortc_subscribe(ortc_context* context, char *channel, int subscribeOnReconnected, void (*onMessage)(ortc_context*, char*, char*)){ if(context->state != CONNECTED){ _ortc_exception(context, "Not connected"); } else if(!channel || strlen(channel)==0){ _ortc_exception(context, "Channel is null or empty"); } else if(!_ortc_isValidInput(context, channel)) { _ortc_exception(context, "Channel has invalid characters"); } else if(_ortc_is_subscribing(context, channel)){ char *ex_msg = _ortc_ch_ex_msg("Already trying to subscribe this channel", channel); _ortc_exception(context, ex_msg); free(ex_msg); } else if(ortc_is_subscribed(context, channel)){ char *ex_msg = _ortc_ch_ex_msg("Already subscribed to this channel", channel); _ortc_exception(context, ex_msg); free(ex_msg); } else if(strlen(channel) > ORTC_CHANNEL_MAX_SIZE){ _ortc_exception(context, "Channel size exceeds the limit of characters"); } else if(!onMessage){ _ortc_exception(context, "The argument \"onMessage\" must point to a function"); } else { _ortc_subscribe(context, channel, subscribeOnReconnected, 1, onMessage); } }
void ortc_unsubscribe(ortc_context* context, char *channel){ if(context->state != CONNECTED){ _ortc_exception(context, "Not connected"); } else if(!channel || strlen(channel)==0){ _ortc_exception(context, "Channel is null or empty"); } else if(!_ortc_isValidInput(context, channel)) { _ortc_exception(context, "Channel has invalid characters"); } else if(!ortc_is_subscribed(context, channel)){ char *ex_msg = _ortc_ch_ex_msg("Not subscribed to the channel", channel); _ortc_exception(context, ex_msg); free(ex_msg); } else if(strlen(channel) > ORTC_CHANNEL_MAX_SIZE){ _ortc_exception(context, "Channel size exceeds the limit of characters"); } else { _ortc_unsubscribe(context, channel); } }
int main(void){ char key = ' '; ortc_context *context; ortc_channelPermissions chPerm[] = { {"yellow", "rwp"}, {"blue", "rw"}, {"black", "w"} }; #if _MSC_VER SetConsoleOutputCP(65001); #endif context = ortc_create_context(); ortc_set_cluster(context, ORTC_CLUSTER); ortc_set_connection_metadata(context, "api c example metadata"); ortc_set_onConnected (context, onConnected); ortc_set_onDisconnected(context, onDisconnected); ortc_set_onSubscribed (context, onSubscribed); ortc_set_onUnsubscribed(context, onUnsubscribed); ortc_set_onException (context, onException); ortc_set_onReconnected (context, onReconnected); ortc_set_onReconnecting(context, onReconnecting); print_help(); while(key!='q'){ key = getchar(); switch(key) { case 'h': print_help(); break; case 'v': print_version(); break; case 'c': ortc_connect(context, ORTC_APP_KEY, ORTC_AUTH_TOKEN); break; case 'd': ortc_disconnect(context); break; case 's': ortc_subscribe(context, DEFAULT_CHANNEL, 1, onMessage); break; case 'u': ortc_unsubscribe(context, DEFAULT_CHANNEL); break; case 'm': ortc_send(context, DEFAULT_CHANNEL, "api C message"); break; case 'p': ortc_presence(context, DEFAULT_CHANNEL, onPresence); break; case 'e': ortc_presence_ex(context, ORTC_CLUSTER, 1, ORTC_APP_KEY, ORTC_AUTH_TOKEN, DEFAULT_CHANNEL, onPresence); break; case 'i': printf("Is subscribed? %s\n", (ortc_is_subscribed(context, DEFAULT_CHANNEL)?"true":"false")); break; case '1': ortc_enable_presence(context, ORTC_PRV_KEY, DEFAULT_CHANNEL, 1, onPresenceCommand); break; case '0': printf("Is connected? %s\n", (ortc_is_connected(context)?"true":"false")); break; case '2': ortc_disable_presence(context, ORTC_PRV_KEY, DEFAULT_CHANNEL, onPresenceCommand); break; case '3': ortc_enable_presence_ex(context, ORTC_CLUSTER, 1, ORTC_APP_KEY, ORTC_PRV_KEY, DEFAULT_CHANNEL, 1, onPresenceCommand); break; case '4': ortc_disable_presence_ex(context, ORTC_CLUSTER, 1, ORTC_APP_KEY, ORTC_PRV_KEY, DEFAULT_CHANNEL, onPresenceCommand); break; case 'a': ortc_save_authentication(context, ORTC_AUTH_TOKEN, 0, 3600, ORTC_PRV_KEY, chPerm, 3, onAuthentication); break; case '5': ortc_save_authentication_ex(context, ORTC_CLUSTER, 1, ORTC_AUTH_TOKEN, 0, ORTC_APP_KEY, 3600, ORTC_PRV_KEY, chPerm, 3, onAuthentication); break; case 'z': printf("Session ID: %s\n", ortc_get_sessionId(context)); break; } } ortc_free_context(context); return 0; }