Created
August 7, 2014 16:32
-
-
Save davidszotten/549b5306f2b34a205075 to your computer and use it in GitHub Desktop.
redis c pubsub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <hiredis.h> | |
| int main(int argc, char **argv) { | |
| unsigned int j; | |
| redisContext *conn; | |
| redisReply *reply; | |
| const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1"; | |
| int port = (argc > 2) ? atoi(argv[2]) : 6379; | |
| struct timeval timeout = { 1, 500000 }; // 1.5 seconds | |
| conn = redisConnectWithTimeout(hostname, port, timeout); | |
| if (conn == NULL || conn->err) { | |
| if (conn) { | |
| printf("Connection error: %s\n", conn->errstr); | |
| redisFree(conn); | |
| } else { | |
| printf("Connection error: can't allocate redis context\n"); | |
| } | |
| exit(1); | |
| } | |
| reply = redisCommand(conn,"PUBLISH foo hello"); | |
| printf("PUBLISH: %s\n", reply->str); | |
| freeReplyObject(reply); | |
| /* Disconnects and frees the context */ | |
| redisFree(conn); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment