blob: 363695173fef5303204691843fe13d8224f96a37 [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,
95 * LWS_CALLBACK_CLIENT_WRITEABLE will come immediately
96 */
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
121 usleep(200);
Andy Green4739e5c2011-01-22 12:51:57 +0000122 break;
123
124 default:
125 break;
126 }
127
128 return 0;
129}
130
131
132/* list of supported protocols and callbacks */
133
134static struct libwebsocket_protocols protocols[] = {
135
136 [PROTOCOL_DUMB_INCREMENT] = {
137 .name = "dumb-increment-protocol",
138 .callback = callback_dumb_increment,
139 },
140 [PROTOCOL_LWS_MIRROR] = {
141 .name = "lws-mirror-protocol",
142 .callback = callback_lws_mirror,
143 },
144 [DEMO_PROTOCOL_COUNT] = { /* end of list */
145 .callback = NULL
146 }
147};
148
149static struct option options[] = {
Andy Green90c7cbc2011-01-27 06:26:52 +0000150 { "help", no_argument, NULL, 'h' },
151 { "port", required_argument, NULL, 'p' },
152 { "ssl", no_argument, NULL, 's' },
Andy Green4739e5c2011-01-22 12:51:57 +0000153 { NULL, 0, 0, 0 }
154};
155
156
157int main(int argc, char **argv)
158{
159 int n = 0;
160 int port = 7681;
161 int use_ssl = 0;
162 struct libwebsocket_context *context;
Andy Green90c7cbc2011-01-27 06:26:52 +0000163 const char *address = argv[1];
Andy Green4739e5c2011-01-22 12:51:57 +0000164 struct libwebsocket *wsi_dumb;
165 struct libwebsocket *wsi_mirror;
Andy Green90c7cbc2011-01-27 06:26:52 +0000166
Andy Green4739e5c2011-01-22 12:51:57 +0000167
168 fprintf(stderr, "libwebsockets test client\n"
169 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
170 "licensed under LGPL2.1\n");
171
172 if (argc < 2)
173 goto usage;
174
175 optind++;
176
177 while (n >= 0) {
178 n = getopt_long(argc, argv, "hsp:", options, NULL);
179 if (n < 0)
180 continue;
181 switch (n) {
182 case 's':
Andy Green90c7cbc2011-01-27 06:26:52 +0000183 use_ssl = 2; /* 2 = allow selfsigned */
Andy Green4739e5c2011-01-22 12:51:57 +0000184 break;
185 case 'p':
186 port = atoi(optarg);
187 break;
188 case 'h':
189 goto usage;
190 }
191 }
192
193 /*
194 * create the websockets context. This tracks open connections and
195 * knows how to route any traffic and which protocol version to use,
196 * and if each connection is client or server side.
197 *
198 * For this client-only demo, we tell it to not listen on any port.
199 */
200
201 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN,
202 protocols, NULL, NULL, -1, -1);
203 if (context == NULL) {
204 fprintf(stderr, "Creating libwebsocket context failed\n");
205 return 1;
206 }
207
208
209 /* create a client websocket using dumb increment protocol */
210
Andy Green90c7cbc2011-01-27 06:26:52 +0000211 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
212 "/", "http://host", "origin",
Andy Green4739e5c2011-01-22 12:51:57 +0000213 protocols[PROTOCOL_DUMB_INCREMENT].name);
214
215 if (wsi_dumb == NULL) {
216 fprintf(stderr, "libwebsocket dumb connect failed\n");
217 return -1;
218 }
219
220 /* create a client websocket using mirror protocol */
221
Andy Green90c7cbc2011-01-27 06:26:52 +0000222 wsi_mirror = libwebsocket_client_connect(context, address, port,
223 use_ssl, "/", "http://host", "origin",
Andy Green4739e5c2011-01-22 12:51:57 +0000224 protocols[PROTOCOL_LWS_MIRROR].name);
225
226 if (wsi_mirror == NULL) {
227 fprintf(stderr, "libwebsocket dumb connect failed\n");
228 return -1;
229 }
230
231 fprintf(stderr, "Websocket connections opened\n");
232
233 /*
234 * sit there servicing the websocket context to handle incoming
235 * packets, and drawing random circles on the mirror protocol websocket
236 */
237
238 n = 0;
Andy Green90c7cbc2011-01-27 06:26:52 +0000239 while (n >= 0)
240 n = libwebsocket_service(context, 1000);
Andy Green4739e5c2011-01-22 12:51:57 +0000241
242 libwebsocket_client_close(wsi_dumb);
243 libwebsocket_client_close(wsi_mirror);
244
Andy Green6964bb52011-01-23 16:50:33 +0000245 libwebsocket_context_destroy(context);
246
Andy Green4739e5c2011-01-22 12:51:57 +0000247 return 0;
248
249usage:
250 fprintf(stderr, "Usage: libwebsockets-test-client "
251 "<server address> [--port=<p>] "
252 "[--ssl]\n");
253 return 1;
254}