2 * libwebsockets-test-client - libwebsockets test implementation
4 * Copyright (C) 2011 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28 #include "../lib/libwebsockets.h"
30 static unsigned int opts;
31 static int was_closed;
32 static int deny_deflate;
33 static struct libwebsocket *wsi_mirror;
36 * This demo shows how to connect multiple websockets simultaneously to a
37 * websocket server (there is no restriction on their having to be the same
38 * server just it simplifies the demo).
40 * dumb-increment-protocol: we connect to the server and print the number
43 * lws-mirror-protocol: draws random circles, which are mirrored on to every
44 * client (see them being drawn in every browser
45 * session also using the test server)
50 PROTOCOL_DUMB_INCREMENT,
58 /* dumb_increment protocol */
61 callback_dumb_increment(struct libwebsocket_context * this,
62 struct libwebsocket *wsi,
63 enum libwebsocket_callback_reasons reason,
64 void *user, void *in, size_t len)
68 case LWS_CALLBACK_CLOSED:
69 fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
73 case LWS_CALLBACK_CLIENT_RECEIVE:
74 ((char *)in)[len] = '\0';
75 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
78 /* because we are protocols[0] ... */
80 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
81 if (strcmp(in, "deflate-stream") == 0)
83 fprintf(stderr, "denied deflate-stream extension\n");
96 /* lws-mirror_protocol */
100 callback_lws_mirror(struct libwebsocket_context * this,
101 struct libwebsocket *wsi,
102 enum libwebsocket_callback_reasons reason,
103 void *user, void *in, size_t len)
105 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
106 LWS_SEND_BUFFER_POST_PADDING];
111 case LWS_CALLBACK_CLOSED:
112 fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED\n");
116 case LWS_CALLBACK_CLIENT_ESTABLISHED:
119 * start the ball rolling,
120 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
123 libwebsocket_callback_on_writable(this, wsi);
126 case LWS_CALLBACK_CLIENT_RECEIVE:
127 /* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
130 case LWS_CALLBACK_CLIENT_WRITEABLE:
132 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
134 (int)random() & 0xffffff,
139 libwebsocket_write(wsi,
140 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
142 /* get notified as soon as we can write again */
144 libwebsocket_callback_on_writable(this, wsi);
147 * without at least this delay, we choke the browser
148 * and the connection stalls, despite we now take care about
163 /* list of supported protocols and callbacks */
165 static struct libwebsocket_protocols protocols[] = {
167 "dumb-increment-protocol",
168 callback_dumb_increment,
172 "lws-mirror-protocol",
183 static struct option options[] = {
184 { "help", no_argument, NULL, 'h' },
185 { "port", required_argument, NULL, 'p' },
186 { "ssl", no_argument, NULL, 's' },
187 { "killmask", no_argument, NULL, 'k' },
188 { "version", required_argument, NULL, 'v' },
189 { "undeflated", no_argument, NULL, 'u' },
194 int main(int argc, char **argv)
199 struct libwebsocket_context *context;
201 struct libwebsocket *wsi_dumb;
202 int ietf_version = -1; /* latest */
203 int mirror_lifetime = 0;
205 fprintf(stderr, "libwebsockets test client\n"
206 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
207 "licensed under LGPL2.1\n");
213 n = getopt_long(argc, argv, "uv:khsp:", options, NULL);
218 use_ssl = 2; /* 2 = allow selfsigned */
224 opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
227 ietf_version = atoi(optarg);
240 address = argv[optind];
243 * create the websockets context. This tracks open connections and
244 * knows how to route any traffic and which protocol version to use,
245 * and if each connection is client or server side.
247 * For this client-only demo, we tell it to not listen on any port.
250 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
251 protocols, libwebsocket_internal_extensions,
252 NULL, NULL, -1, -1, 0);
253 if (context == NULL) {
254 fprintf(stderr, "Creating libwebsocket context failed\n");
259 /* create a client websocket using dumb increment protocol */
261 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
262 "/", argv[optind], argv[optind],
263 protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
265 if (wsi_dumb == NULL) {
266 fprintf(stderr, "libwebsocket dumb connect failed\n");
270 fprintf(stderr, "Websocket connections opened\n");
273 * sit there servicing the websocket context to handle incoming
274 * packets, and drawing random circles on the mirror protocol websocket
278 while (n >= 0 && !was_closed) {
279 n = libwebsocket_service(context, 1000);
281 if (wsi_mirror == NULL) {
283 /* create a client websocket using mirror protocol */
285 wsi_mirror = libwebsocket_client_connect(context, address, port,
286 use_ssl, "/", argv[optind], argv[optind],
287 protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
289 if (wsi_mirror == NULL) {
290 fprintf(stderr, "libwebsocket dumb connect failed\n");
294 mirror_lifetime = 10 + (random() & 1023);
296 fprintf(stderr, "opened mirror connection with %d lifetime\n", mirror_lifetime);
301 if (mirror_lifetime == 0) {
302 fprintf(stderr, "closing mirror session\n");
303 libwebsocket_close_and_free_session(context,
304 wsi_mirror, LWS_CLOSE_STATUS_GOINGAWAY);
307 * wsi_mirror will get set to NULL in
308 * callback when close completes
314 fprintf(stderr, "Exiting\n");
316 libwebsocket_context_destroy(context);
321 fprintf(stderr, "Usage: libwebsockets-test-client "
322 "<server address> [--port=<p>] "
323 "[--ssl] [-k] [-v <ver>]\n");