2010-11-08 17:12:19 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets-test-server - libwebsockets test implementation
|
2010-11-13 10:03:47 +00:00
|
|
|
*
|
2010-11-08 17:12:19 +00:00
|
|
|
* Copyright (C) 2010 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
|
|
|
|
*/
|
|
|
|
|
2010-10-29 14:15:22 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2010-10-31 07:40:33 +00:00
|
|
|
#include <getopt.h>
|
2010-10-31 11:57:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-11-01 09:12:17 +00:00
|
|
|
#include "../lib/libwebsockets.h"
|
2010-10-29 14:15:22 +01:00
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
/*
|
|
|
|
* This demo server shows how to use libwebsockets for one or more
|
|
|
|
* websocket protocols in the same server
|
|
|
|
*
|
|
|
|
* It defines the following websocket protocols:
|
|
|
|
*
|
|
|
|
* dumb-increment-protocol: once the socket is opened, an incrementing
|
|
|
|
* ascii string is sent down it every 50ms.
|
|
|
|
* If you send "reset\n" on the websocket, then
|
|
|
|
* the incrementing number is reset to 0.
|
2010-11-12 14:12:13 +00:00
|
|
|
*
|
|
|
|
* lws-mirror-protocol: copies any received packet to every connection also
|
|
|
|
* using this protocol, including the sender
|
2010-11-12 13:10:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-11-01 09:20:48 +00:00
|
|
|
#define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server"
|
2010-10-29 14:15:22 +01:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
/* this protocol server (always the first one) just knows how to do HTTP */
|
2010-10-31 12:42:52 +00:00
|
|
|
|
2010-11-13 10:03:47 +00:00
|
|
|
static int callback_http(struct libwebsocket *wsi,
|
|
|
|
enum libwebsocket_callback_reasons reason, void *user,
|
2010-11-03 11:13:06 +00:00
|
|
|
void *in, size_t len)
|
2010-10-29 14:15:22 +01:00
|
|
|
{
|
|
|
|
switch (reason) {
|
2010-10-31 11:57:17 +00:00
|
|
|
case LWS_CALLBACK_HTTP:
|
2010-11-11 12:28:29 +00:00
|
|
|
fprintf(stderr, "serving HTTP URI %s\n", in);
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-11 12:28:29 +00:00
|
|
|
if (in && strcmp(in, "/favicon.ico") == 0) {
|
2010-11-01 09:20:48 +00:00
|
|
|
if (libwebsockets_serve_http_file(wsi,
|
|
|
|
LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon"))
|
2010-10-31 12:42:52 +00:00
|
|
|
fprintf(stderr, "Failed to send favicon\n");
|
2010-10-31 11:57:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-10-31 11:57:17 +00:00
|
|
|
/* send the script... when it runs it'll start websockets */
|
|
|
|
|
2010-11-01 09:20:48 +00:00
|
|
|
if (libwebsockets_serve_http_file(wsi,
|
|
|
|
LOCAL_RESOURCE_PATH"/test.html", "text/html"))
|
2010-10-31 11:57:17 +00:00
|
|
|
fprintf(stderr, "Failed to send HTTP file\n");
|
2010-10-29 14:15:22 +01:00
|
|
|
break;
|
2010-11-11 12:28:29 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dumb_increment protocol */
|
|
|
|
|
2010-12-19 20:50:01 +00:00
|
|
|
/*
|
|
|
|
* one of these is auto-created for each connection and a pointer to the
|
|
|
|
* appropriate instance is passed to the callback in the user parameter
|
|
|
|
*
|
|
|
|
* for this example protocol we use it to individualize the count for each
|
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
struct per_session_data__dumb_increment {
|
|
|
|
int number;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2010-11-13 10:03:47 +00:00
|
|
|
callback_dumb_increment(struct libwebsocket *wsi,
|
2010-11-12 10:44:16 +00:00
|
|
|
enum libwebsocket_callback_reasons reason,
|
2010-11-13 10:03:47 +00:00
|
|
|
void *user, void *in, size_t len)
|
2010-11-12 10:44:16 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
|
|
|
|
LWS_SEND_BUFFER_POST_PADDING];
|
2010-11-15 12:46:25 +00:00
|
|
|
char *p = (char *)&buf[LWS_SEND_BUFFER_PRE_PADDING];
|
2010-11-13 10:03:47 +00:00
|
|
|
struct per_session_data__dumb_increment *pss = user;
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
switch (reason) {
|
2010-11-11 12:28:29 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
|
|
|
pss->number = 0;
|
|
|
|
break;
|
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
/*
|
|
|
|
* in this protocol, we just use the broadcast action as the chance to
|
|
|
|
* send our own connection-specific data and ignore the broadcast info
|
|
|
|
* that is available in the 'in' parameter
|
|
|
|
*/
|
|
|
|
|
|
|
|
case LWS_CALLBACK_BROADCAST:
|
2010-11-12 10:44:16 +00:00
|
|
|
n = sprintf(p, "%d", pss->number++);
|
|
|
|
n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
|
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr, "ERROR writing to socket");
|
2010-11-11 12:52:28 +00:00
|
|
|
return 1;
|
2010-11-11 12:28:29 +00:00
|
|
|
}
|
2010-11-12 10:44:16 +00:00
|
|
|
break;
|
2010-11-11 12:52:28 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2010-11-12 13:10:40 +00:00
|
|
|
fprintf(stderr, "rx %d\n", len);
|
2010-11-12 10:44:16 +00:00
|
|
|
if (len < 6)
|
|
|
|
break;
|
|
|
|
if (strcmp(in, "reset\n") == 0)
|
|
|
|
pss->number = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2010-10-29 14:15:22 +01:00
|
|
|
}
|
2010-10-31 12:42:52 +00:00
|
|
|
|
2010-10-29 14:15:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
/* lws-mirror_protocol */
|
|
|
|
|
|
|
|
static int
|
2010-11-13 10:03:47 +00:00
|
|
|
callback_lws_mirror(struct libwebsocket *wsi,
|
2010-11-12 13:10:40 +00:00
|
|
|
enum libwebsocket_callback_reasons reason,
|
2010-11-13 10:03:47 +00:00
|
|
|
void *user, void *in, size_t len)
|
2010-11-12 13:10:40 +00:00
|
|
|
{
|
|
|
|
int n;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
switch (reason) {
|
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
case LWS_CALLBACK_BROADCAST:
|
|
|
|
n = libwebsocket_write(wsi, in, len, LWS_WRITE_TEXT);
|
2010-11-12 13:10:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2010-12-18 15:13:50 +00:00
|
|
|
/*
|
|
|
|
* copy the incoming packet to all other protocol users
|
|
|
|
*
|
|
|
|
* This demonstrates how easy it is to broadcast from inside
|
|
|
|
* a callback.
|
|
|
|
*
|
|
|
|
* How this works is it calls back to the callback for all
|
|
|
|
* connected sockets using this protocol with
|
|
|
|
* LWS_CALLBACK_BROADCAST reason. Our handler for that above
|
|
|
|
* writes the data down the socket.
|
|
|
|
*/
|
|
|
|
libwebsockets_broadcast(libwebsockets_get_protocol(wsi),
|
|
|
|
in, len);
|
2010-11-12 13:10:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
/* list of supported protocols and callbacks */
|
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
static struct libwebsocket_protocols protocols[] = {
|
2010-12-19 20:50:01 +00:00
|
|
|
/* first protocol must always be HTTP handler */
|
|
|
|
[0] = {
|
2010-11-12 10:44:16 +00:00
|
|
|
.name = "http-only",
|
|
|
|
.callback = callback_http,
|
|
|
|
},
|
2010-12-19 20:50:01 +00:00
|
|
|
[1] = {
|
2010-11-12 10:44:16 +00:00
|
|
|
.name = "dumb-increment-protocol",
|
|
|
|
.callback = callback_dumb_increment,
|
|
|
|
.per_session_data_size =
|
|
|
|
sizeof(struct per_session_data__dumb_increment),
|
|
|
|
},
|
2010-12-19 20:50:01 +00:00
|
|
|
[2] = {
|
2010-11-12 13:10:40 +00:00
|
|
|
.name = "lws-mirror-protocol",
|
|
|
|
.callback = callback_lws_mirror,
|
|
|
|
},
|
2010-12-19 20:50:01 +00:00
|
|
|
[3] = { /* end of list */
|
2010-11-12 10:44:16 +00:00
|
|
|
.callback = NULL
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
static struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "port", required_argument, NULL, 'p' },
|
2010-11-08 17:03:03 +00:00
|
|
|
{ "ssl", no_argument, NULL, 's' },
|
2010-10-31 07:40:33 +00:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
2010-10-29 14:15:22 +01:00
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
int main(int argc, char **argv)
|
2010-10-29 14:15:22 +01:00
|
|
|
{
|
2010-10-31 07:40:33 +00:00
|
|
|
int n = 0;
|
2010-11-13 10:03:47 +00:00
|
|
|
const char *cert_path =
|
2010-11-08 20:20:42 +00:00
|
|
|
LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
|
2010-11-13 10:03:47 +00:00
|
|
|
const char *key_path =
|
2010-11-08 20:20:42 +00:00
|
|
|
LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
|
2010-12-18 15:13:50 +00:00
|
|
|
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 +
|
|
|
|
LWS_SEND_BUFFER_POST_PADDING];
|
2010-12-19 20:50:01 +00:00
|
|
|
int port = 7681;
|
|
|
|
int use_ssl = 0;
|
2010-10-31 07:40:33 +00:00
|
|
|
|
2010-10-31 12:42:52 +00:00
|
|
|
fprintf(stderr, "libwebsockets test server\n"
|
2010-11-11 09:22:22 +00:00
|
|
|
"(C) Copyright 2010 Andy Green <andy@warmcat.com> "
|
|
|
|
"licensed under LGPL2.1\n");
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
while (n >= 0) {
|
2010-12-19 20:50:01 +00:00
|
|
|
n = getopt_long(argc, argv, "hsp:", options, NULL);
|
2010-10-31 07:40:33 +00:00
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
2010-11-08 17:03:03 +00:00
|
|
|
case 's':
|
|
|
|
use_ssl = 1;
|
|
|
|
break;
|
2010-10-31 07:40:33 +00:00
|
|
|
case 'p':
|
|
|
|
port = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
2010-10-31 12:42:52 +00:00
|
|
|
fprintf(stderr, "Usage: test-server "
|
2010-11-12 10:44:16 +00:00
|
|
|
"[--port=<p>] [--ssl]\n");
|
2010-10-31 07:40:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2010-11-08 17:03:03 +00:00
|
|
|
|
|
|
|
if (!use_ssl)
|
|
|
|
cert_path = key_path = NULL;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
if (libwebsocket_create_server(port, protocols, cert_path, key_path,
|
|
|
|
-1, -1) < 0) {
|
2010-10-29 14:15:22 +01:00
|
|
|
fprintf(stderr, "libwebsocket init failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
/*
|
|
|
|
* After initializing and creating the websocket server in its own fork
|
|
|
|
* we return to the main process here
|
|
|
|
*/
|
|
|
|
|
|
|
|
buf[LWS_SEND_BUFFER_PRE_PADDING] = 'x';
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
2010-12-19 20:50:01 +00:00
|
|
|
usleep(50000);
|
2010-12-18 15:13:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This broadcasts to all dumb-increment-protocol connections
|
2010-12-19 20:50:01 +00:00
|
|
|
* at 20Hz.
|
2010-12-18 15:13:50 +00:00
|
|
|
*
|
|
|
|
* We're just sending a character 'x', in these examples the
|
|
|
|
* callbacks send their own per-connection content.
|
|
|
|
*
|
|
|
|
* You have to send something with nonzero length to get the
|
|
|
|
* callback actions delivered.
|
|
|
|
*
|
|
|
|
* We take care of pre-and-post padding allocation.
|
|
|
|
*/
|
|
|
|
|
2010-12-19 20:50:01 +00:00
|
|
|
/* protocols[1] == dumb-increment-protocol */
|
2010-12-18 15:13:50 +00:00
|
|
|
libwebsockets_broadcast(&protocols[1],
|
|
|
|
&buf[LWS_SEND_BUFFER_PRE_PADDING], 1);
|
|
|
|
}
|
|
|
|
|
2010-10-29 14:15:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|