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;
35 * This demo shows how to connect multiple websockets simultaneously to a
36 * websocket server (there is no restriction on their having to be the same
37 * server just it simplifies the demo).
39 * dumb-increment-protocol: we connect to the server and print the number
42 * lws-mirror-protocol: draws random circles, which are mirrored on to every
43 * client (see them being drawn in every browser
44 * session also using the test server)
49 PROTOCOL_DUMB_INCREMENT,
57 /* dumb_increment protocol */
60 callback_dumb_increment(struct libwebsocket_context * this,
61 struct libwebsocket *wsi,
62 enum libwebsocket_callback_reasons reason,
63 void *user, void *in, size_t len)
67 case LWS_CALLBACK_CLIENT_RECEIVE:
68 ((char *)in)[len] = '\0';
69 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
72 /* because we are protocols[0] ... */
74 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
75 if (strcmp(in, "deflate-stream") == 0)
77 fprintf(stderr, "denied deflate-stream extension\n");
90 /* lws-mirror_protocol */
94 callback_lws_mirror(struct libwebsocket_context * this,
95 struct libwebsocket *wsi,
96 enum libwebsocket_callback_reasons reason,
97 void *user, void *in, size_t len)
99 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
100 LWS_SEND_BUFFER_POST_PADDING];
105 case LWS_CALLBACK_CLOSED:
106 fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
110 case LWS_CALLBACK_CLIENT_ESTABLISHED:
113 * start the ball rolling,
114 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
117 libwebsocket_callback_on_writable(this, wsi);
120 case LWS_CALLBACK_CLIENT_RECEIVE:
121 /* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
124 case LWS_CALLBACK_CLIENT_WRITEABLE:
126 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
128 (int)random() & 0xffffff,
133 libwebsocket_write(wsi,
134 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
136 /* get notified as soon as we can write again */
138 libwebsocket_callback_on_writable(this, wsi);
141 * without at least this delay, we choke the browser
142 * and the connection stalls, despite we now take care about
157 /* list of supported protocols and callbacks */
159 static struct libwebsocket_protocols protocols[] = {
161 "dumb-increment-protocol",
162 callback_dumb_increment,
166 "lws-mirror-protocol",
177 static struct option options[] = {
178 { "help", no_argument, NULL, 'h' },
179 { "port", required_argument, NULL, 'p' },
180 { "ssl", no_argument, NULL, 's' },
181 { "killmask", no_argument, NULL, 'k' },
182 { "version", required_argument, NULL, 'v' },
183 { "undeflated", no_argument, NULL, 'u' },
188 int main(int argc, char **argv)
193 struct libwebsocket_context *context;
195 struct libwebsocket *wsi_dumb;
196 struct libwebsocket *wsi_mirror;
197 int ietf_version = -1; /* latest */
199 fprintf(stderr, "libwebsockets test client\n"
200 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
201 "licensed under LGPL2.1\n");
207 n = getopt_long(argc, argv, "uv:khsp:", options, NULL);
212 use_ssl = 2; /* 2 = allow selfsigned */
218 opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
221 ietf_version = atoi(optarg);
234 address = argv[optind];
237 * create the websockets context. This tracks open connections and
238 * knows how to route any traffic and which protocol version to use,
239 * and if each connection is client or server side.
241 * For this client-only demo, we tell it to not listen on any port.
244 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
245 protocols, libwebsocket_internal_extensions,
246 NULL, NULL, -1, -1, 0);
247 if (context == NULL) {
248 fprintf(stderr, "Creating libwebsocket context failed\n");
253 /* create a client websocket using dumb increment protocol */
255 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
256 "/", argv[optind], argv[optind],
257 protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
259 if (wsi_dumb == NULL) {
260 fprintf(stderr, "libwebsocket dumb connect failed\n");
264 /* create a client websocket using mirror protocol */
266 wsi_mirror = libwebsocket_client_connect(context, address, port,
267 use_ssl, "/", argv[optind], argv[optind],
268 protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
270 if (wsi_mirror == NULL) {
271 fprintf(stderr, "libwebsocket dumb connect failed\n");
275 fprintf(stderr, "Websocket connections opened\n");
278 * sit there servicing the websocket context to handle incoming
279 * packets, and drawing random circles on the mirror protocol websocket
283 while (n >= 0 && !was_closed)
284 n = libwebsocket_service(context, 1000);
286 fprintf(stderr, "Exiting\n");
288 libwebsocket_context_destroy(context);
293 fprintf(stderr, "Usage: libwebsockets-test-client "
294 "<server address> [--port=<p>] "
295 "[--ssl] [-k] [-v <ver>]\n");