{"id":4647,"date":"2022-02-01T18:32:06","date_gmt":"2022-02-01T09:32:06","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4647"},"modified":"2022-02-16T15:19:07","modified_gmt":"2022-02-16T06:19:07","slug":"libwebsockets","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4647","title":{"rendered":"[Deprecated] C libwebsockets"},"content":{"rendered":"<h1>[Deprecated] C libwebsockets<\/h1>\n<p>\ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \ub09c\uc7a1\ud558\ub2e4&#8230;&#8230;<\/p>\n<p><a href=\"https:\/\/fzco.wackymango.net\/web-socket-client\/\">\ucc38\uc870<\/a><\/p>\n<p>. . . . . . . . . . . . . . . . . . . . . . . .<br \/>\n. . . . . . . . . . . . . . . . . . . . . . . .<br \/>\n. . . . . . . . . . . . . . . . . . . . . . . .<\/p>\n<h2>branch v4.3-stable<\/h2>\n<pre><code class=\"language-bash\">git clone https:\/\/github.com\/warmcat\/libwebsockets.git -b v4.3-stable\ncd libwebsockets\nsudo apt-get install libssl-dev\nmkdir build\ncd build\ncmake ..\nmake\nsudo make install\nsudo ldconfig<\/code><\/pre>\n<h2>\uc11c\ubc84 \uc0dd\uc131<\/h2>\n<p><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4668\">https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4668<\/a> \uc5d0\uc11c nodeJS \ub85c \uc11c\ubc84\ub97c \uad6c\uc131\ud55c\ub2e4.<\/p>\n<h2>\ud074\ub77c\uc774\uc5b8\ud2b8 \uc0dd\uc131<\/h2>\n<pre><code class=\"language-c\">#include &lt;libwebsockets.h&gt;\n\nstatic int callback( lws*, lws_callback_reasons reason, void* user, void* p, size_t z )\n{\n    switch( reason ) {\n        case LWS_CALLBACK_CLIENT_ESTABLISHED:\n            \/\/ Connected to server.\n            break;\n\n        case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:\n            \/\/ Unable to connect to server.  Check protocol?\n            break;\n\n        case LWS_CALLBACK_CLOSED:\n            \/\/ Connection to server closed.\n            break;\n\n        case LWS_CALLBACK_CLIENT_RECEIVE:\n            \/\/ Data of z bytes is accessible via pointer p.\n            fprintf(stdout, &quot;%s\\n&quot;, (const char*)p); fflush(stdout);\n            break;\n\n        case LWS_CALLBACK_CLIENT_WRITEABLE:\n            \/\/ Now is your chance to send stuff to server, use API\n            \/\/ lws_write() to do so.\n            break;\n\n        default: break;\n    }\n    return 0;\n}\n\nstatic lws_protocols protocols[] = {\n    {\n        &quot;test-protocol&quot;, \/\/ protocol name\n        callback,        \/\/ callback for protocol\n        0,               \/\/ per session data size\n        0,               \/\/ receive buffer size\n        0,               \/\/ user defined ID, not used by LWS\n        nullptr,         \/\/ user defined pointer\n        0                \/\/ transmit buffer size\n    },\n    { nullptr, nullptr, 0, 0, 0, nullptr, 0 } \/\/ protocol list terminator\n};\n\nint ws_parse_uri(const char *_url, struct lws_client_connect_info *ccinfo) {\n    char url[1024];\n    const char *protocol, *_path, *_address;\n    char *path, *address;\n\n    lws_strncpy(url, _url, sizeof(url));\n\n    if (lws_parse_uri(url, &amp;protocol, &amp;_address, &amp;ccinfo-&gt;port, &amp;_path)) {\n        return 1;\n    }\n\n    if (!strcmp(protocol, &quot;https&quot;) || !strcmp(protocol, &quot;wss&quot;)) {\n        ccinfo-&gt;protocol = &quot;wss&quot;;\n        ccinfo-&gt;ssl_connection = LCCSCF_USE_SSL |\n                                 LCCSCF_ALLOW_SELFSIGNED |\n                                 LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;\n    } else {\n        ccinfo-&gt;protocol = &quot;ws&quot;;\n        ccinfo-&gt;ssl_connection = 0;\n    }\n\n    address = (char *) malloc(sizeof (char) * (strlen(_address) + 1));\n    memcpy(address, _address, (sizeof (char) * (strlen(_address) + 1)));\n\n    if (_path[0] != &#039;\/&#039;) {\n        path = (char *) malloc(sizeof (char) * (strlen(_path) + 2));\n        path[0] = &#039;\/&#039;;\n        memcpy(path + 1, _path, (sizeof (char) * (strlen(_path) + 1)));\n    } else {\n        path = (char *) malloc(sizeof (char) * (strlen(_path) + 1));\n        memcpy(path, _path, sizeof (char) * (strlen(_path) + 1));\n    }\n\n    ccinfo-&gt;address = address;\n    ccinfo-&gt;path = path;\n\n    return 0;\n}\n\nint main(int argc, char **argv) {\n\n    \/\/ ~\/work\/example\/websocket\/libwebsockets\n\n    \/\/ ====================================================\n    \/\/ creation info\n    lws_context_creation_info cinfo;\n    memset(&amp;cinfo, 0, sizeof(cinfo));\n    cinfo.port = CONTEXT_PORT_NO_LISTEN;\n    cinfo.protocols = protocols;\n    cinfo.extensions = nullptr;\n    cinfo.gid = -1;\n    cinfo.uid = -1;\n    cinfo.user = nullptr;\n    cinfo.options = 0;\n\n    \/\/ ====================================================\n    \/\/ context\n    lws_context *context = lws_create_context(&amp;cinfo);\n\n    \/\/ ====================================================\n    \/\/ client connect info\n    const char *url = &quot;ws:\/\/localhost:30001\/index.html&quot;;\n    struct lws_client_connect_info ccinfo;\n    memset(&amp;ccinfo, 0, sizeof(ccinfo));\n    ws_parse_uri(url, &amp;ccinfo);\n\n    if(context) {\n        ccinfo.context = context;\n        ccinfo.protocol = &quot;test-protocol&quot;;     \/\/ protocol\n        ccinfo.ietf_version_or_minus_one = -1;   \/\/ IETF version\n\n        \/\/ ====================================================\n        \/\/ client connect\n        lws *socket = lws_client_connect_via_info(&amp;ccinfo);\n        if( socket ) {\n            bool running = true;\n            while( running ) {\n                lws_service(context, 0); \/\/ 0 : timeout is ignored\n                lws_callback_on_writable_all_protocol(context, &amp;protocols[0]);\n            }\n        }\n        lws_context_destroy(context);\n    }\n\n    \/\/ printf(&quot;ccinfo.protocol : %s\\n&quot;, ccinfo.protocol);\n    \/\/ printf(&quot;ccinfo.address : %s\\n&quot;, ccinfo.address);\n    \/\/ printf(&quot;ccinfo.port : %i\\n&quot;, ccinfo.port);\n    \/\/ printf(&quot;ccinfo.path : %s\\n&quot;, ccinfo.path);\n\n    free((void *) ccinfo.address);\n    free((void *) ccinfo.path);\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">gcc main.cpp -lwebsockets\n.\/a.out<\/code><\/pre>\n<h2>\uc8fc\uc758\uc0ac\ud56d<\/h2>\n<p><a href=\"https:\/\/libwebsockets.org\/lws-api-doc-master\/html\/md_README.coding.html\">\ucc38\uc870<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Deprecated] C libwebsockets \ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \ub09c\uc7a1\ud558\ub2e4&#8230;&#8230; \ucc38\uc870 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4647\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-4647","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4647"}],"version-history":[{"count":18,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4647\/revisions"}],"predecessor-version":[{"id":4732,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4647\/revisions\/4732"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}