{"action":"closed","issue":{"url":"https://api.github.com/repos/warmcat/libwebsockets/issues/892","repository_url":"https://api.github.com/repos/warmcat/libwebsockets","labels_url":"https://api.github.com/repos/warmcat/libwebsockets/issues/892/labels{/name}","comments_url":"https://api.github.com/repos/warmcat/libwebsockets/issues/892/comments","events_url":"https://api.github.com/repos/warmcat/libwebsockets/issues/892/events","html_url":"https://github.com/warmcat/libwebsockets/issues/892","id":227681805,"number":892,"title":"Very first program","user":{"login":"Yatekii","id":529795,"avatar_url":"https://avatars3.githubusercontent.com/u/529795?v=3","gravatar_id":"","url":"https://api.github.com/users/Yatekii","html_url":"https://github.com/Yatekii","followers_url":"https://api.github.com/users/Yatekii/followers","following_url":"https://api.github.com/users/Yatekii/following{/other_user}","gists_url":"https://api.github.com/users/Yatekii/gists{/gist_id}","starred_url":"https://api.github.com/users/Yatekii/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Yatekii/subscriptions","organizations_url":"https://api.github.com/users/Yatekii/orgs","repos_url":"https://api.github.com/users/Yatekii/repos","events_url":"https://api.github.com/users/Yatekii/events{/privacy}","received_events_url":"https://api.github.com/users/Yatekii/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-05-10T13:34:16Z","updated_at":"2017-05-10T14:34:45Z","closed_at":"2017-05-10T14:34:44Z","body":"Hi!\r\n\r\nI have found some sample code and since I was unable to find a basic example from the \"official docs\" I tried to make it work.\r\n\r\n```\r\n#include <math.h>\r\n#include <stdio.h>\r\n#include <unistd.h>\r\n#include <stdlib.h>\r\n#include <string.h>\r\n#include <libwebsockets.h>\r\n\r\n#define PI 3.14159265\r\n\r\nstatic int callback_http(struct lws_context * this,\r\n                         struct lws *wsi,\r\n                         enum lws_callback_reasons reason, void *user,\r\n                         void *in, size_t len)\r\n{\r\n    return 0;\r\n}\r\n\r\nstatic int callback_dumb_increment(struct lws_context * this,\r\n                                   struct lws *wsi,\r\n                                   enum lws_callback_reasons reason,\r\n                                   void *user, void *in, size_t len)\r\n{\r\n    printf(\"iekekekek\");\r\n    switch (reason) {\r\n        case LWS_CALLBACK_ESTABLISHED: // just log message that someone is connecting\r\n            printf(\"connection established\\n\");\r\n            break;\r\n        case LWS_CALLBACK_RECEIVE: { // the funny part\r\n            // create a buffer to hold our response\r\n            // it has to have some pre and post padding. You don't need to care\r\n            // what comes there, libwebsockets will do everything for you. For more info see\r\n            // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/libwebsockets.h#n597\r\n            unsigned char *buf = (unsigned char*) malloc(LWS_SEND_BUFFER_PRE_PADDING + len +\r\n                                                         LWS_SEND_BUFFER_POST_PADDING);\r\n           \r\n            int i;\r\n           \r\n            // pointer to `void *in` holds the incomming request\r\n            // we're just going to put it in reverse order and put it in `buf` with\r\n            // correct offset. `len` holds length of the request.\r\n            for (i=0; i < len; i++) {\r\n                buf[LWS_SEND_BUFFER_PRE_PADDING + (len - 1) - i ] = ((char *) in)[i];\r\n            }\r\n           \r\n            // log what we recieved and what we're going to send as a response.\r\n            // that disco syntax `%.*s` is used to print just a part of our buffer\r\n            // http://stackoverflow.com/questions/5189071/print-part-of-char-array\r\n            printf(\"received data: %s, replying: %.*s\\n\", (char *) in, (int) len,\r\n                 buf + LWS_SEND_BUFFER_PRE_PADDING);\r\n           \r\n            // send response\r\n            // just notice that we have to tell where exactly our response starts. That's\r\n            // why there's `buf[LWS_SEND_BUFFER_PRE_PADDING]` and how long it is.\r\n            // we know that our response has the same length as request because\r\n            // it's the same message in reverse order.\r\n            lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], len, LWS_WRITE_TEXT);\r\n           \r\n            // release memory back into the wild\r\n            free(buf);\r\n            break;\r\n        }\r\n        default:\r\n            break;\r\n    }\r\n   \r\n   \r\n    return 0;\r\n}\r\n\r\nstatic struct lws_protocols protocols[] = {\r\n    /* first protocol must always be HTTP handler */\r\n    {\r\n        \"http-only\",   // name\r\n        (lws_callback_function*)callback_http, // callback\r\n        0              // per_session_data_size\r\n    },\r\n    {\r\n        \"dumb-increment-protocol\", // protocol name - very important!\r\n        (lws_callback_function*)callback_dumb_increment,   // callback\r\n        0                          // we don't use any per session data\r\n\r\n    },\r\n    {\r\n        NULL, NULL, 0   /* End of list */\r\n    }\r\n};\r\n \r\nint main(void) {\r\n    // server url will be http://localhost:9000\r\n    struct lws_context *context;\r\n    struct lws_context_creation_info info;\r\n    memset(&info, 0, sizeof info);\r\n    info.port = 50090;\r\n\r\n    // create libwebsocket context representing this server\r\n    context = lws_create_context(&info);\r\n   \r\n    if (context == NULL) {\r\n        fprintf(stderr, \"libwebsocket init failed\\n\");\r\n        return -1;\r\n    }\r\n   \r\n    printf(\"starting server...\\n\");\r\n   \r\n    // infinite loop, to end this server send SIGTERM. (CTRL+C)\r\n    while (1) {\r\n        lws_service(context, 50);\r\n        // lws_service will process all waiting events with their\r\n        // callback functions and then wait 50 ms.\r\n        // (this is a single threaded webserver and this will keep our server\r\n        // from generating load while there are not requests to process)\r\n    }\r\n   \r\n    lws_context_destroy(context);\r\n   \r\n    return 0;\r\n}\r\n```\r\n\r\nI can compile it and run the server which gives the following output:\r\n\r\n```\r\n[2017/05/10 15:28:15:3007] NOTICE: Initial logging level 7\r\n[2017/05/10 15:28:15:3007] NOTICE: Libwebsockets version: 2.2.0 brew@ElCapitan.local-\r\n[2017/05/10 15:28:15:3007] NOTICE: IPV6 not compiled in\r\n[2017/05/10 15:28:15:3007] NOTICE: libev support not compiled in\r\n[2017/05/10 15:28:15:3007] NOTICE: libuv support not compiled in\r\n[2017/05/10 15:28:15:3008] NOTICE:  Threads: 1 each 4864 fds\r\n[2017/05/10 15:28:15:3008] NOTICE:  mem: platform fd map: 38912 bytes\r\n[2017/05/10 15:28:15:3009] NOTICE:  Compiled with OpenSSL support\r\n[2017/05/10 15:28:15:3009] NOTICE:  SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT\r\n[2017/05/10 15:28:15:3009] NOTICE: Creating Vhost 'default' port 50090, 1 protocols, IPv6 off\r\n[2017/05/10 15:28:15:3026] NOTICE:  Listening on port 50090\r\n[2017/05/10 15:28:15:3026] NOTICE:  mem: per-conn:          568 bytes + protocol rx buf\r\n[2017/05/10 15:28:15:3027] NOTICE:  canonical_hostname = noahs-mbp.edu.ds.fhnw.ch\r\n[2017/05/10 15:28:15:3027] WARN: setgid: Operation not permitted\r\n[2017/05/10 15:28:15:3030] WARN: setuid: Operation not permitted\r\nstarting server...\r\n```\r\n\r\nSounds ok to me.\r\n\r\nI have this js demo-testcode:\r\n\r\n```\r\n<!DOCTYPE html>\r\n<html>\r\n    <head>\r\n        <meta charset=\"utf-8\">\r\n        <script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\"></script>\r\n        <script type=\"text/javascript\">\r\n            $(function() {\r\n                window.WebSocket = window.WebSocket || window.MozWebSocket;\r\n                var websocket = new WebSocket('ws://localhost:50090', 'dumb-increment-protocol'); //10.84.130.54\r\n                websocket.onopen = function () {\r\n                    $('h1').css('color', 'green');\r\n                };\r\n                websocket.onerror = function () {\r\n                    $('h1').css('color', 'red');\r\n                };\r\n                websocket.onmessage = function (message) {\r\n                    console.log(message.data);\r\n                    $('div').append($('<p>', { text: message.data }));\r\n                };\r\n                \r\n                $('button').click(function(e) {\r\n                    e.preventDefault();\r\n                    websocket.send($('input').val());\r\n                    $('input').val('');\r\n                });\r\n            });\r\n        </script>\r\n        </head>\r\n    <body>\r\n        <h1>WebSockets test</h1>\r\n        <form>\r\n            <input type=\"text\" />\r\n            <button>Send</button>\r\n        </form>\r\n        <div></div>\r\n    </body>\r\n</html>\r\n```\r\n\r\nThe socket can open properly apparently, but I cannot send any data ...\r\nThe server does not say anything whatsoever.\r\n\r\nAll I try to do is open a basic connection where I can push binary arrays and some JSON through ...\r\nThis was like 20lines in python and worked out of the box, so I am sure it can't be too hard in c either ...\r\nCan anybody help me?\r\n\r\nThanks\r\nYatekii"}}
{"actor":{"display_login":"lws-team"}}