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