2 * libwebsockets-test-client - libwebsockets test implementation
4 * Copyright (C) 2011-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
16 * The test apps are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
29 #include "gettimeofday.h"
36 #include "../lib/libwebsockets.h"
38 static int deny_deflate, deny_mux, longlived, mirror_lifetime;
39 static struct lws *wsi_dumb, *wsi_mirror;
40 static volatile int force_exit;
41 static unsigned int opts;
44 * This demo shows how to connect multiple websockets simultaneously to a
45 * websocket server (there is no restriction on their having to be the same
46 * server just it simplifies the demo).
48 * dumb-increment-protocol: we connect to the server and print the number
51 * lws-mirror-protocol: draws random circles, which are mirrored on to every
52 * client (see them being drawn in every browser
53 * session also using the test server)
58 PROTOCOL_DUMB_INCREMENT,
67 * dumb_increment protocol
69 * since this also happens to be protocols[0], some callbacks that are not
70 * bound to a specific protocol also turn up here.
74 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
75 void *user, void *in, size_t len)
77 char *buf = (char *)in;
81 case LWS_CALLBACK_CLIENT_ESTABLISHED:
82 lwsl_info("dumb: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
85 case LWS_CALLBACK_CLOSED:
86 lwsl_notice("dumb: LWS_CALLBACK_CLOSED\n");
90 case LWS_CALLBACK_CLIENT_RECEIVE:
91 ((char *)in)[len] = '\0';
92 lwsl_info("rx %d '%s'\n", (int)len, (char *)in);
95 /* because we are protocols[0] ... */
97 case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
98 if (wsi == wsi_dumb) {
99 lwsl_err("dumb: LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
102 if (wsi == wsi_mirror) {
103 lwsl_err("mirror: LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
108 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
109 if ((strcmp(in, "deflate-stream") == 0) && deny_deflate) {
110 lwsl_notice("denied deflate-stream extension\n");
113 if ((strcmp(in, "x-webkit-deflate-frame") == 0))
115 if ((strcmp(in, "deflate-frame") == 0))
119 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
124 case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
137 /* lws-mirror_protocol */
141 callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
142 void *user, void *in, size_t len)
144 unsigned char buf[LWS_PRE + 4096];
145 unsigned int rands[4];
150 case LWS_CALLBACK_CLIENT_ESTABLISHED:
152 lwsl_notice("mirror: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
154 lws_get_random(lws_get_context(wsi), rands, sizeof(rands[0]));
155 mirror_lifetime = 16384 + (rands[0] & 65535);
156 /* useful to test single connection stability */
158 mirror_lifetime += 500000;
160 lwsl_info("opened mirror connection with "
161 "%d lifetime\n", mirror_lifetime);
164 * mirror_lifetime is decremented each send, when it reaches
165 * zero the connection is closed in the send callback.
166 * When the close callback comes, wsi_mirror is set to NULL
167 * so a new connection will be opened
169 * start the ball rolling,
170 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
172 lws_callback_on_writable(wsi);
175 case LWS_CALLBACK_CLOSED:
176 lwsl_notice("mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d\n", mirror_lifetime);
180 case LWS_CALLBACK_CLIENT_WRITEABLE:
181 for (n = 0; n < 1; n++) {
182 lws_get_random(lws_get_context(wsi), rands, sizeof(rands));
183 l += sprintf((char *)&buf[LWS_PRE + l],
185 rands[0] & 0xffffff, /* colour */
186 rands[1] & 511, /* x */
187 rands[2] & 255, /* y */
188 (rands[3] & 31) + 1); /* radius */
191 n = lws_write(wsi, &buf[LWS_PRE], l,
192 opts | LWS_WRITE_TEXT);
196 lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
201 if (!mirror_lifetime) {
202 lwsl_info("closing mirror session\n");
205 /* get notified as soon as we can write again */
206 lws_callback_on_writable(wsi);
217 /* list of supported protocols and callbacks */
219 static struct lws_protocols protocols[] = {
221 "dumb-increment-protocol,fake-nonexistant-protocol",
222 callback_dumb_increment,
227 "fake-nonexistant-protocol,lws-mirror-protocol",
232 { NULL, NULL, 0, 0 } /* end */
235 static const struct lws_extension exts[] = {
237 "permessage-deflate",
238 lws_extension_callback_pm_deflate,
239 "permessage-deflate; client_max_window_bits"
243 lws_extension_callback_pm_deflate,
246 { NULL, NULL, NULL /* terminator */ }
251 void sighandler(int sig)
256 static struct option options[] = {
257 { "help", no_argument, NULL, 'h' },
258 { "debug", required_argument, NULL, 'd' },
259 { "port", required_argument, NULL, 'p' },
260 { "ssl", no_argument, NULL, 's' },
261 { "version", required_argument, NULL, 'v' },
262 { "undeflated", no_argument, NULL, 'u' },
263 { "nomux", no_argument, NULL, 'n' },
264 { "longlived", no_argument, NULL, 'l' },
268 static int ratelimit_connects(unsigned int *last, unsigned int secs)
272 gettimeofday(&tv, NULL);
273 if (tv.tv_sec - (*last) < secs)
281 int main(int argc, char **argv)
283 int n = 0, ret = 0, port = 7681, use_ssl = 0, ietf_version = -1;
284 unsigned int rl_dumb = 0, rl_mirror = 0, do_ws = 1;
285 struct lws_context_creation_info info;
286 struct lws_client_connect_info i;
287 struct lws_context *context;
288 const char *prot, *p;
291 memset(&info, 0, sizeof info);
293 lwsl_notice("libwebsockets test client - license LGPL2.1+SLE\n");
294 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
300 n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL);
305 lws_set_log_level(atoi(optarg), NULL);
308 use_ssl = 2; /* 2 = allow selfsigned */
317 ietf_version = atoi(optarg);
333 signal(SIGINT, sighandler);
335 memset(&i, 0, sizeof(i));
338 if (lws_parse_uri(argv[optind], &prot, &i.address, &i.port, &p))
341 /* add back the leading / on path */
343 strncpy(path + 1, p, sizeof(path) - 2);
344 path[sizeof(path) - 1] = '\0';
347 if (!strcmp(prot, "http") || !strcmp(prot, "ws"))
349 if (!strcmp(prot, "https") || !strcmp(prot, "wss"))
353 * create the websockets context. This tracks open connections and
354 * knows how to route any traffic and which protocol version to use,
355 * and if each connection is client or server side.
357 * For this client-only demo, we tell it to not listen on any port.
360 info.port = CONTEXT_PORT_NO_LISTEN;
361 info.protocols = protocols;
365 context = lws_create_context(&info);
366 if (context == NULL) {
367 fprintf(stderr, "Creating libwebsocket context failed\n");
372 i.ssl_connection = use_ssl;
374 i.origin = i.address;
375 i.ietf_version_or_minus_one = ietf_version;
376 i.client_exts = exts;
378 if (!strcmp(prot, "http") || !strcmp(prot, "https")) {
379 lwsl_notice("using %s mode (non-ws)\n", prot);
383 lwsl_notice("using %s mode (ws)\n", prot);
386 * sit there servicing the websocket context to handle incoming
387 * packets, and drawing random circles on the mirror protocol websocket
389 * nothing happens until the client websocket connection is
390 * asynchronously established... calling lws_client_connect() only
391 * instantiates the connection logically, lws_service() progresses it
395 while (!force_exit) {
398 if (!wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) {
399 lwsl_notice("dumb: connecting\n");
400 i.protocol = protocols[PROTOCOL_DUMB_INCREMENT].name;
401 wsi_dumb = lws_client_connect_via_info(&i);
404 if (!wsi_mirror && ratelimit_connects(&rl_mirror, 2u)) {
405 lwsl_notice("mirror: connecting\n");
406 i.protocol = protocols[PROTOCOL_LWS_MIRROR].name;
407 wsi_mirror = lws_client_connect_via_info(&i);
410 if (!wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) {
411 lwsl_notice("http: connecting\n");
412 wsi_dumb = lws_client_connect_via_info(&i);
415 lws_service(context, 500);
418 lwsl_err("Exiting\n");
419 lws_context_destroy(context);
424 fprintf(stderr, "Usage: libwebsockets-test-client "
425 "<server address> [--port=<p>] "
426 "[--ssl] [-k] [-v <ver>] "
427 "[-d <log bitfield>] [-l]\n");