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 *wsi,
60 enum libwebsocket_callback_reasons reason,
61 void *user, void *in, size_t len)
65 case LWS_CALLBACK_CLIENT_RECEIVE:
66 ((char *)in)[len] = '\0';
67 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
78 /* lws-mirror_protocol */
82 callback_lws_mirror(struct libwebsocket *wsi,
83 enum libwebsocket_callback_reasons reason,
84 void *user, void *in, size_t len)
86 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
87 LWS_SEND_BUFFER_POST_PADDING];
92 case LWS_CALLBACK_CLIENT_ESTABLISHED:
95 * start the ball rolling,
96 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
99 libwebsocket_callback_on_writable(wsi);
102 case LWS_CALLBACK_CLIENT_RECEIVE:
103 /* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
106 case LWS_CALLBACK_CLIENT_WRITEABLE:
108 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
110 (int)random() & 0xffffff,
115 libwebsocket_write(wsi,
116 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
118 /* get notified as soon as we can write again */
120 libwebsocket_callback_on_writable(wsi);
123 * without at least this delay, we choke the browser
124 * and the connection stalls, despite we now take care about
139 /* list of supported protocols and callbacks */
141 static struct libwebsocket_protocols protocols[] = {
143 [PROTOCOL_DUMB_INCREMENT] = {
144 .name = "dumb-increment-protocol",
145 .callback = callback_dumb_increment,
147 [PROTOCOL_LWS_MIRROR] = {
148 .name = "lws-mirror-protocol",
149 .callback = callback_lws_mirror,
151 [DEMO_PROTOCOL_COUNT] = { /* end of list */
156 static struct option options[] = {
157 { "help", no_argument, NULL, 'h' },
158 { "port", required_argument, NULL, 'p' },
159 { "ssl", no_argument, NULL, 's' },
160 { "killmask", no_argument, NULL, 'k' },
161 { "version", required_argument, NULL, 'v' },
166 int main(int argc, char **argv)
171 struct libwebsocket_context *context;
173 struct libwebsocket *wsi_dumb;
174 struct libwebsocket *wsi_mirror;
175 int ietf_version = -1; /* latest */
177 fprintf(stderr, "libwebsockets test client\n"
178 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
179 "licensed under LGPL2.1\n");
185 n = getopt_long(argc, argv, "v:khsp:", options, NULL);
190 use_ssl = 2; /* 2 = allow selfsigned */
196 opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
199 ietf_version = atoi(optarg);
209 address = argv[optind];
212 * create the websockets context. This tracks open connections and
213 * knows how to route any traffic and which protocol version to use,
214 * and if each connection is client or server side.
216 * For this client-only demo, we tell it to not listen on any port.
219 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN,
220 protocols, NULL, NULL, -1, -1, 0);
221 if (context == NULL) {
222 fprintf(stderr, "Creating libwebsocket context failed\n");
227 /* create a client websocket using dumb increment protocol */
229 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
230 "/", argv[optind], argv[optind],
231 protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
233 if (wsi_dumb == NULL) {
234 fprintf(stderr, "libwebsocket dumb connect failed\n");
238 /* create a client websocket using mirror protocol */
240 wsi_mirror = libwebsocket_client_connect(context, address, port,
241 use_ssl, "/", argv[optind], argv[optind],
242 protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
244 if (wsi_mirror == NULL) {
245 fprintf(stderr, "libwebsocket dumb connect failed\n");
249 fprintf(stderr, "Websocket connections opened\n");
252 * sit there servicing the websocket context to handle incoming
253 * packets, and drawing random circles on the mirror protocol websocket
258 n = libwebsocket_service(context, 1000);
260 libwebsocket_context_destroy(context);
265 fprintf(stderr, "Usage: libwebsockets-test-client "
266 "<server address> [--port=<p>] "