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,
29 #include "../lib/libwebsockets.h"
33 * This demo shows how to connect multiple websockets simultaneously to a
34 * websocket server (there is no restriction on their having to be the same
35 * server just it simplifies the demo).
37 * dumb-increment-protocol: we connect to the server and print the number
40 * lws-mirror-protocol: draws random circles, which are mirrored on to every
41 * client (see them being drawn in every browser
42 * session also using the test server)
47 PROTOCOL_DUMB_INCREMENT,
55 /* dumb_increment protocol */
58 callback_dumb_increment(struct libwebsocket *wsi,
59 enum libwebsocket_callback_reasons reason,
60 void *user, void *in, size_t len)
64 case LWS_CALLBACK_CLIENT_RECEIVE:
65 ((char *)in)[len] = '\0';
66 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
77 /* lws-mirror_protocol */
81 callback_lws_mirror(struct libwebsocket *wsi,
82 enum libwebsocket_callback_reasons reason,
83 void *user, void *in, size_t len)
85 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
86 LWS_SEND_BUFFER_POST_PADDING];
91 case LWS_CALLBACK_CLIENT_ESTABLISHED:
94 * start the ball rolling,
95 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
98 libwebsocket_callback_on_writable(wsi);
101 case LWS_CALLBACK_CLIENT_RECEIVE:
102 /* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
105 case LWS_CALLBACK_CLIENT_WRITEABLE:
107 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
109 (int)random() & 0xffffff,
114 libwebsocket_write(wsi,
115 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, LWS_WRITE_TEXT);
117 /* get notified as soon as we can write again */
119 libwebsocket_callback_on_writable(wsi);
122 * without at least this delay, we choke the browser
123 * and the connection stalls, despite we now take care about
138 /* list of supported protocols and callbacks */
140 static struct libwebsocket_protocols protocols[] = {
142 [PROTOCOL_DUMB_INCREMENT] = {
143 .name = "dumb-increment-protocol",
144 .callback = callback_dumb_increment,
146 [PROTOCOL_LWS_MIRROR] = {
147 .name = "lws-mirror-protocol",
148 .callback = callback_lws_mirror,
150 [DEMO_PROTOCOL_COUNT] = { /* end of list */
155 static struct option options[] = {
156 { "help", no_argument, NULL, 'h' },
157 { "port", required_argument, NULL, 'p' },
158 { "ssl", no_argument, NULL, 's' },
163 int main(int argc, char **argv)
168 struct libwebsocket_context *context;
169 const char *address = argv[1];
170 struct libwebsocket *wsi_dumb;
171 struct libwebsocket *wsi_mirror;
174 fprintf(stderr, "libwebsockets test client\n"
175 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
176 "licensed under LGPL2.1\n");
184 n = getopt_long(argc, argv, "hsp:", options, NULL);
189 use_ssl = 2; /* 2 = allow selfsigned */
200 * create the websockets context. This tracks open connections and
201 * knows how to route any traffic and which protocol version to use,
202 * and if each connection is client or server side.
204 * For this client-only demo, we tell it to not listen on any port.
207 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN,
208 protocols, NULL, NULL, -1, -1);
209 if (context == NULL) {
210 fprintf(stderr, "Creating libwebsocket context failed\n");
215 /* create a client websocket using dumb increment protocol */
217 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
218 "/", "http://host", "origin",
219 protocols[PROTOCOL_DUMB_INCREMENT].name);
221 if (wsi_dumb == NULL) {
222 fprintf(stderr, "libwebsocket dumb connect failed\n");
226 /* create a client websocket using mirror protocol */
228 wsi_mirror = libwebsocket_client_connect(context, address, port,
229 use_ssl, "/", "http://host", "origin",
230 protocols[PROTOCOL_LWS_MIRROR].name);
232 if (wsi_mirror == NULL) {
233 fprintf(stderr, "libwebsocket dumb connect failed\n");
237 fprintf(stderr, "Websocket connections opened\n");
240 * sit there servicing the websocket context to handle incoming
241 * packets, and drawing random circles on the mirror protocol websocket
246 n = libwebsocket_service(context, 1000);
248 libwebsocket_client_close(wsi_dumb);
249 libwebsocket_client_close(wsi_mirror);
251 libwebsocket_context_destroy(context);
256 fprintf(stderr, "Usage: libwebsockets-test-client "
257 "<server address> [--port=<p>] "