Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1 | /* |
| 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 Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 28 | #include "../lib/libwebsockets.h" |
| 29 | #include <poll.h> |
| 30 | |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 31 | static unsigned int opts; |
Andy Green | b3ae0a3 | 2011-02-14 20:25:43 +0000 | [diff] [blame^] | 32 | static int was_closed; |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 33 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 34 | /* |
| 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 Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 40 | * we are given |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 41 | * |
| 42 | * lws-mirror-protocol: draws random circles, which are mirrored on to every |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 43 | * client (see them being drawn in every browser |
| 44 | * session also using the test server) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | enum 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 | |
| 59 | static int |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 60 | callback_dumb_increment(struct libwebsocket_context * this, |
| 61 | struct libwebsocket *wsi, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 62 | 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 Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 68 | ((char *)in)[len] = '\0'; |
| 69 | fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 70 | break; |
| 71 | |
| 72 | default: |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /* lws-mirror_protocol */ |
| 81 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 82 | |
| 83 | static int |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 84 | callback_lws_mirror(struct libwebsocket_context * this, |
| 85 | struct libwebsocket *wsi, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 86 | enum libwebsocket_callback_reasons reason, |
| 87 | void *user, void *in, size_t len) |
| 88 | { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 89 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + |
| 90 | LWS_SEND_BUFFER_POST_PADDING]; |
| 91 | int l; |
| 92 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 93 | switch (reason) { |
| 94 | |
Andy Green | b3ae0a3 | 2011-02-14 20:25:43 +0000 | [diff] [blame^] | 95 | case LWS_CALLBACK_CLOSED: |
| 96 | fprintf(stderr, "LWS_CALLBACK_CLOSED\n"); |
| 97 | was_closed = 1; |
| 98 | break; |
| 99 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 100 | case LWS_CALLBACK_CLIENT_ESTABLISHED: |
| 101 | |
| 102 | /* |
| 103 | * start the ball rolling, |
Andy Green | b6e6ebe | 2011-01-27 06:36:39 +0000 | [diff] [blame] | 104 | * LWS_CALLBACK_CLIENT_WRITEABLE will come next service |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 105 | */ |
| 106 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 107 | libwebsocket_callback_on_writable(this, wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 108 | break; |
| 109 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 110 | case LWS_CALLBACK_CLIENT_RECEIVE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 111 | /* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */ |
| 112 | break; |
| 113 | |
| 114 | case LWS_CALLBACK_CLIENT_WRITEABLE: |
| 115 | |
| 116 | l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING], |
| 117 | "c #%06X %d %d %d;", |
| 118 | (int)random() & 0xffffff, |
| 119 | (int)random() % 500, |
| 120 | (int)random() % 250, |
| 121 | (int)random() % 24); |
| 122 | |
| 123 | libwebsocket_write(wsi, |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 124 | &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 125 | |
| 126 | /* get notified as soon as we can write again */ |
| 127 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 128 | libwebsocket_callback_on_writable(this, wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 129 | |
Andy Green | b6e6ebe | 2011-01-27 06:36:39 +0000 | [diff] [blame] | 130 | /* |
| 131 | * without at least this delay, we choke the browser |
| 132 | * and the connection stalls, despite we now take care about |
| 133 | * flow control |
| 134 | */ |
| 135 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 136 | usleep(200); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 137 | break; |
| 138 | |
| 139 | default: |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* list of supported protocols and callbacks */ |
| 148 | |
| 149 | static struct libwebsocket_protocols protocols[] = { |
| 150 | |
| 151 | [PROTOCOL_DUMB_INCREMENT] = { |
| 152 | .name = "dumb-increment-protocol", |
| 153 | .callback = callback_dumb_increment, |
| 154 | }, |
| 155 | [PROTOCOL_LWS_MIRROR] = { |
| 156 | .name = "lws-mirror-protocol", |
| 157 | .callback = callback_lws_mirror, |
| 158 | }, |
| 159 | [DEMO_PROTOCOL_COUNT] = { /* end of list */ |
| 160 | .callback = NULL |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | static struct option options[] = { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 165 | { "help", no_argument, NULL, 'h' }, |
| 166 | { "port", required_argument, NULL, 'p' }, |
| 167 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 168 | { "killmask", no_argument, NULL, 'k' }, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 169 | { "version", required_argument, NULL, 'v' }, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 170 | { NULL, 0, 0, 0 } |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | int main(int argc, char **argv) |
| 175 | { |
| 176 | int n = 0; |
| 177 | int port = 7681; |
| 178 | int use_ssl = 0; |
| 179 | struct libwebsocket_context *context; |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 180 | const char *address; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 181 | struct libwebsocket *wsi_dumb; |
| 182 | struct libwebsocket *wsi_mirror; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 183 | int ietf_version = -1; /* latest */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 184 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 185 | fprintf(stderr, "libwebsockets test client\n" |
| 186 | "(C) Copyright 2010 Andy Green <andy@warmcat.com> " |
| 187 | "licensed under LGPL2.1\n"); |
| 188 | |
| 189 | if (argc < 2) |
| 190 | goto usage; |
| 191 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 192 | while (n >= 0) { |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 193 | n = getopt_long(argc, argv, "v:khsp:", options, NULL); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 194 | if (n < 0) |
| 195 | continue; |
| 196 | switch (n) { |
| 197 | case 's': |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 198 | use_ssl = 2; /* 2 = allow selfsigned */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 199 | break; |
| 200 | case 'p': |
| 201 | port = atoi(optarg); |
| 202 | break; |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 203 | case 'k': |
| 204 | opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK; |
| 205 | break; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 206 | case 'v': |
| 207 | ietf_version = atoi(optarg); |
| 208 | break; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 209 | case 'h': |
| 210 | goto usage; |
| 211 | } |
| 212 | } |
| 213 | |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 214 | if (optind >= argc) |
| 215 | goto usage; |
| 216 | |
| 217 | address = argv[optind]; |
| 218 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 219 | /* |
| 220 | * create the websockets context. This tracks open connections and |
| 221 | * knows how to route any traffic and which protocol version to use, |
| 222 | * and if each connection is client or server side. |
| 223 | * |
| 224 | * For this client-only demo, we tell it to not listen on any port. |
| 225 | */ |
| 226 | |
| 227 | context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 228 | protocols, NULL, NULL, -1, -1, 0); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 229 | if (context == NULL) { |
| 230 | fprintf(stderr, "Creating libwebsocket context failed\n"); |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* create a client websocket using dumb increment protocol */ |
| 236 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 237 | wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl, |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 238 | "/", argv[optind], argv[optind], |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 239 | protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 240 | |
| 241 | if (wsi_dumb == NULL) { |
| 242 | fprintf(stderr, "libwebsocket dumb connect failed\n"); |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | /* create a client websocket using mirror protocol */ |
| 247 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 248 | wsi_mirror = libwebsocket_client_connect(context, address, port, |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 249 | use_ssl, "/", argv[optind], argv[optind], |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 250 | protocols[PROTOCOL_LWS_MIRROR].name, ietf_version); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 251 | |
| 252 | if (wsi_mirror == NULL) { |
| 253 | fprintf(stderr, "libwebsocket dumb connect failed\n"); |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | fprintf(stderr, "Websocket connections opened\n"); |
| 258 | |
| 259 | /* |
| 260 | * sit there servicing the websocket context to handle incoming |
| 261 | * packets, and drawing random circles on the mirror protocol websocket |
| 262 | */ |
| 263 | |
| 264 | n = 0; |
Andy Green | b3ae0a3 | 2011-02-14 20:25:43 +0000 | [diff] [blame^] | 265 | while (n >= 0 && !was_closed) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 266 | n = libwebsocket_service(context, 1000); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 267 | |
Andy Green | b3ae0a3 | 2011-02-14 20:25:43 +0000 | [diff] [blame^] | 268 | fprintf(stderr, "Exiting\n"); |
| 269 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 270 | libwebsocket_context_destroy(context); |
| 271 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 272 | return 0; |
| 273 | |
| 274 | usage: |
| 275 | fprintf(stderr, "Usage: libwebsockets-test-client " |
| 276 | "<server address> [--port=<p>] " |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 277 | "[--ssl] [-k] [-v <ver>]\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 278 | return 1; |
| 279 | } |