Created
September 3, 2018 14:40
-
-
Save davehorton/5136b200d8eb3c514698d5300b87aa76 to your computer and use it in GitHub Desktop.
callback with LWS_CALLBACK_PROTOCOL_INIT never invoked ?
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 <libwebsockets.h> | |
#include <string.h> | |
#include <signal.h> | |
#include <inttypes.h> | |
#include "read_wav.h" | |
#define LWS_PLUGIN_STATIC | |
struct vhd_asapp { | |
struct lws_context *context; | |
struct lws_vhost *vhost; | |
struct lws *client_wsi; | |
}; | |
int ws_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len); | |
int connect_client(struct vhd_asapp *vhd); | |
static struct lws_protocols protocols[] = { | |
{NULL, ws_callback, 0, 128}, | |
{ NULL, NULL, 0, 0 } /* terminator */ | |
}; | |
static int interrupted = 0; | |
static int16_t* samples; | |
void sigint_handler(int sig) | |
{ | |
interrupted = 1; | |
} | |
int main(int argc, const char **argv) | |
{ | |
struct lws_context_creation_info info; | |
struct lws_context *context; | |
const char *f; | |
int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_CLIENT | LLL_DEBUG | |
/* for LLL_ verbosity above NOTICE to be built into lws, | |
* lws must have been configured and built with | |
* -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ | |
/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ | |
/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ | |
/* | LLL_DEBUG */; | |
signal(SIGINT, sigint_handler); | |
lws_set_log_level(logs, NULL); | |
lwsl_user("LWS minimal transcribe function\n"); | |
lwsl_user(" %s -f <wav file>\n", argv[0]); | |
if ((f = lws_cmdline_option(argc, argv, "-f"))) { | |
if (wavread(f, &samples) < 0) { | |
lwsl_err("exiting..\n"); | |
return -1; | |
} | |
} | |
else { | |
lwsl_err("-f option is required\n"); | |
return -1; | |
} | |
memset(&info, 0, sizeof info); | |
info.port = CONTEXT_PORT_NO_LISTEN; | |
info.protocols = protocols; | |
info.pt_serv_buf_size = 32 * 1024; | |
context = lws_create_context(&info); | |
if (!context) { | |
lwsl_err("lws init failed\n"); | |
return 1; | |
} | |
lwsl_user("lws init succeeded, starting service loop\n"); | |
while (n >= 0 && !interrupted) | |
n = lws_service(context, 1000); | |
lws_context_destroy(context); | |
lwsl_user("Completed %s\n", interrupted == 2 ? "OK" : "failed"); | |
return 0; | |
} | |
int ws_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { | |
struct vhd_asapp *vhd = (struct vhd_asapp *) | |
lws_protocol_vh_priv_get(lws_get_vhost(wsi), | |
lws_get_protocol(wsi)); | |
lwsl_user("ws_callback called, reason: %d\n", reason); | |
switch (reason) { | |
case LWS_CALLBACK_PROTOCOL_INIT: | |
if (connect_client(vhd)) { | |
lwsl_user("failed to connect to server"); | |
} | |
break; | |
case LWS_CALLBACK_CLIENT_ESTABLISHED: | |
lwsl_user("connected successfully"); | |
lws_callback_on_writable(wsi); | |
break; | |
case LWS_CALLBACK_CLIENT_WRITEABLE: | |
lwsl_user("socket writable"); | |
break; | |
case LWS_CALLBACK_CLIENT_RECEIVE: | |
lwsl_user("received message"); | |
break; | |
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: | |
lwsl_err("CLIENT_CONNECTION_ERROR: %s\n", | |
in ? (char *)in : "(null)"); | |
break; | |
case LWS_CALLBACK_CLIENT_CLOSED: | |
lwsl_user("connection closed"); | |
break; | |
/* rate-limited client connect retries */ | |
case LWS_CALLBACK_USER: | |
lwsl_notice("%s: LWS_CALLBACK_USER\n", __func__); | |
break; | |
default: | |
break; | |
} | |
return 0; | |
} | |
int connect_client(struct vhd_asapp *vhd) | |
{ | |
struct lws_client_connect_info i; | |
memset(&i, 0, sizeof(i)); | |
i.context = vhd->context; | |
i.port = 3001; | |
i.address = "localhost"; | |
i.path = "/"; | |
i.host = i.address; | |
i.origin = i.address; | |
i.ssl_connection = 0; | |
i.vhost = vhd->vhost; | |
i.pwsi = &vhd->client_wsi; | |
lwsl_user("Attempting to connect to %s:%d\n", i.address, i.port); | |
return !lws_client_connect_via_info(&i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment