Created
March 3, 2014 15:48
-
-
Save sukinull/9327778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by sukinull on 2/14/14. | |
// Copyright (c) 2014 sukinull. All rights reserved. | |
// | |
// Simplest libwebsocks example | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <getopt.h> | |
#include <string.h> | |
#include "libwebsockets.h" | |
//static unsigned int opts; | |
static int was_closed; | |
static int deny_deflate; | |
enum signaling_protocols { | |
PROTOCOL_SIGNALING, | |
/* always last */ | |
DEMO_PROTOCOL_COUNT | |
}; | |
static int | |
callback_signaling(struct libwebsocket_context * this, | |
struct libwebsocket *wsi, | |
enum libwebsocket_callback_reasons reason, | |
void *user, void *in, size_t len) | |
{ | |
int wlen = 0, l; | |
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + | |
LWS_SEND_BUFFER_POST_PADDING]; | |
switch (reason) { | |
case LWS_CALLBACK_CLOSED: | |
lwsl_notice("Client has been closed\n"); | |
was_closed = 1; | |
break; | |
case LWS_CALLBACK_CLIENT_ESTABLISHED: | |
lwsl_notice("Client has connected\n"); | |
break; | |
case LWS_CALLBACK_CLIENT_RECEIVE: | |
lwsl_notice("Client RX: %s", (char *)in); | |
//libwebsocket_callback_on_writable(this, wsi); | |
break; | |
case LWS_CALLBACK_CLIENT_WRITEABLE: | |
l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING], | |
"{ \"r\": \"c #%06X %d %d %d\" }", | |
(int)random() & 0xffffff, | |
(int)random() % 500, | |
(int)random() % 250, | |
(int)random() % 24); | |
wlen = libwebsocket_write(wsi, | |
&buf[LWS_SEND_BUFFER_PRE_PADDING], l, LWS_WRITE_TEXT); | |
break; | |
default: | |
break; | |
} | |
return 0; | |
} | |
static struct libwebsocket_protocols protocols[] = { | |
{ | |
"signaling-protocol", | |
callback_signaling, | |
0, | |
}, | |
{ /* end of list */ | |
NULL, | |
NULL, | |
0 | |
} | |
}; | |
int main(int argc, char **argv) | |
{ | |
int n; | |
int port = 8000; | |
int use_ssl = 0; | |
struct libwebsocket_context *context; | |
const char *address = "linode.sukinull.tw"; | |
struct libwebsocket *wsi_dumb; | |
// struct libwebsocket *wsi_mirror; | |
int ietf_version = -1; /* latest */ | |
/* | |
* 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. | |
*/ | |
/* old version: | |
context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL, | |
protocols, libwebsocket_internal_extensions, | |
NULL, NULL, -1, -1, 0); | |
*/ | |
//-- new Version: | |
struct lws_context_creation_info info; | |
memset(&info, 0, sizeof info); | |
info.port = CONTEXT_PORT_NO_LISTEN; | |
info.protocols = protocols; | |
#ifndef LWS_NO_EXTENSIONS | |
info.extensions = libwebsocket_get_internal_extensions(); | |
#endif | |
//if (!use_ssl) { | |
info.ssl_cert_filepath = NULL; | |
info.ssl_private_key_filepath = NULL; | |
//} else { | |
// info.ssl_cert_filepath = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem"; | |
// info.ssl_private_key_filepath = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem"; | |
//} | |
info.gid = -1; | |
info.uid = -1; | |
context = libwebsocket_create_context(&info); | |
//------- | |
if (context == NULL) { | |
fprintf(stderr, "Creating libwebsocket context failed\n"); | |
return 1; | |
} | |
/* create a client websocket using dumb increment protocol */ | |
wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl, | |
"/", address, address, | |
protocols[PROTOCOL_SIGNALING].name, ietf_version); | |
if (wsi_dumb == 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 | |
*/ | |
libwebsocket_callback_on_writable(context, wsi_dumb); | |
n = 0; | |
while (n >= 0 && !was_closed) | |
n = libwebsocket_service(context, 1000); | |
fprintf(stderr, "Exiting\n"); | |
libwebsocket_context_destroy(context); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment