2011-01-22 12:51:57 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets-test-client - libwebsockets test implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "../lib/libwebsockets.h"
|
|
|
|
#include <poll.h>
|
|
|
|
|
2011-01-30 21:04:24 +00:00
|
|
|
static unsigned int opts;
|
2011-02-14 20:25:43 +00:00
|
|
|
static int was_closed;
|
2011-01-30 21:04:24 +00:00
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
/*
|
|
|
|
* This demo shows how to connect multiple websockets simultaneously to a
|
|
|
|
* websocket server (there is no restriction on their having to be the same
|
|
|
|
* server just it simplifies the demo).
|
|
|
|
*
|
|
|
|
* dumb-increment-protocol: we connect to the server and print the number
|
2011-01-27 06:26:52 +00:00
|
|
|
* we are given
|
2011-01-22 12:51:57 +00:00
|
|
|
*
|
|
|
|
* lws-mirror-protocol: draws random circles, which are mirrored on to every
|
2011-01-27 06:26:52 +00:00
|
|
|
* client (see them being drawn in every browser
|
|
|
|
* session also using the test server)
|
2011-01-22 12:51:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum demo_protocols {
|
|
|
|
|
|
|
|
PROTOCOL_DUMB_INCREMENT,
|
|
|
|
PROTOCOL_LWS_MIRROR,
|
|
|
|
|
|
|
|
/* always last */
|
|
|
|
DEMO_PROTOCOL_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* dumb_increment protocol */
|
|
|
|
|
|
|
|
static int
|
2011-02-14 09:14:25 +00:00
|
|
|
callback_dumb_increment(struct libwebsocket_context * this,
|
|
|
|
struct libwebsocket *wsi,
|
2011-01-22 12:51:57 +00:00
|
|
|
enum libwebsocket_callback_reasons reason,
|
|
|
|
void *user, void *in, size_t len)
|
|
|
|
{
|
|
|
|
switch (reason) {
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
2011-01-27 06:26:52 +00:00
|
|
|
((char *)in)[len] = '\0';
|
|
|
|
fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
|
2011-01-22 12:51:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* lws-mirror_protocol */
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2011-02-14 09:14:25 +00:00
|
|
|
callback_lws_mirror(struct libwebsocket_context * this,
|
|
|
|
struct libwebsocket *wsi,
|
2011-01-22 12:51:57 +00:00
|
|
|
enum libwebsocket_callback_reasons reason,
|
|
|
|
void *user, void *in, size_t len)
|
|
|
|
{
|
2011-01-27 06:26:52 +00:00
|
|
|
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
|
|
|
|
LWS_SEND_BUFFER_POST_PADDING];
|
|
|
|
int l;
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
switch (reason) {
|
|
|
|
|
2011-02-14 20:25:43 +00:00
|
|
|
case LWS_CALLBACK_CLOSED:
|
|
|
|
fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
|
|
|
|
was_closed = 1;
|
|
|
|
break;
|
|
|
|
|
2011-01-27 06:26:52 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start the ball rolling,
|
2011-01-27 06:36:39 +00:00
|
|
|
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
|
2011-01-27 06:26:52 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-14 09:14:25 +00:00
|
|
|
libwebsocket_callback_on_writable(this, wsi);
|
2011-01-27 06:26:52 +00:00
|
|
|
break;
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
2011-01-27 06:26:52 +00:00
|
|
|
/* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
|
|
|
|
|
|
|
l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
|
|
|
|
"c #%06X %d %d %d;",
|
|
|
|
(int)random() & 0xffffff,
|
|
|
|
(int)random() % 500,
|
|
|
|
(int)random() % 250,
|
|
|
|
(int)random() % 24);
|
|
|
|
|
|
|
|
libwebsocket_write(wsi,
|
2011-01-30 21:04:24 +00:00
|
|
|
&buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
|
2011-01-27 06:26:52 +00:00
|
|
|
|
|
|
|
/* get notified as soon as we can write again */
|
|
|
|
|
2011-02-14 09:14:25 +00:00
|
|
|
libwebsocket_callback_on_writable(this, wsi);
|
2011-01-27 06:26:52 +00:00
|
|
|
|
2011-01-27 06:36:39 +00:00
|
|
|
/*
|
|
|
|
* without at least this delay, we choke the browser
|
|
|
|
* and the connection stalls, despite we now take care about
|
|
|
|
* flow control
|
|
|
|
*/
|
|
|
|
|
2011-01-27 06:26:52 +00:00
|
|
|
usleep(200);
|
2011-01-22 12:51:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* list of supported protocols and callbacks */
|
|
|
|
|
|
|
|
static struct libwebsocket_protocols protocols[] = {
|
|
|
|
|
|
|
|
[PROTOCOL_DUMB_INCREMENT] = {
|
|
|
|
.name = "dumb-increment-protocol",
|
|
|
|
.callback = callback_dumb_increment,
|
|
|
|
},
|
|
|
|
[PROTOCOL_LWS_MIRROR] = {
|
|
|
|
.name = "lws-mirror-protocol",
|
|
|
|
.callback = callback_lws_mirror,
|
|
|
|
},
|
|
|
|
[DEMO_PROTOCOL_COUNT] = { /* end of list */
|
|
|
|
.callback = NULL
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct option options[] = {
|
2011-01-27 06:26:52 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
{ "ssl", no_argument, NULL, 's' },
|
2011-01-30 21:04:24 +00:00
|
|
|
{ "killmask", no_argument, NULL, 'k' },
|
2011-02-09 08:49:14 +00:00
|
|
|
{ "version", required_argument, NULL, 'v' },
|
2011-01-22 12:51:57 +00:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
int port = 7681;
|
|
|
|
int use_ssl = 0;
|
|
|
|
struct libwebsocket_context *context;
|
2011-02-06 17:42:06 +00:00
|
|
|
const char *address;
|
2011-01-22 12:51:57 +00:00
|
|
|
struct libwebsocket *wsi_dumb;
|
|
|
|
struct libwebsocket *wsi_mirror;
|
2011-02-09 08:49:14 +00:00
|
|
|
int ietf_version = -1; /* latest */
|
2011-01-27 06:26:52 +00:00
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
fprintf(stderr, "libwebsockets test client\n"
|
|
|
|
"(C) Copyright 2010 Andy Green <andy@warmcat.com> "
|
|
|
|
"licensed under LGPL2.1\n");
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
while (n >= 0) {
|
2011-02-09 08:49:14 +00:00
|
|
|
n = getopt_long(argc, argv, "v:khsp:", options, NULL);
|
2011-01-22 12:51:57 +00:00
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
|
|
|
case 's':
|
2011-01-27 06:26:52 +00:00
|
|
|
use_ssl = 2; /* 2 = allow selfsigned */
|
2011-01-22 12:51:57 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
port = atoi(optarg);
|
|
|
|
break;
|
2011-01-30 21:04:24 +00:00
|
|
|
case 'k':
|
|
|
|
opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
|
|
|
|
break;
|
2011-02-09 08:49:14 +00:00
|
|
|
case 'v':
|
|
|
|
ietf_version = atoi(optarg);
|
|
|
|
break;
|
2011-01-22 12:51:57 +00:00
|
|
|
case 'h':
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:42:06 +00:00
|
|
|
if (optind >= argc)
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
address = argv[optind];
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
/*
|
|
|
|
* create the websockets context. This tracks open connections and
|
|
|
|
* knows how to route any traffic and which protocol version to use,
|
|
|
|
* and if each connection is client or server side.
|
|
|
|
*
|
|
|
|
* For this client-only demo, we tell it to not listen on any port.
|
|
|
|
*/
|
|
|
|
|
|
|
|
context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN,
|
2011-01-30 20:57:25 +00:00
|
|
|
protocols, NULL, NULL, -1, -1, 0);
|
2011-01-22 12:51:57 +00:00
|
|
|
if (context == NULL) {
|
|
|
|
fprintf(stderr, "Creating libwebsocket context failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* create a client websocket using dumb increment protocol */
|
|
|
|
|
2011-01-27 06:26:52 +00:00
|
|
|
wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
|
2011-02-06 17:42:06 +00:00
|
|
|
"/", argv[optind], argv[optind],
|
2011-02-09 08:49:14 +00:00
|
|
|
protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
if (wsi_dumb == NULL) {
|
|
|
|
fprintf(stderr, "libwebsocket dumb connect failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a client websocket using mirror protocol */
|
|
|
|
|
2011-01-27 06:26:52 +00:00
|
|
|
wsi_mirror = libwebsocket_client_connect(context, address, port,
|
2011-02-06 17:42:06 +00:00
|
|
|
use_ssl, "/", argv[optind], argv[optind],
|
2011-02-09 08:49:14 +00:00
|
|
|
protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
if (wsi_mirror == NULL) {
|
|
|
|
fprintf(stderr, "libwebsocket dumb connect failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Websocket connections opened\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sit there servicing the websocket context to handle incoming
|
|
|
|
* packets, and drawing random circles on the mirror protocol websocket
|
|
|
|
*/
|
|
|
|
|
|
|
|
n = 0;
|
2011-02-14 20:25:43 +00:00
|
|
|
while (n >= 0 && !was_closed)
|
2011-01-27 06:26:52 +00:00
|
|
|
n = libwebsocket_service(context, 1000);
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2011-02-14 20:25:43 +00:00
|
|
|
fprintf(stderr, "Exiting\n");
|
|
|
|
|
2011-01-23 16:50:33 +00:00
|
|
|
libwebsocket_context_destroy(context);
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
usage:
|
|
|
|
fprintf(stderr, "Usage: libwebsockets-test-client "
|
|
|
|
"<server address> [--port=<p>] "
|
2011-02-10 09:07:05 +00:00
|
|
|
"[--ssl] [-k] [-v <ver>]\n");
|
2011-01-22 12:51:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|