0

I'm trying to create a client using Libwebsockets library to connect to Socket.IO server. But client show error on console screen as below:

[2023/03/09 15:52:15:0950] N: lws_create_context: LWS: 4.3.2-unknown, NET CLI SRV H1 H2 WS ConMon IPV6-on
[2023/03/09 15:52:15:1122] N: __lws_lc_tag:  ++ [wsi|0|pipe] (1)
[2023/03/09 15:52:15:1453] N: __lws_lc_tag:  ++ [vh|0|default||-1] (1)
[2023/03/09 15:52:16:2184] N: __lws_lc_tag:  ++ [wsicli|0|GET/h1/default/localhost] (1)
[2023/03/09 15:52:16:2414] W: lws_plat_set_socket_options_ip: not implemented on windows platform

My client C++ code:

#include <libwebsockets.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static struct lws* web_socket = NULL;

#define EXAMPLE_RX_BUFFER_BYTES (10)

static int callback_example(struct lws* wsi, enum lws_callback_reasons reason, void* user, void* in, size_t len)
{
    switch (reason)
    {
    case LWS_CALLBACK_CLIENT_ESTABLISHED:
        lws_callback_on_writable(wsi);
        break;

    case LWS_CALLBACK_CLIENT_RECEIVE:
        /* Handle incomming messages here. */
        break;

    case LWS_CALLBACK_CLIENT_WRITEABLE:
    {
        //unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + EXAMPLE_RX_BUFFER_BYTES + LWS_SEND_BUFFER_POST_PADDING];
        //unsigned char* p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
        //size_t n = sprintf((char*)p, "%u", rand());
        //lws_write(wsi, p, n, LWS_WRITE_TEXT);
        break;
    }

    case LWS_CALLBACK_CLOSED:
    case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
        web_socket = NULL;
        break;

    default:
        break;
    }

    return 0;
}

enum protocols
{
    PROTOCOL_EXAMPLE = 0,
    PROTOCOL_COUNT
};

static struct lws_protocols protocols[] =
{
    {
        "http",
        callback_example,
        0, 0, 0, NULL, 0
    },
    LWS_PROTOCOL_LIST_TERM
};

int main(int argc, char* argv[])
{
    struct lws_context_creation_info info;
    memset(&info, 0, sizeof(info));

    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = protocols;
    info.gid = -1;
    info.uid = -1;
    info.options = 0;

    struct lws_context* context = lws_create_context(&info);

    time_t old = 0;
    while (1)
    {
        struct timeval tv;
        //gettimeofday(&tv, NULL);

        /* Connect if we are not connected to the server. */
        if (!web_socket)
        {
            struct lws_client_connect_info ccinfo = { 0 };
            ccinfo.context = context;
            ccinfo.address = "localhost";
            ccinfo.port = 3000;
            ccinfo.path = "/socket.io/?transport=websocket";
            ccinfo.host = ccinfo.address; //lws_canonical_hostname(context);
            ccinfo.origin = ccinfo.address;
            ccinfo.protocol = protocols[0].name;
            ccinfo.ssl_connection = 0;
            ccinfo.alpn = "http/1.1";
            ccinfo.method = "GET";
            web_socket = lws_client_connect_via_info(&ccinfo);
        }

        //if (tv.tv_sec != old)
        //{
        //  /* Send a random number to the server every second. */
        //  lws_callback_on_writable(web_socket);
        //  old = tv.tv_sec;
        //}

        lws_service(context, /* timeout_ms = */ 250);
    }

    lws_context_destroy(context);

    return 0;
}

And my Socket.IO server code, and it is working well:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="body">
    <script src="http://localhost:3000/socket.io/socket.io.js"></script>
    <script>
            var socket = io.connect("http://localhost:3000");
            socket.on("connect", (sk) => {
              alert("connected!");
            });
            socket.on('disconnect', function(){
                alert('disconnect')
                location.reload();
            });
            socket.on("booth-connect:110003", function(data) {
                console.log(data);
                alert(data);
                data = JSON.parse(data);
            });
    </script>
</body>
</html>

Where did my code go wrong? Can anybody help me?

Mathieu
  • 8,840
  • 7
  • 32
  • 45
Don Le
  • 1
  • 1

0 Answers0