blob: 4c5dbab4fff5f54882f6955b7c332990f0940339 [file] [log] [blame]
Andy Green4739e5c2011-01-22 12:51:57 +00001/*
2 * libwebsockets-test-client - libwebsockets test implementation
3 *
4 * Copyright (C) 2011 Andy Green <andy@warmcat.com>
5 *
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.
10 *
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.
15 *
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,
19 * MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <getopt.h>
26#include <string.h>
27
Andy Green4739e5c2011-01-22 12:51:57 +000028#include "../lib/libwebsockets.h"
Andy Green4739e5c2011-01-22 12:51:57 +000029
Andy Green990d5062011-01-30 21:04:24 +000030static unsigned int opts;
Andy Greenb3ae0a32011-02-14 20:25:43 +000031static int was_closed;
Andy Green775884e2011-03-06 13:32:53 +000032static int deny_deflate;
Andy Green990d5062011-01-30 21:04:24 +000033
Andy Green4739e5c2011-01-22 12:51:57 +000034/*
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).
38 *
39 * dumb-increment-protocol: we connect to the server and print the number
Andy Green90c7cbc2011-01-27 06:26:52 +000040 * we are given
Andy Green4739e5c2011-01-22 12:51:57 +000041 *
42 * lws-mirror-protocol: draws random circles, which are mirrored on to every
Andy Green90c7cbc2011-01-27 06:26:52 +000043 * client (see them being drawn in every browser
44 * session also using the test server)
Andy Green4739e5c2011-01-22 12:51:57 +000045 */
46
47enum demo_protocols {
48
49 PROTOCOL_DUMB_INCREMENT,
50 PROTOCOL_LWS_MIRROR,
51
52 /* always last */
53 DEMO_PROTOCOL_COUNT
54};
55
56
57/* dumb_increment protocol */
58
59static int
Andy Green62c54d22011-02-14 09:14:25 +000060callback_dumb_increment(struct libwebsocket_context * this,
61 struct libwebsocket *wsi,
Andy Green4739e5c2011-01-22 12:51:57 +000062 enum libwebsocket_callback_reasons reason,
63 void *user, void *in, size_t len)
64{
65 switch (reason) {
66
67 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +000068 ((char *)in)[len] = '\0';
69 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
Andy Green4739e5c2011-01-22 12:51:57 +000070 break;
71
Andy Green775884e2011-03-06 13:32:53 +000072 /* because we are protocols[0] ... */
73
74 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
75 if (strcmp(in, "deflate-stream") == 0)
76 if (deny_deflate) {
77 fprintf(stderr, "denied deflate-stream extension\n");
78 return 1;
79 }
80 break;
81
Andy Green4739e5c2011-01-22 12:51:57 +000082 default:
83 break;
84 }
85
86 return 0;
87}
88
89
90/* lws-mirror_protocol */
91
Andy Green4739e5c2011-01-22 12:51:57 +000092
93static int
Andy Green62c54d22011-02-14 09:14:25 +000094callback_lws_mirror(struct libwebsocket_context * this,
95 struct libwebsocket *wsi,
Andy Green4739e5c2011-01-22 12:51:57 +000096 enum libwebsocket_callback_reasons reason,
97 void *user, void *in, size_t len)
98{
Andy Green90c7cbc2011-01-27 06:26:52 +000099 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
100 LWS_SEND_BUFFER_POST_PADDING];
101 int l;
102
Andy Green4739e5c2011-01-22 12:51:57 +0000103 switch (reason) {
104
Andy Greenb3ae0a32011-02-14 20:25:43 +0000105 case LWS_CALLBACK_CLOSED:
106 fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
107 was_closed = 1;
108 break;
109
Andy Green90c7cbc2011-01-27 06:26:52 +0000110 case LWS_CALLBACK_CLIENT_ESTABLISHED:
111
112 /*
113 * start the ball rolling,
Andy Greenb6e6ebe2011-01-27 06:36:39 +0000114 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
Andy Green90c7cbc2011-01-27 06:26:52 +0000115 */
116
Andy Green62c54d22011-02-14 09:14:25 +0000117 libwebsocket_callback_on_writable(this, wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +0000118 break;
119
Andy Green4739e5c2011-01-22 12:51:57 +0000120 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +0000121/* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
122 break;
123
124 case LWS_CALLBACK_CLIENT_WRITEABLE:
125
126 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
127 "c #%06X %d %d %d;",
128 (int)random() & 0xffffff,
129 (int)random() % 500,
130 (int)random() % 250,
131 (int)random() % 24);
132
133 libwebsocket_write(wsi,
Andy Green990d5062011-01-30 21:04:24 +0000134 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
Andy Green90c7cbc2011-01-27 06:26:52 +0000135
136 /* get notified as soon as we can write again */
137
Andy Green62c54d22011-02-14 09:14:25 +0000138 libwebsocket_callback_on_writable(this, wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +0000139
Andy Greenb6e6ebe2011-01-27 06:36:39 +0000140 /*
141 * without at least this delay, we choke the browser
142 * and the connection stalls, despite we now take care about
143 * flow control
144 */
145
Andy Green90c7cbc2011-01-27 06:26:52 +0000146 usleep(200);
Andy Green4739e5c2011-01-22 12:51:57 +0000147 break;
148
149 default:
150 break;
151 }
152
153 return 0;
154}
155
156
157/* list of supported protocols and callbacks */
158
159static struct libwebsocket_protocols protocols[] = {
Peter Hinz56885f32011-03-02 22:03:47 +0000160 {
161 "dumb-increment-protocol",
162 callback_dumb_increment,
163 0,
Andy Green4739e5c2011-01-22 12:51:57 +0000164 },
Peter Hinz56885f32011-03-02 22:03:47 +0000165 {
166 "lws-mirror-protocol",
167 callback_lws_mirror,
168 0,
Andy Green4739e5c2011-01-22 12:51:57 +0000169 },
Peter Hinz56885f32011-03-02 22:03:47 +0000170 { /* end of list */
171 NULL,
172 NULL,
173 0
Andy Green4739e5c2011-01-22 12:51:57 +0000174 }
175};
176
177static struct option options[] = {
Andy Green90c7cbc2011-01-27 06:26:52 +0000178 { "help", no_argument, NULL, 'h' },
179 { "port", required_argument, NULL, 'p' },
180 { "ssl", no_argument, NULL, 's' },
Andy Green990d5062011-01-30 21:04:24 +0000181 { "killmask", no_argument, NULL, 'k' },
Andy Greenbfb051f2011-02-09 08:49:14 +0000182 { "version", required_argument, NULL, 'v' },
Andy Green775884e2011-03-06 13:32:53 +0000183 { "undeflated", no_argument, NULL, 'u' },
Andy Green4739e5c2011-01-22 12:51:57 +0000184 { NULL, 0, 0, 0 }
185};
186
187
188int main(int argc, char **argv)
189{
190 int n = 0;
191 int port = 7681;
192 int use_ssl = 0;
193 struct libwebsocket_context *context;
Andy Green1efb63c2011-02-06 17:42:06 +0000194 const char *address;
Andy Green4739e5c2011-01-22 12:51:57 +0000195 struct libwebsocket *wsi_dumb;
196 struct libwebsocket *wsi_mirror;
Andy Greenbfb051f2011-02-09 08:49:14 +0000197 int ietf_version = -1; /* latest */
Andy Green90c7cbc2011-01-27 06:26:52 +0000198
Andy Green4739e5c2011-01-22 12:51:57 +0000199 fprintf(stderr, "libwebsockets test client\n"
200 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
201 "licensed under LGPL2.1\n");
202
203 if (argc < 2)
204 goto usage;
205
Andy Green4739e5c2011-01-22 12:51:57 +0000206 while (n >= 0) {
Andy Green775884e2011-03-06 13:32:53 +0000207 n = getopt_long(argc, argv, "uv:khsp:", options, NULL);
Andy Green4739e5c2011-01-22 12:51:57 +0000208 if (n < 0)
209 continue;
210 switch (n) {
211 case 's':
Andy Green90c7cbc2011-01-27 06:26:52 +0000212 use_ssl = 2; /* 2 = allow selfsigned */
Andy Green4739e5c2011-01-22 12:51:57 +0000213 break;
214 case 'p':
215 port = atoi(optarg);
216 break;
Andy Green990d5062011-01-30 21:04:24 +0000217 case 'k':
218 opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
219 break;
Andy Greenbfb051f2011-02-09 08:49:14 +0000220 case 'v':
221 ietf_version = atoi(optarg);
222 break;
Andy Green775884e2011-03-06 13:32:53 +0000223 case 'u':
224 deny_deflate = 1;
225 break;
Andy Green4739e5c2011-01-22 12:51:57 +0000226 case 'h':
227 goto usage;
228 }
229 }
230
Andy Green1efb63c2011-02-06 17:42:06 +0000231 if (optind >= argc)
232 goto usage;
233
234 address = argv[optind];
235
Andy Green4739e5c2011-01-22 12:51:57 +0000236 /*
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.
240 *
241 * For this client-only demo, we tell it to not listen on any port.
242 */
243
Andy Green32375b72011-02-19 08:32:53 +0000244 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
Andy Green4cd87a02011-03-06 13:15:32 +0000245 protocols, libwebsocket_internal_extensions,
246 NULL, NULL, -1, -1, 0);
Andy Green4739e5c2011-01-22 12:51:57 +0000247 if (context == NULL) {
248 fprintf(stderr, "Creating libwebsocket context failed\n");
249 return 1;
250 }
251
252
253 /* create a client websocket using dumb increment protocol */
254
Andy Green90c7cbc2011-01-27 06:26:52 +0000255 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
Andy Green1efb63c2011-02-06 17:42:06 +0000256 "/", argv[optind], argv[optind],
Andy Greenbfb051f2011-02-09 08:49:14 +0000257 protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
Andy Green4739e5c2011-01-22 12:51:57 +0000258
259 if (wsi_dumb == NULL) {
260 fprintf(stderr, "libwebsocket dumb connect failed\n");
261 return -1;
262 }
263
264 /* create a client websocket using mirror protocol */
265
Andy Green90c7cbc2011-01-27 06:26:52 +0000266 wsi_mirror = libwebsocket_client_connect(context, address, port,
Andy Green1efb63c2011-02-06 17:42:06 +0000267 use_ssl, "/", argv[optind], argv[optind],
Andy Greenbfb051f2011-02-09 08:49:14 +0000268 protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
Andy Green4739e5c2011-01-22 12:51:57 +0000269
270 if (wsi_mirror == NULL) {
271 fprintf(stderr, "libwebsocket dumb connect failed\n");
272 return -1;
273 }
274
275 fprintf(stderr, "Websocket connections opened\n");
276
277 /*
278 * sit there servicing the websocket context to handle incoming
279 * packets, and drawing random circles on the mirror protocol websocket
280 */
281
282 n = 0;
Andy Greenb3ae0a32011-02-14 20:25:43 +0000283 while (n >= 0 && !was_closed)
Andy Green90c7cbc2011-01-27 06:26:52 +0000284 n = libwebsocket_service(context, 1000);
Andy Green4739e5c2011-01-22 12:51:57 +0000285
Andy Greenb3ae0a32011-02-14 20:25:43 +0000286 fprintf(stderr, "Exiting\n");
287
Andy Green6964bb52011-01-23 16:50:33 +0000288 libwebsocket_context_destroy(context);
289
Andy Green4739e5c2011-01-22 12:51:57 +0000290 return 0;
291
292usage:
293 fprintf(stderr, "Usage: libwebsockets-test-client "
294 "<server address> [--port=<p>] "
Andy Green5e1fa172011-02-10 09:07:05 +0000295 "[--ssl] [-k] [-v <ver>]\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000296 return 1;
297}