blob: 59595e03a78e39d9c74516b26cfe197404a8c143 [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"
29#include <poll.h>
30
Andy Green990d5062011-01-30 21:04:24 +000031static unsigned int opts;
32
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
59callback_dumb_increment(struct libwebsocket *wsi,
60 enum libwebsocket_callback_reasons reason,
61 void *user, void *in, size_t len)
62{
63 switch (reason) {
64
65 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +000066 ((char *)in)[len] = '\0';
67 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
Andy Green4739e5c2011-01-22 12:51:57 +000068 break;
69
70 default:
71 break;
72 }
73
74 return 0;
75}
76
77
78/* lws-mirror_protocol */
79
Andy Green4739e5c2011-01-22 12:51:57 +000080
81static int
82callback_lws_mirror(struct libwebsocket *wsi,
83 enum libwebsocket_callback_reasons reason,
84 void *user, void *in, size_t len)
85{
Andy Green90c7cbc2011-01-27 06:26:52 +000086 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
87 LWS_SEND_BUFFER_POST_PADDING];
88 int l;
89
Andy Green4739e5c2011-01-22 12:51:57 +000090 switch (reason) {
91
Andy Green90c7cbc2011-01-27 06:26:52 +000092 case LWS_CALLBACK_CLIENT_ESTABLISHED:
93
94 /*
95 * start the ball rolling,
Andy Greenb6e6ebe2011-01-27 06:36:39 +000096 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
Andy Green90c7cbc2011-01-27 06:26:52 +000097 */
98
99 libwebsocket_callback_on_writable(wsi);
100 break;
101
Andy Green4739e5c2011-01-22 12:51:57 +0000102 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +0000103/* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
104 break;
105
106 case LWS_CALLBACK_CLIENT_WRITEABLE:
107
108 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
109 "c #%06X %d %d %d;",
110 (int)random() & 0xffffff,
111 (int)random() % 500,
112 (int)random() % 250,
113 (int)random() % 24);
114
115 libwebsocket_write(wsi,
Andy Green990d5062011-01-30 21:04:24 +0000116 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
Andy Green90c7cbc2011-01-27 06:26:52 +0000117
118 /* get notified as soon as we can write again */
119
120 libwebsocket_callback_on_writable(wsi);
121
Andy Greenb6e6ebe2011-01-27 06:36:39 +0000122 /*
123 * without at least this delay, we choke the browser
124 * and the connection stalls, despite we now take care about
125 * flow control
126 */
127
Andy Green90c7cbc2011-01-27 06:26:52 +0000128 usleep(200);
Andy Green4739e5c2011-01-22 12:51:57 +0000129 break;
130
131 default:
132 break;
133 }
134
135 return 0;
136}
137
138
139/* list of supported protocols and callbacks */
140
141static struct libwebsocket_protocols protocols[] = {
142
143 [PROTOCOL_DUMB_INCREMENT] = {
144 .name = "dumb-increment-protocol",
145 .callback = callback_dumb_increment,
146 },
147 [PROTOCOL_LWS_MIRROR] = {
148 .name = "lws-mirror-protocol",
149 .callback = callback_lws_mirror,
150 },
151 [DEMO_PROTOCOL_COUNT] = { /* end of list */
152 .callback = NULL
153 }
154};
155
156static struct option options[] = {
Andy Green90c7cbc2011-01-27 06:26:52 +0000157 { "help", no_argument, NULL, 'h' },
158 { "port", required_argument, NULL, 'p' },
159 { "ssl", no_argument, NULL, 's' },
Andy Green990d5062011-01-30 21:04:24 +0000160 { "killmask", no_argument, NULL, 'k' },
Andy Green4739e5c2011-01-22 12:51:57 +0000161 { NULL, 0, 0, 0 }
162};
163
164
165int main(int argc, char **argv)
166{
167 int n = 0;
168 int port = 7681;
169 int use_ssl = 0;
170 struct libwebsocket_context *context;
Andy Green1efb63c2011-02-06 17:42:06 +0000171 const char *address;
Andy Green4739e5c2011-01-22 12:51:57 +0000172 struct libwebsocket *wsi_dumb;
173 struct libwebsocket *wsi_mirror;
Andy Green90c7cbc2011-01-27 06:26:52 +0000174
Andy Green4739e5c2011-01-22 12:51:57 +0000175 fprintf(stderr, "libwebsockets test client\n"
176 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
177 "licensed under LGPL2.1\n");
178
179 if (argc < 2)
180 goto usage;
181
182 optind++;
183
184 while (n >= 0) {
Andy Green990d5062011-01-30 21:04:24 +0000185 n = getopt_long(argc, argv, "khsp:", options, NULL);
Andy Green4739e5c2011-01-22 12:51:57 +0000186 if (n < 0)
187 continue;
188 switch (n) {
189 case 's':
Andy Green90c7cbc2011-01-27 06:26:52 +0000190 use_ssl = 2; /* 2 = allow selfsigned */
Andy Green4739e5c2011-01-22 12:51:57 +0000191 break;
192 case 'p':
193 port = atoi(optarg);
194 break;
Andy Green990d5062011-01-30 21:04:24 +0000195 case 'k':
196 opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
197 break;
Andy Green4739e5c2011-01-22 12:51:57 +0000198 case 'h':
199 goto usage;
200 }
201 }
202
Andy Green1efb63c2011-02-06 17:42:06 +0000203 if (optind >= argc)
204 goto usage;
205
206 address = argv[optind];
207
Andy Green4739e5c2011-01-22 12:51:57 +0000208 /*
209 * create the websockets context. This tracks open connections and
210 * knows how to route any traffic and which protocol version to use,
211 * and if each connection is client or server side.
212 *
213 * For this client-only demo, we tell it to not listen on any port.
214 */
215
216 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN,
Andy Green8014b292011-01-30 20:57:25 +0000217 protocols, NULL, NULL, -1, -1, 0);
Andy Green4739e5c2011-01-22 12:51:57 +0000218 if (context == NULL) {
219 fprintf(stderr, "Creating libwebsocket context failed\n");
220 return 1;
221 }
222
223
224 /* create a client websocket using dumb increment protocol */
225
Andy Green90c7cbc2011-01-27 06:26:52 +0000226 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
Andy Green1efb63c2011-02-06 17:42:06 +0000227 "/", argv[optind], argv[optind],
Andy Green4739e5c2011-01-22 12:51:57 +0000228 protocols[PROTOCOL_DUMB_INCREMENT].name);
229
230 if (wsi_dumb == NULL) {
231 fprintf(stderr, "libwebsocket dumb connect failed\n");
232 return -1;
233 }
234
235 /* create a client websocket using mirror protocol */
236
Andy Green90c7cbc2011-01-27 06:26:52 +0000237 wsi_mirror = libwebsocket_client_connect(context, address, port,
Andy Green1efb63c2011-02-06 17:42:06 +0000238 use_ssl, "/", argv[optind], argv[optind],
Andy Green4739e5c2011-01-22 12:51:57 +0000239 protocols[PROTOCOL_LWS_MIRROR].name);
240
241 if (wsi_mirror == NULL) {
242 fprintf(stderr, "libwebsocket dumb connect failed\n");
243 return -1;
244 }
245
246 fprintf(stderr, "Websocket connections opened\n");
247
248 /*
249 * sit there servicing the websocket context to handle incoming
250 * packets, and drawing random circles on the mirror protocol websocket
251 */
252
253 n = 0;
Andy Green90c7cbc2011-01-27 06:26:52 +0000254 while (n >= 0)
255 n = libwebsocket_service(context, 1000);
Andy Green4739e5c2011-01-22 12:51:57 +0000256
Andy Green6964bb52011-01-23 16:50:33 +0000257 libwebsocket_context_destroy(context);
258
Andy Green4739e5c2011-01-22 12:51:57 +0000259 return 0;
260
261usage:
262 fprintf(stderr, "Usage: libwebsockets-test-client "
263 "<server address> [--port=<p>] "
264 "[--ssl]\n");
265 return 1;
266}