代码拉取完成,页面将自动刷新
#include <libwebsockets.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static struct lws *web_socket = NULL;
#define EXAMPLE_TX_BUFFER_BYTES 10
static int callback_example( struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len )
{
switch( reason )
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
lws_callback_on_writable( wsi );
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
/* Handle incomming messages here. */
break;
case LWS_CALLBACK_CLIENT_WRITEABLE:
{
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + EXAMPLE_TX_BUFFER_BYTES + LWS_SEND_BUFFER_POST_PADDING];
unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
int num = rand();
size_t n = sprintf( (char *)p, "%u", num );
lws_write( wsi, p, n, LWS_WRITE_TEXT );
printf("num = %d\r\n",num);
break;
}
case LWS_CALLBACK_CLIENT_CLOSED:
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
web_socket = NULL;
break;
default:
break;
}
return 0;
}
enum protocols
{
PROTOCOL_EXAMPLE = 0,
PROTOCOL_COUNT
};
static struct lws_protocols protocols[] =
{
{
.name = "example-protocol", /* Protocol name*/
.callback = callback_example, /* Protocol callback */
.per_session_data_size = 0, /* Protocol callback 'userdata' size */
.rx_buffer_size = 0, /* Receve buffer size (0 = no restriction) */
.id = 0, /* Protocol Id (version) (optional) */
.user = NULL, /* 'User data' ptr, to access in 'protocol callback */
.tx_packet_size = 0 /* Transmission buffer size restriction (0 = no restriction) */
},
LWS_PROTOCOL_LIST_TERM /* terminator */
};
int main( int argc, char *argv[] )
{
struct lws_context_creation_info info;
memset( &info, 0, sizeof(info) );
info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
info.protocols = protocols;
info.gid = -1;
info.uid = -1;
struct lws_context *context = lws_create_context( &info );
time_t old = 0;
while( 1 )
{
struct timeval tv;
gettimeofday( &tv, NULL );
printf("step1\r\n");
/* Connect if we are not connected to the server. */
if( !web_socket && tv.tv_sec != old )
{
struct lws_client_connect_info ccinfo;
memset(&ccinfo, 0, sizeof(ccinfo));
ccinfo.context = context;
ccinfo.address = "192.168.2.214";
ccinfo.port = 8000;
ccinfo.path = "/";
ccinfo.host = lws_canonical_hostname( context );
ccinfo.origin = "origin";
ccinfo.protocol = protocols[PROTOCOL_EXAMPLE].name;
web_socket = lws_client_connect_via_info(&ccinfo);
}
printf("step2\r\n");
printf("tv.tv_sec = %ld old= %ld\r\n",tv.tv_sec,old);
if( tv.tv_sec != old )
{
/* Send a random number to the server every second. */
//printf("tv.tv_sec = %ld old= %ld\r\n",tv.tv_sec,old);
lws_callback_on_writable( web_socket );
old = tv.tv_sec;
}
printf("step3\r\n");
lws_service( context, /* timeout_ms = */ 0 ); /* NOTE: since v3.2, timeout_ms may be set to '0', since it internally ignored */
printf("step4\r\n");
}
lws_context_destroy( context );
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。