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"
31 static unsigned int opts;
34 * This demo shows how to connect multiple websockets simultaneously to a
35 * websocket server (there is no restriction on their having to be the same
36 * server just it simplifies the demo).
38 * dumb-increment-protocol: we connect to the server and print the number
41 * lws-mirror-protocol: draws random circles, which are mirrored on to every
42 * client (see them being drawn in every browser
43 * session also using the test server)
48 PROTOCOL_DUMB_INCREMENT,
56 /* dumb_increment protocol */
59 callback_dumb_increment(struct libwebsocket_context * this,
60 struct libwebsocket *wsi,
61 enum libwebsocket_callback_reasons reason,
62 void *user, void *in, size_t len)
66 case LWS_CALLBACK_CLIENT_RECEIVE:
67 ((char *)in)[len] = '\0';
68 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
79 /* lws-mirror_protocol */
83 callback_lws_mirror(struct libwebsocket_context * this,
84 struct libwebsocket *wsi,
85 enum libwebsocket_callback_reasons reason,
86 void *user, void *in, size_t len)
88 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
89 LWS_SEND_BUFFER_POST_PADDING];
94 case LWS_CALLBACK_CLIENT_ESTABLISHED:
97 * start the ball rolling,
98 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
101 libwebsocket_callback_on_writable(this, wsi);
104 case LWS_CALLBACK_CLIENT_RECEIVE:
105 /* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
108 case LWS_CALLBACK_CLIENT_WRITEABLE:
110 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
112 (int)random() & 0xffffff,
117 libwebsocket_write(wsi,
118 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
120 /* get notified as soon as we can write again */
122 libwebsocket_callback_on_writable(this, wsi);
125 * without at least this delay, we choke the browser
126 * and the connection stalls, despite we now take care about
141 /* list of supported protocols and callbacks */
143 static struct libwebsocket_protocols protocols[] = {
145 [PROTOCOL_DUMB_INCREMENT] = {
146 .name = "dumb-increment-protocol",
147 .callback = callback_dumb_increment,
149 [PROTOCOL_LWS_MIRROR] = {
150 .name = "lws-mirror-protocol",
151 .callback = callback_lws_mirror,
153 [DEMO_PROTOCOL_COUNT] = { /* end of list */
158 static struct option options[] = {
159 { "help", no_argument, NULL, 'h' },
160 { "port", required_argument, NULL, 'p' },
161 { "ssl", no_argument, NULL, 's' },
162 { "killmask", no_argument, NULL, 'k' },
163 { "version", required_argument, NULL, 'v' },
168 int main(int argc, char **argv)
173 struct libwebsocket_context *context;
175 struct libwebsocket *wsi_dumb;
176 struct libwebsocket *wsi_mirror;
177 int ietf_version = -1; /* latest */
179 fprintf(stderr, "libwebsockets test client\n"
180 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
181 "licensed under LGPL2.1\n");
187 n = getopt_long(argc, argv, "v:khsp:", options, NULL);
192 use_ssl = 2; /* 2 = allow selfsigned */
198 opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
201 ietf_version = atoi(optarg);
211 address = argv[optind];
214 * create the websockets context. This tracks open connections and
215 * knows how to route any traffic and which protocol version to use,
216 * and if each connection is client or server side.
218 * For this client-only demo, we tell it to not listen on any port.
221 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN,
222 protocols, NULL, NULL, -1, -1, 0);
223 if (context == NULL) {
224 fprintf(stderr, "Creating libwebsocket context failed\n");
229 /* create a client websocket using dumb increment protocol */
231 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
232 "/", argv[optind], argv[optind],
233 protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
235 if (wsi_dumb == NULL) {
236 fprintf(stderr, "libwebsocket dumb connect failed\n");
240 /* create a client websocket using mirror protocol */
242 wsi_mirror = libwebsocket_client_connect(context, address, port,
243 use_ssl, "/", argv[optind], argv[optind],
244 protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
246 if (wsi_mirror == NULL) {
247 fprintf(stderr, "libwebsocket dumb connect failed\n");
251 fprintf(stderr, "Websocket connections opened\n");
254 * sit there servicing the websocket context to handle incoming
255 * packets, and drawing random circles on the mirror protocol websocket
260 n = libwebsocket_service(context, 1000);
262 libwebsocket_context_destroy(context);
267 fprintf(stderr, "Usage: libwebsockets-test-client "
268 "<server address> [--port=<p>] "
269 "[--ssl] [-k] [-v <ver>]\n");