blob: 4f13664eff807cab269de5566f8e137fcdcddf80 [file] [log] [blame]
Andy Green775c0dd2010-10-29 14:15:22 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4
5#include "libwebsockets.h"
6
7#define PORT 7681
8
Andy Green4ea60062010-10-30 12:15:07 +01009static int websocket_callback(struct libwebsocket * wsi,
10 enum libwebsocket_callback_reasons reason, void *in, size_t len)
Andy Green775c0dd2010-10-29 14:15:22 +010011{
12 int n;
Andy Green4ea60062010-10-30 12:15:07 +010013 char buf[LWS_SEND_BUFFER_PRE_PADDING + 256 + LWS_SEND_BUFFER_POST_PADDING];
Andy Green775c0dd2010-10-29 14:15:22 +010014 static int bump;
15
16 switch (reason) {
17 case LWS_CALLBACK_ESTABLISHED:
18 fprintf(stderr, "Websocket connection established\n");
19 break;
20
21 case LWS_CALLBACK_CLOSED:
22 fprintf(stderr, "Websocket connection closed\n");
23 break;
24
25 case LWS_CALLBACK_SEND:
26 sleep(1);
Andy Green4ea60062010-10-30 12:15:07 +010027 n = sprintf(&buf[LWS_SEND_BUFFER_PRE_PADDING], "%d\n", bump++);
28 n = libwebsocket_write(wsi, (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING], n, 0);
Andy Green775c0dd2010-10-29 14:15:22 +010029 if (n < 0) {
30 fprintf(stderr, "ERROR writing to socket");
31 exit(1);
32 }
33 break;
34
35 case LWS_CALLBACK_RECEIVE:
Andy Green4ea60062010-10-30 12:15:07 +010036 fprintf(stderr, "Received %d bytes payload\n", (int)len);
Andy Green775c0dd2010-10-29 14:15:22 +010037 break;
38 }
39 return 0;
40}
41
42
43int main(int argv, char **argc)
44{
45 if (libwebsocket_create_server(PORT, websocket_callback) < 0) {
46 fprintf(stderr, "libwebsocket init failed\n");
47 return -1;
48 }
49
50 fprintf(stderr, "Listening on port %d\n", PORT);
51
52 while (1)
53 sleep(1);
54
55 return 0;
56}