Created
March 8, 2016 07:46
-
-
Save tanuva/d48bbba1a7ab736edc09 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
#include <iostream> | |
#include <sstream> | |
#include <libwebsockets.h> | |
const size_t MaxFragmentSize = 65536; | |
const int MaxSendChunkSize = 32768; | |
lws_context *m_context; | |
lws *m_wsi; | |
bool abortConnection = false; | |
std::string m_hostName = "localhost"; | |
unsigned int m_port = 1234; | |
static int lws_callback( lws *wsi, enum lws_callback_reasons reason, | |
void * /*user*/, void *in, size_t len ) | |
{ | |
switch ( reason ) { | |
case LWS_CALLBACK_CLOSED: | |
std::cout << "closed\n"; | |
break; | |
case LWS_CALLBACK_CLIENT_WRITEABLE: | |
std::cout << "writeable\n"; | |
break; | |
case LWS_CALLBACK_CLIENT_RECEIVE: | |
std::cout << "receive\n"; | |
break; | |
case LWS_CALLBACK_CLIENT_ESTABLISHED: | |
std::cout << "client established\n"; | |
break; | |
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: | |
std::cout << "client connection error\n"; | |
break; | |
case LWS_CALLBACK_WSI_CREATE: | |
case LWS_CALLBACK_WSI_DESTROY: | |
case LWS_CALLBACK_PROTOCOL_INIT: | |
case LWS_CALLBACK_PROTOCOL_DESTROY: | |
case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: | |
case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: | |
// Filter these reasons since they're not interesting for us. | |
break; | |
case LWS_CALLBACK_GET_THREAD_ID: | |
case LWS_CALLBACK_ADD_POLL_FD: | |
case LWS_CALLBACK_DEL_POLL_FD: | |
case LWS_CALLBACK_CHANGE_MODE_POLL_FD: | |
case LWS_CALLBACK_LOCK_POLL: | |
case LWS_CALLBACK_UNLOCK_POLL: | |
break; | |
default: | |
std::cout << "unhandled: " << reason << std::endl; | |
break; | |
} | |
return abortConnection ? -1 : 0; | |
} | |
static lws_protocols protocols[] = { | |
{ | |
NULL, /* name */ | |
lws_callback, /* callback */ | |
0, /* data */ | |
MaxFragmentSize, /* receive and send buffer size, make sure its large enough to hold our data at all times */ | |
0, | |
0 | |
}, | |
{ | |
NULL, NULL, 0, 0, 0, 0 /* End of list */ | |
} | |
}; | |
static lws_context *create_lws_context( const int port, void *user ) | |
{ | |
// lwss-related initialization | |
lws_context_creation_info info; | |
memset( &info, 0, sizeof info ); | |
info.port = port; | |
info.iface = NULL; | |
info.protocols = protocols; | |
info.gid = -1; | |
info.uid = -1; | |
info.options = 0; | |
info.user = user; | |
return lws_create_context( &info ); | |
} | |
int main(int, char**) | |
{ | |
std::cout << "Blah!\n"; | |
m_context = create_lws_context( CONTEXT_PORT_NO_LISTEN, NULL ); | |
if( !m_context ) { | |
std::cout << "create context failed\n"; | |
return 1; | |
} | |
std::stringstream hostPort; | |
hostPort << m_hostName << ':' << m_port; | |
m_wsi = lws_client_connect(m_context, | |
m_hostName.c_str(), | |
m_port, | |
0, // 0: no SSL, 1: SSL, 2: SSL + allow selfsigned | |
"/", // TODO make this configurable | |
hostPort.str().c_str(), | |
NULL/*"localhost"*/, | |
NULL, | |
-1); | |
if ( m_wsi == NULL ) { | |
std::cout << "client connect failed\n"; | |
lws_context_destroy( m_context ); | |
m_context = NULL; | |
return 1; | |
} | |
while ( !abortConnection ) { | |
if ( lws_service( m_context, 500 ) < 0 ) { | |
std::cout << "lws_service != 0\n"; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment