Note that there are some explanatory texts on larger screens.

plurals
  1. POexchange data with socket.io server from embedded system websockets cpp client
    primarykey
    data
    text
    <p>I'm unable to exchange data with a socket.io server from a native C++ WebSocket (www.websocket.org) client. Note that I do NOT want a solution based on Boost as I'm running the WebSocket client from an embedded system (it has to be light-weight).</p> <p>I've tried both LWS_WRITE_TEXT and LWS_WRITE_HTTP (no padding) modes. I'm not using secure sockets (i.e., not using wss or SSL certs).</p> <p>myServer.js:</p> <pre><code>var io = require('socket.io').listen(80); io.configure('production', function(){ io.enable('browser client etag'); io.set('log level', 3); io.set('transports', ['websocket']); }); io.configure('development', function(){ io.set('log level', 3); io.set('transports', ['websocket']); }); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); </code></pre> <p>I've enhanced the libwebsockets-test-echo.cpp sample code as a starting point and have it working fine when both client and server use WebSockets C++ API. However, when I connect to the socket.io server, the server console reports: warn - unknown transport: "undefined" .</p> <p>client.cpp:</p> <pre><code>... struct per_session_data__echo { // for LWS_WRITE_TEXT mode, PRE_PADDING is defined to LWS_SEND_BUFFER_PRE_PADDING and POST_PADDING is LWS_SEND_BUFFER_POST_PADDING, otherwise they're both set to 0. unsigned char buf[PRE_PADDING + 1400 + POST_PADDING]; unsigned int len; unsigned int index; }; static int callback_echo(struct libwebsocket_context *context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) { struct per_session_data__echo *pss = (struct per_session_data__echo *)user; int n; switch (reason) { case LWS_CALLBACK_CLIENT_ESTABLISHED: lwsl_notice("Client has connected\n"); pss-&gt;index = 0; break; case LWS_CALLBACK_CLIENT_RECEIVE: lwsl_notice("Client RX: %s", (char *)in); break; case LWS_CALLBACK_CLIENT_WRITEABLE: /* we will send our packet... */ pss-&gt;len = sprintf((char *)&amp;pss-&gt;buf[LWS_SEND_BUFFER_PRE_PADDING], "hello from libwebsockets-test-echo client pid %d index %d\n", getpid(), pss-&gt;index++); lwsl_notice("Client TX: %s", &amp;pss-&gt;buf[LWS_SEND_BUFFER_PRE_PADDING]); n = libwebsocket_write(wsi, &amp;pss-&gt;buf[LWS_SEND_BUFFER_PRE_PADDING], pss-&gt;len, LWS_WRITE_TEXT); if (n &lt; 0) { lwsl_err("ERROR %d writing to socket, hanging up\n", n); return -1; } if (n &lt; (int)pss-&gt;len) { lwsl_err("Partial write\n"); return -1; } break; default: break; } return 0; } static struct libwebsocket_protocols protocols[] = { /* first protocol must always be HTTP handler */ { "my other event", /* name */ callback_echo, /* callback */ sizeof(struct per_session_data__echo), /* per_session_data_size */ 0 }, { NULL, NULL, 0, 0 /* End of list */ } }; ... struct lws_context_creation_info info; info.port = CONTEXT_PORT_NO_LISTEN; info.iface = "eth0"; info.gid = -1; info.uid = -1; info.options = 0; info.extensions = libwebsocket_get_internal_extensions(); info.protocols = protocols; libwebsocket_context context_; context_ = libwebsocket_create_context(&amp;info); libwebsocket *wsi_; wsi_ = libwebsocket_client_connect(context_, "localhost", 80, // port 0, // "ws:" (no SSL) "/socket.io", // path "localhost", // host name "controller", // Socket origin name "my other event", // libwebsocket protocol name -1 ); ... libwebsocket_callback_on_writable_all_protocol(&amp;protocols_[0]); ... int rc = 0; unsigned int oldus = 0; while (rc &gt;= 0 &amp;&amp; !forceExit_) { struct timeval tv; gettimeofday(&amp;tv, NULL); if (((unsigned int)tv.tv_usec - oldus) &gt; pollingRate_) { oldus = tv.tv_usec; } rc = libwebsocket_service(context_, 1000); // wait 1000 msec if (rc != 0) printf("rc=%d\n", rc); } </code></pre> <p>...</p> <p>Here's the output from 'sudo node server.js':</p> <pre><code> info - socket.io started warn - unknown transport: "undefined" </code></pre> <p>Here's the output from client.cpp console. Do NOT be mislead by "listening on port=0" as it's the context port setting used by libwebsockets-test-echo.cpp, which works for websocket to websocket client and server. Note the connection completes and this output is the same for both LWS_WRITE_TEXT and LWS_WRITE_HTTP modes.</p> <pre><code>listening on port=0 listening on interface=eth0 lwsts[12960]: Initial logging level 7 lwsts[12960]: Library version: 1.3 502b994 lwsts[12960]: Started with daemon pid 0 lwsts[12960]: static allocation: 4488 + (16 x 1024 fds) = 20872 bytes lwsts[12960]: canonical_hostname = sogo lwsts[12960]: context-&gt;protocols[0].name='my other event' lwsts[12960]: LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS lwsts[12960]: context-&gt;protocols[0].name='my other event' lwsts[12960]: LWS_CALLBACK_PROTOCOL_INIT lwsts[12960]: WebSocketClient::connect(): connecting to localhost:80 ... sslCerts=0 protocolNameList='my other event' connecting to address=localhost and port=80 lwsts[13340]: context-&gt;protocols[0].name='my other event' lwsts[13340]: LWS_CALLBACK_ADD_POLL_FD lwsts[13340]: context-&gt;protocols[0].name='my other event' lwsts[13340]: LWS_CALLBACK_CLEAR_MODE_POLL_FD lwsts[13340]: context-&gt;protocols[0].name='my other event' lwsts[13340]: LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED lwsts[13340]: context-&gt;protocols[0].name='my other event' lwsts[13340]: LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER lwsts[13340]: WebSocketClient::connect(): connected to localhost:80 lwsts[13340]: context-&gt;protocols[0].name='my other event' lwsts[13340]: LWS_CALLBACK_SET_MODE_POLL_FD lwsts[13340]: WebSocketClient::run(): running ... lwsts[13340]: problems parsing header lwsts[13340]: context-&gt;protocols[0].name='my other event' lwsts[13340]: LWS_CALLBACK_DEL_POLL_FD </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload