blob: c1abff705b5f9511a0c82090bcd8983ae69f000d [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 Green990d5062011-01-30 21:04:24 +000032
Andy Green4739e5c2011-01-22 12:51:57 +000033/*
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).
37 *
38 * dumb-increment-protocol: we connect to the server and print the number
Andy Green90c7cbc2011-01-27 06:26:52 +000039 * we are given
Andy Green4739e5c2011-01-22 12:51:57 +000040 *
41 * lws-mirror-protocol: draws random circles, which are mirrored on to every
Andy Green90c7cbc2011-01-27 06:26:52 +000042 * client (see them being drawn in every browser
43 * session also using the test server)
Andy Green4739e5c2011-01-22 12:51:57 +000044 */
45
46enum demo_protocols {
47
48 PROTOCOL_DUMB_INCREMENT,
49 PROTOCOL_LWS_MIRROR,
50
51 /* always last */
52 DEMO_PROTOCOL_COUNT
53};
54
55
56/* dumb_increment protocol */
57
58static int
Andy Green62c54d22011-02-14 09:14:25 +000059callback_dumb_increment(struct libwebsocket_context * this,
60 struct libwebsocket *wsi,
Andy Green4739e5c2011-01-22 12:51:57 +000061 enum libwebsocket_callback_reasons reason,
62 void *user, void *in, size_t len)
63{
64 switch (reason) {
65
66 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +000067 ((char *)in)[len] = '\0';
68 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
Andy Green4739e5c2011-01-22 12:51:57 +000069 break;
70
71 default:
72 break;
73 }
74
75 return 0;
76}
77
78
79/* lws-mirror_protocol */
80
Andy Green4739e5c2011-01-22 12:51:57 +000081
82static int
Andy Green62c54d22011-02-14 09:14:25 +000083callback_lws_mirror(struct libwebsocket_context * this,
84 struct libwebsocket *wsi,
Andy Green4739e5c2011-01-22 12:51:57 +000085 enum libwebsocket_callback_reasons reason,
86 void *user, void *in, size_t len)
87{
Andy Green90c7cbc2011-01-27 06:26:52 +000088 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
89 LWS_SEND_BUFFER_POST_PADDING];
90 int l;
91
Andy Green4739e5c2011-01-22 12:51:57 +000092 switch (reason) {
93
Andy Greenb3ae0a32011-02-14 20:25:43 +000094 case LWS_CALLBACK_CLOSED:
95 fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
96 was_closed = 1;
97 break;
98
Andy Green90c7cbc2011-01-27 06:26:52 +000099 case LWS_CALLBACK_CLIENT_ESTABLISHED:
100
101 /*
102 * start the ball rolling,
Andy Greenb6e6ebe2011-01-27 06:36:39 +0000103 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
Andy Green90c7cbc2011-01-27 06:26:52 +0000104 */
105
Andy Green62c54d22011-02-14 09:14:25 +0000106 libwebsocket_callback_on_writable(this, wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +0000107 break;
108
Andy Green4739e5c2011-01-22 12:51:57 +0000109 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +0000110/* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
111 break;
112
113 case LWS_CALLBACK_CLIENT_WRITEABLE:
114
115 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
116 "c #%06X %d %d %d;",
117 (int)random() & 0xffffff,
118 (int)random() % 500,
119 (int)random() % 250,
120 (int)random() % 24);
121
122 libwebsocket_write(wsi,
Andy Green990d5062011-01-30 21:04:24 +0000123 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
Andy Green90c7cbc2011-01-27 06:26:52 +0000124
125 /* get notified as soon as we can write again */
126
Andy Green62c54d22011-02-14 09:14:25 +0000127 libwebsocket_callback_on_writable(this, wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +0000128
Andy Greenb6e6ebe2011-01-27 06:36:39 +0000129 /*
130 * without at least this delay, we choke the browser
131 * and the connection stalls, despite we now take care about
132 * flow control
133 */
134
Andy Green90c7cbc2011-01-27 06:26:52 +0000135 usleep(200);
Andy Green4739e5c2011-01-22 12:51:57 +0000136 break;
137
138 default:
139 break;
140 }
141
142 return 0;
143}
144
145
146/* list of supported protocols and callbacks */
147
148static struct libwebsocket_protocols protocols[] = {
Peter Hinz56885f32011-03-02 22:03:47 +0000149 {
150 "dumb-increment-protocol",
151 callback_dumb_increment,
152 0,
Andy Green4739e5c2011-01-22 12:51:57 +0000153 },
Peter Hinz56885f32011-03-02 22:03:47 +0000154 {
155 "lws-mirror-protocol",
156 callback_lws_mirror,
157 0,
Andy Green4739e5c2011-01-22 12:51:57 +0000158 },
Peter Hinz56885f32011-03-02 22:03:47 +0000159 { /* end of list */
160 NULL,
161 NULL,
162 0
Andy Green4739e5c2011-01-22 12:51:57 +0000163 }
164};
165
166static struct option options[] = {
Andy Green90c7cbc2011-01-27 06:26:52 +0000167 { "help", no_argument, NULL, 'h' },
168 { "port", required_argument, NULL, 'p' },
169 { "ssl", no_argument, NULL, 's' },
Andy Green990d5062011-01-30 21:04:24 +0000170 { "killmask", no_argument, NULL, 'k' },
Andy Greenbfb051f2011-02-09 08:49:14 +0000171 { "version", required_argument, NULL, 'v' },
Andy Green4739e5c2011-01-22 12:51:57 +0000172 { NULL, 0, 0, 0 }
173};
174
175
176int main(int argc, char **argv)
177{
178 int n = 0;
179 int port = 7681;
180 int use_ssl = 0;
181 struct libwebsocket_context *context;
Andy Green1efb63c2011-02-06 17:42:06 +0000182 const char *address;
Andy Green4739e5c2011-01-22 12:51:57 +0000183 struct libwebsocket *wsi_dumb;
184 struct libwebsocket *wsi_mirror;
Andy Greenbfb051f2011-02-09 08:49:14 +0000185 int ietf_version = -1; /* latest */
Andy Green90c7cbc2011-01-27 06:26:52 +0000186
Andy Green4739e5c2011-01-22 12:51:57 +0000187 fprintf(stderr, "libwebsockets test client\n"
188 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
189 "licensed under LGPL2.1\n");
190
191 if (argc < 2)
192 goto usage;
193
Andy Green4739e5c2011-01-22 12:51:57 +0000194 while (n >= 0) {
Andy Greenbfb051f2011-02-09 08:49:14 +0000195 n = getopt_long(argc, argv, "v:khsp:", options, NULL);
Andy Green4739e5c2011-01-22 12:51:57 +0000196 if (n < 0)
197 continue;
198 switch (n) {
199 case 's':
Andy Green90c7cbc2011-01-27 06:26:52 +0000200 use_ssl = 2; /* 2 = allow selfsigned */
Andy Green4739e5c2011-01-22 12:51:57 +0000201 break;
202 case 'p':
203 port = atoi(optarg);
204 break;
Andy Green990d5062011-01-30 21:04:24 +0000205 case 'k':
206 opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
207 break;
Andy Greenbfb051f2011-02-09 08:49:14 +0000208 case 'v':
209 ietf_version = atoi(optarg);
210 break;
Andy Green4739e5c2011-01-22 12:51:57 +0000211 case 'h':
212 goto usage;
213 }
214 }
215
Andy Green1efb63c2011-02-06 17:42:06 +0000216 if (optind >= argc)
217 goto usage;
218
219 address = argv[optind];
220
Andy Green4739e5c2011-01-22 12:51:57 +0000221 /*
222 * create the websockets context. This tracks open connections and
223 * knows how to route any traffic and which protocol version to use,
224 * and if each connection is client or server side.
225 *
226 * For this client-only demo, we tell it to not listen on any port.
227 */
228
Andy Green32375b72011-02-19 08:32:53 +0000229 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
Andy Green8014b292011-01-30 20:57:25 +0000230 protocols, NULL, NULL, -1, -1, 0);
Andy Green4739e5c2011-01-22 12:51:57 +0000231 if (context == NULL) {
232 fprintf(stderr, "Creating libwebsocket context failed\n");
233 return 1;
234 }
235
236
237 /* create a client websocket using dumb increment protocol */
238
Andy Green90c7cbc2011-01-27 06:26:52 +0000239 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
Andy Green1efb63c2011-02-06 17:42:06 +0000240 "/", argv[optind], argv[optind],
Andy Greenbfb051f2011-02-09 08:49:14 +0000241 protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
Andy Green4739e5c2011-01-22 12:51:57 +0000242
243 if (wsi_dumb == NULL) {
244 fprintf(stderr, "libwebsocket dumb connect failed\n");
245 return -1;
246 }
247
248 /* create a client websocket using mirror protocol */
249
Andy Green90c7cbc2011-01-27 06:26:52 +0000250 wsi_mirror = libwebsocket_client_connect(context, address, port,
Andy Green1efb63c2011-02-06 17:42:06 +0000251 use_ssl, "/", argv[optind], argv[optind],
Andy Greenbfb051f2011-02-09 08:49:14 +0000252 protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
Andy Green4739e5c2011-01-22 12:51:57 +0000253
254 if (wsi_mirror == NULL) {
255 fprintf(stderr, "libwebsocket dumb connect failed\n");
256 return -1;
257 }
258
259 fprintf(stderr, "Websocket connections opened\n");
260
261 /*
262 * sit there servicing the websocket context to handle incoming
263 * packets, and drawing random circles on the mirror protocol websocket
264 */
265
266 n = 0;
Andy Greenb3ae0a32011-02-14 20:25:43 +0000267 while (n >= 0 && !was_closed)
Andy Green90c7cbc2011-01-27 06:26:52 +0000268 n = libwebsocket_service(context, 1000);
Andy Green4739e5c2011-01-22 12:51:57 +0000269
Andy Greenb3ae0a32011-02-14 20:25:43 +0000270 fprintf(stderr, "Exiting\n");
271
Andy Green6964bb52011-01-23 16:50:33 +0000272 libwebsocket_context_destroy(context);
273
Andy Green4739e5c2011-01-22 12:51:57 +0000274 return 0;
275
276usage:
277 fprintf(stderr, "Usage: libwebsockets-test-client "
278 "<server address> [--port=<p>] "
Andy Green5e1fa172011-02-10 09:07:05 +0000279 "[--ssl] [-k] [-v <ver>]\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000280 return 1;
281}