Created
March 3, 2014 15:53
-
-
Save sukinull/9327937 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// Created by sukinull on 2/14/14. | |
// Copyright (c) 2014 sukinull. All rights reserved. | |
// | |
// Simplest websocks example using nopoll - http://www.aspl.es/nopoll/ | |
// remark nopoll_conn_set_sock_block (session, nopoll_false); in nopoll_conn_sock_connect under OS X | |
// use nopoll_conn_set_sock_block to set sock non-block after connect | |
// | |
#include <stdio.h> | |
#include <nopoll.h> | |
int main(int argc, char* argv[]) | |
{ | |
noPollCtx * ctx = nopoll_ctx_new (); | |
if (! ctx) { | |
// error some handling code here | |
fprintf(stderr, "nopoll_ctx_new is failed\n"); | |
nopoll_ctx_unref (ctx); | |
return 1; | |
} | |
nopoll_log_enable (ctx, nopoll_true); | |
// call to create a connection | |
nopoll_conn_connect_timeout(ctx, 10000l); | |
noPollConn * conn = nopoll_conn_new (ctx, "127.0.0.1", "8000", NULL, NULL, NULL, NULL); | |
if (! nopoll_conn_is_ok (conn)) { | |
// some error handling here | |
fprintf(stderr, "nopoll_conn_new is failed\n"); | |
nopoll_ctx_unref (ctx); | |
return 2; | |
} | |
while(nopoll_true != nopoll_conn_is_ready(conn)) { | |
fprintf(stderr, "#"); | |
} | |
// send a message | |
if (nopoll_conn_send_text (conn, "Echo!", 5) != 5) { | |
// send a message | |
fprintf(stderr, "nopoll_conn_send_text\n"); | |
nopoll_ctx_unref (ctx); | |
return 3; | |
} | |
/* wait for the reply */ | |
noPollMsg *msg; | |
while ((msg = nopoll_conn_get_msg (conn)) == NULL) { | |
continue; | |
} | |
const noPollPtr m = nopoll_msg_get_payload(msg); | |
int n = nopoll_msg_get_payload_size(msg); | |
fprintf(stderr, "connection mesg: %s[%d]\n", m, n); | |
if (! nopoll_conn_is_ok (conn)) { | |
printf ("ERROR: received websocket connection close during wait reply..\n"); | |
return nopoll_false; | |
} | |
// do some WebSocket operations (as client or listener)... | |
// ...and once you are done and after terminating all messages and | |
// connections created you have to release the context by doing the | |
// following: | |
nopoll_ctx_unref (ctx); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, sukinull, can you help me? if I meet the url has log path, such like ws://s-test.api.aispeech.com:10000/cn.dlg.ita/xm_aihome?applicationId=14476531938594b9×tamp=1460946981&authId=5673ffe0-050e-11e6-8b70-8c705ac0a268&sig=91b6a3121898bc027283c44b729e19f136a8beee&userId=test_yuyintianxia, how do I use the API nopoll_conn_new(), thanks!