blob: 734aab20b5a0a6925f20d2933fe6af47d4168137 [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
28
29#include "../lib/libwebsockets.h"
30#include <poll.h>
31
32/*
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).
36 *
37 * dumb-increment-protocol: we connect to the server and print the number
Andy Green90c7cbc2011-01-27 06:26:52 +000038 * we are given
Andy Green4739e5c2011-01-22 12:51:57 +000039 *
40 * lws-mirror-protocol: draws random circles, which are mirrored on to every
Andy Green90c7cbc2011-01-27 06:26:52 +000041 * client (see them being drawn in every browser
42 * session also using the test server)
Andy Green4739e5c2011-01-22 12:51:57 +000043 */
44
45enum demo_protocols {
46
47 PROTOCOL_DUMB_INCREMENT,
48 PROTOCOL_LWS_MIRROR,
49
50 /* always last */
51 DEMO_PROTOCOL_COUNT
52};
53
54
55/* dumb_increment protocol */
56
57static int
58callback_dumb_increment(struct libwebsocket *wsi,
59 enum libwebsocket_callback_reasons reason,
60 void *user, void *in, size_t len)
61{
62 switch (reason) {
63
64 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +000065 ((char *)in)[len] = '\0';
66 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
Andy Green4739e5c2011-01-22 12:51:57 +000067 break;
68
69 default:
70 break;
71 }
72
73 return 0;
74}
75
76
77/* lws-mirror_protocol */
78
Andy Green4739e5c2011-01-22 12:51:57 +000079
80static int
81callback_lws_mirror(struct libwebsocket *wsi,
82 enum libwebsocket_callback_reasons reason,
83 void *user, void *in, size_t len)
84{
Andy Green90c7cbc2011-01-27 06:26:52 +000085 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
86 LWS_SEND_BUFFER_POST_PADDING];
87 int l;
88
Andy Green4739e5c2011-01-22 12:51:57 +000089 switch (reason) {
90
Andy Green90c7cbc2011-01-27 06:26:52 +000091 case LWS_CALLBACK_CLIENT_ESTABLISHED:
92
93 /*
94 * start the ball rolling,
Andy Greenb6e6ebe2011-01-27 06:36:39 +000095 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
Andy Green90c7cbc2011-01-27 06:26:52 +000096 */
97
98 libwebsocket_callback_on_writable(wsi);
99 break;
100
Andy Green4739e5c2011-01-22 12:51:57 +0000101 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +0000102/* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
103 break;
104
105 case LWS_CALLBACK_CLIENT_WRITEABLE:
106
107 l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
108 "c #%06X %d %d %d;",
109 (int)random() & 0xffffff,
110 (int)random() % 500,
111 (int)random() % 250,
112 (int)random() % 24);
113
114 libwebsocket_write(wsi,
115 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, LWS_WRITE_TEXT);
116
117 /* get notified as soon as we can write again */
118
119 libwebsocket_callback_on_writable(wsi);
120
Andy Greenb6e6ebe2011-01-27 06:36:39 +0000121 /*
122 * without at least this delay, we choke the browser
123 * and the connection stalls, despite we now take care about
124 * flow control
125 */
126
Andy Green90c7cbc2011-01-27 06:26:52 +0000127 usleep(200);
Andy Green4739e5c2011-01-22 12:51:57 +0000128 break;
129
130 default:
131 break;
132 }
133
134 return 0;
135}
136
137
138/* list of supported protocols and callbacks */
139
140static struct libwebsocket_protocols protocols[] = {
141
142 [PROTOCOL_DUMB_INCREMENT] = {
143 .name = "dumb-increment-protocol",
144 .callback = callback_dumb_increment,
145 },
146 [PROTOCOL_LWS_MIRROR] = {
147 .name = "lws-mirror-protocol",
148 .callback = callback_lws_mirror,
149 },
150 [DEMO_PROTOCOL_COUNT] = { /* end of list */
151 .callback = NULL
152 }
153};
154
155static struct option options[] = {
Andy Green90c7cbc2011-01-27 06:26:52 +0000156 { "help", no_argument, NULL, 'h' },
157 { "port", required_argument, NULL, 'p' },
158 { "ssl", no_argument, NULL, 's' },
Andy Green4739e5c2011-01-22 12:51:57 +0000159 { NULL, 0, 0, 0 }
160};
161
162
163int main(int argc, char **argv)
164{
165 int n = 0;
166 int port = 7681;
167 int use_ssl = 0;
168 struct libwebsocket_context *context;
Andy Green90c7cbc2011-01-27 06:26:52 +0000169 const char *address = argv[1];
Andy Green4739e5c2011-01-22 12:51:57 +0000170 struct libwebsocket *wsi_dumb;
171 struct libwebsocket *wsi_mirror;
Andy Green90c7cbc2011-01-27 06:26:52 +0000172
Andy Green4739e5c2011-01-22 12:51:57 +0000173
174 fprintf(stderr, "libwebsockets test client\n"
175 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
176 "licensed under LGPL2.1\n");
177
178 if (argc < 2)
179 goto usage;
180
181 optind++;
182
183 while (n >= 0) {
184 n = getopt_long(argc, argv, "hsp:", options, NULL);
185 if (n < 0)
186 continue;
187 switch (n) {
188 case 's':
Andy Green90c7cbc2011-01-27 06:26:52 +0000189 use_ssl = 2; /* 2 = allow selfsigned */
Andy Green4739e5c2011-01-22 12:51:57 +0000190 break;
191 case 'p':
192 port = atoi(optarg);
193 break;
194 case 'h':
195 goto usage;
196 }
197 }
198
199 /*
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.
203 *
204 * For this client-only demo, we tell it to not listen on any port.
205 */
206
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");
211 return 1;
212 }
213
214
215 /* create a client websocket using dumb increment protocol */
216
Andy Green90c7cbc2011-01-27 06:26:52 +0000217 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
218 "/", "http://host", "origin",
Andy Green4739e5c2011-01-22 12:51:57 +0000219 protocols[PROTOCOL_DUMB_INCREMENT].name);
220
221 if (wsi_dumb == NULL) {
222 fprintf(stderr, "libwebsocket dumb connect failed\n");
223 return -1;
224 }
225
226 /* create a client websocket using mirror protocol */
227
Andy Green90c7cbc2011-01-27 06:26:52 +0000228 wsi_mirror = libwebsocket_client_connect(context, address, port,
229 use_ssl, "/", "http://host", "origin",
Andy Green4739e5c2011-01-22 12:51:57 +0000230 protocols[PROTOCOL_LWS_MIRROR].name);
231
232 if (wsi_mirror == NULL) {
233 fprintf(stderr, "libwebsocket dumb connect failed\n");
234 return -1;
235 }
236
237 fprintf(stderr, "Websocket connections opened\n");
238
239 /*
240 * sit there servicing the websocket context to handle incoming
241 * packets, and drawing random circles on the mirror protocol websocket
242 */
243
244 n = 0;
Andy Green90c7cbc2011-01-27 06:26:52 +0000245 while (n >= 0)
246 n = libwebsocket_service(context, 1000);
Andy Green4739e5c2011-01-22 12:51:57 +0000247
248 libwebsocket_client_close(wsi_dumb);
249 libwebsocket_client_close(wsi_mirror);
250
Andy Green6964bb52011-01-23 16:50:33 +0000251 libwebsocket_context_destroy(context);
252
Andy Green4739e5c2011-01-22 12:51:57 +0000253 return 0;
254
255usage:
256 fprintf(stderr, "Usage: libwebsockets-test-client "
257 "<server address> [--port=<p>] "
258 "[--ssl]\n");
259 return 1;
260}