2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
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.
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.
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,
28 #include "../lib/libwebsockets.h"
30 #define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server"
31 static int port = 7681;
32 static int use_ssl = 0;
34 /* this protocol server (always the first one) just knows how to do HTTP */
36 static int callback_http(struct libwebsocket * wsi,
37 enum libwebsocket_callback_reasons reason, void * user,
41 case LWS_CALLBACK_HTTP:
42 fprintf(stderr, "serving HTTP URI %s\n", in);
44 if (in && strcmp(in, "/favicon.ico") == 0) {
45 if (libwebsockets_serve_http_file(wsi,
46 LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon"))
47 fprintf(stderr, "Failed to send favicon\n");
51 /* send the script... when it runs it'll start websockets */
53 if (libwebsockets_serve_http_file(wsi,
54 LOCAL_RESOURCE_PATH"/test.html", "text/html"))
55 fprintf(stderr, "Failed to send HTTP file\n");
65 /* dumb_increment protocol */
67 struct per_session_data__dumb_increment {
72 callback_dumb_increment(struct libwebsocket * wsi,
73 enum libwebsocket_callback_reasons reason,
74 void * user, void *in, size_t len)
77 char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
78 LWS_SEND_BUFFER_POST_PADDING];
79 unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING];
80 struct per_session_data__dumb_increment * pss = user;
84 case LWS_CALLBACK_ESTABLISHED:
88 case LWS_CALLBACK_SEND:
89 n = sprintf(p, "%d", pss->number++);
90 n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
92 fprintf(stderr, "ERROR writing to socket");
97 case LWS_CALLBACK_RECEIVE:
100 if (strcmp(in, "reset\n") == 0)
112 /* list of supported protocols and callbacks */
114 static const struct libwebsocket_protocols protocols[] = {
117 .callback = callback_http,
118 .per_session_data_size = 0,
121 .name = "dumb-increment-protocol",
122 .callback = callback_dumb_increment,
123 .per_session_data_size =
124 sizeof(struct per_session_data__dumb_increment),
131 static struct option options[] = {
132 { "help", no_argument, NULL, 'h' },
133 { "port", required_argument, NULL, 'p' },
134 { "ssl", no_argument, NULL, 's' },
138 int main(int argc, char **argv)
141 const char * cert_path =
142 LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
143 const char * key_path =
144 LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
146 fprintf(stderr, "libwebsockets test server\n"
147 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
148 "licensed under LGPL2.1\n");
151 n = getopt_long(argc, argv, "hp:", options, NULL);
162 fprintf(stderr, "Usage: test-server "
163 "[--port=<p>] [--ssl]\n");
169 cert_path = key_path = NULL;
171 if (libwebsocket_create_server(port, protocols,
172 cert_path, key_path, -1, -1) < 0) {
173 fprintf(stderr, "libwebsocket init failed\n");
177 /* just sit there until killed */