};
-int libwebsocket_create_server(int port, int (*callback)(struct libwebsocket *, enum libwebsocket_callback_reasons, void *, size_t))
+int libwebsocket_create_server(int port,
+ int (*callback)(struct libwebsocket *,
+ enum libwebsocket_callback_reasons, void *, size_t),
+ int protocol)
{
int n;
int sockfd, newsockfd;
}
wsi->callback = callback;
-// wsi->ietf_spec_revision = 0;
- wsi->ietf_spec_revision = 76;
+ switch (protocol) {
+ case 0:
+ case 2:
+ case 76:
+ fprintf(stderr, "Using protocol v%d\n", protocol);
+ wsi->ietf_spec_revision = protocol;
+ break;
+ default:
+ fprintf(stderr, "protocol %d not supported (try 0 2 or 76)\n",
+ protocol);
+ return -1;
+ }
/* sit there listening for connects, accept and spawn session servers */
serv_addr.sin_port = htons(port);
n = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (n < 0) {
- fprintf(stderr, "ERROR on binding %d %d\n", n, errno);
+ fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", port, n,
+ errno);
return -1;
}
if (n)
return 0;
-
+ fprintf(stderr, "Listening on port %d\n", port);
+
listen(sockfd, 5);
while (1) {
p += wsi->utf8_token[WSI_TOKEN_HOST].token_len;
strcpy(p, wsi->utf8_token[WSI_TOKEN_GET_URI].token);
p += wsi->utf8_token[WSI_TOKEN_GET_URI].token_len;
- strcpy(p, "\x0d\x0aSec-WebSocket-Protocol: ");
- p += strlen("\x0d\x0aSec-WebSocket-Protocol: ");
+
if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token) {
+ strcpy(p, "\x0d\x0aSec-WebSocket-Protocol: ");
+ p += strlen("\x0d\x0aSec-WebSocket-Protocol: ");
strcpy(p, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
p += wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len;
- } else {
- strcpy(p, "none");
- p += strlen("none");
}
+
strcpy(p, "\x0d\x0a\x0d\x0a");
p += strlen("\x0d\x0a\x0d\x0a");
}
break;
}
+
+ for (n = 0; n < (len + pre + post); n++)
+ fprintf(stderr, "%02X ", buf[n - pre]);
+
+ fprintf(stderr, "\n");
n = write(wsi->sock, buf - pre, len + pre + post);
if (n < 0) {
extern int libwebsocket_create_server(int port,
int (*callback)(struct libwebsocket *,
- enum libwebsocket_callback_reasons, void *, size_t));
+ enum libwebsocket_callback_reasons, void *, size_t),
+ int protocol);
/*
* IMPORTANT NOTICE!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <getopt.h>
#include "libwebsockets.h"
-#define PORT 7681
+/*
+ * libwebsocket Example server Copyright 2010 Andy Green <andy@warmcat.com>
+ *
+ * Shows how to use libwebsocket
+ */
+
+
+static int port = 7681;
+static int ws_protocol = 76;
static int websocket_callback(struct libwebsocket * wsi,
enum libwebsocket_callback_reasons reason, void *in, size_t len)
int n;
char buf[LWS_SEND_BUFFER_PRE_PADDING + 256 + LWS_SEND_BUFFER_POST_PADDING];
static int bump;
+ static int slow;
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
fprintf(stderr, "Websocket connection established\n");
+ slow = 500;
break;
case LWS_CALLBACK_CLOSED:
break;
case LWS_CALLBACK_SEND:
- sleep(1);
- n = sprintf(&buf[LWS_SEND_BUFFER_PRE_PADDING], "%d\n", bump++);
+ slow--;
+ if (slow) {
+ usleep(10000);
+ break;
+ }
+ slow = 100;
+ n = sprintf(&buf[LWS_SEND_BUFFER_PRE_PADDING], "%d", bump++);
n = libwebsocket_write(wsi, (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING], n, 0);
if (n < 0) {
fprintf(stderr, "ERROR writing to socket");
return 0;
}
+static struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "port", required_argument, NULL, 'p' },
+ { "protocol", required_argument, NULL, 'r' },
+ { NULL, 0, 0, 0 }
+};
-int main(int argv, char **argc)
+int main(int argc, char **argv)
{
- if (libwebsocket_create_server(PORT, websocket_callback) < 0) {
+ int n = 0;
+
+ fprintf(stderr, "libwebsockets test server\nCopyright 2010 Andy Green <andy@warmcat.com> licensed under GPL2\n");
+
+ while (n >= 0) {
+ n = getopt_long(argc, argv, "hp:r:", options, NULL);
+ if (n < 0)
+ continue;
+ switch (n) {
+ case 'p':
+ port = atoi(optarg);
+ break;
+ case 'r':
+ ws_protocol = atoi(optarg);
+ break;
+ case 'h':
+ fprintf(stderr, "Usage: test-server [--port=<p>] [--protocol=<v>]\n");
+ exit(1);
+ }
+
+ }
+
+ if (libwebsocket_create_server(port, websocket_callback, ws_protocol) < 0) {
fprintf(stderr, "libwebsocket init failed\n");
return -1;
}
-
- fprintf(stderr, "Listening on port %d\n", PORT);
-
+
while (1)
sleep(1);