-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
// client: 1006 CLOSE_ABNORMAL
// test on windows
// wireshark no found: HTTP/1.1 101 Switching Protocols
///////////////////////////////////////////
// server
#include <libwebsockets.h>
#include <string.h>
#include <stdio.h>
static int callback_lws_protocol(struct lws* wsi, enum lws_callback_reasons reason,
void* user, void* in, size_t len)
{
switch (reason)
{
case LWS_CALLBACK_ESTABLISHED:
{
lwsl_notice("Connection established.\n");
const char* protocol_name = lws_get_protocol(wsi)->name;
if (protocol_name && protocol_name[0] != '\0') {
lwsl_notice("Client used protocol: %s\n", protocol_name);
}
else {
lwsl_notice("Client did NOT specify a subprotocol (Using default handler).\n");
}
}
break;
case LWS_CALLBACK_RECEIVE:
{
if (lws_write(wsi, (unsigned char*)in, len, LWS_WRITE_TEXT) < 0) {
lwsl_err("Error writing to socket\n");
}
lwsl_notice("Received data and echoed back: %.*s\n", (int)len, (char*)in);
}
break;
case LWS_CALLBACK_CLOSED:
lwsl_notice("Connection closed.\n");
break;
default:
break;
}
return 0;
}
static const struct lws_protocols protocols[] = {
{
"",
callback_lws_protocol,
0,
4096,
},
{
"example-protocol",
callback_lws_protocol,
0,
4096,
},
{ NULL, NULL, 0, 0 }
};
//
//static const struct lws_protocol_vhost_options pvo_ops = {
// NULL,
// NULL,
// "default", /* pvo name /
// "" / ignored /
//};
//
//static const struct lws_protocol_vhost_options pvo = {
// NULL, / "next" pvo linked-list /
// &pvo_ops, / "child" pvo linked-list /
// "example-protocol", / protocol name we belong to on this vhost /
// "" / ignored */
//};
int main(void)
{
int port = 7681;
int opts = 0;// LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; // LWS_SERVER_OPTION_VLHOSTS
struct lws_context_creation_info info;
memset(&info, 0, sizeof(info));
info.port = port;
info.protocols = protocols;
info.gid = -1;
info.uid = -1;
info.options = opts;
//info.pvo = &pvo;
lws_set_log_level(LLL_NOTICE | LLL_WARN | LLL_ERR, NULL);
struct lws_context* context = lws_create_context(&info);
if (!context) {
lwsl_err("lws_create_context failed.\n");
return 1;
}
lwsl_notice("LWS server started on port %d. Press Ctrl+C to exit.\n", port);
while (1) {
lws_service(context, 50);
}
lws_context_destroy(context);
return 0;
}