c++ - exchange data with socket.io server from embedded system websockets cpp client -
i'm unable exchange data socket.io server native c++ websocket (www.websocket.org) client. note not want solution based on boost i'm running websocket client embedded system (it has light-weight).
i've tried both lws_write_text , lws_write_http (no padding) modes. i'm not using secure sockets (i.e., not using wss or ssl certs).
myserver.js:
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); }); });
i've enhanced libwebsockets-test-echo.cpp sample code starting point , have working fine when both client , server use websockets c++ api. however, when connect socket.io server, server console reports: warn - unknown transport: "undefined" .
client.cpp:
... struct per_session_data__echo { // lws_write_text mode, pre_padding defined lws_send_buffer_pre_padding , post_padding lws_send_buffer_post_padding, otherwise they're both set 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->index = 0; break; case lws_callback_client_receive: lwsl_notice("client rx: %s", (char *)in); break; case lws_callback_client_writeable: /* send our packet... */ pss->len = sprintf((char *)&pss->buf[lws_send_buffer_pre_padding], "hello libwebsockets-test-echo client pid %d index %d\n", getpid(), pss->index++); lwsl_notice("client tx: %s", &pss->buf[lws_send_buffer_pre_padding]); n = libwebsocket_write(wsi, &pss->buf[lws_send_buffer_pre_padding], pss->len, lws_write_text); if (n < 0) { lwsl_err("error %d writing socket, hanging up\n", n); return -1; } if (n < (int)pss->len) { lwsl_err("partial write\n"); return -1; } break; default: break; } return 0; } static struct libwebsocket_protocols protocols[] = { /* first protocol must 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(&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(&protocols_[0]); ... int rc = 0; unsigned int oldus = 0; while (rc >= 0 && !forceexit_) { struct timeval tv; gettimeofday(&tv, null); if (((unsigned int)tv.tv_usec - oldus) > pollingrate_) { oldus = tv.tv_usec; } rc = libwebsocket_service(context_, 1000); // wait 1000 msec if (rc != 0) printf("rc=%d\n", rc); }
...
here's output 'sudo node server.js':
info - socket.io started warn - unknown transport: "undefined"
here's output client.cpp console. not mislead "listening on port=0" it's context port setting used libwebsockets-test-echo.cpp, works websocket websocket client , server. note connection completes , output same both lws_write_text , lws_write_http modes.
listening on port=0 listening on interface=eth0 lwsts[12960]: initial logging level 7 lwsts[12960]: library version: 1.3 502b994 lwsts[12960]: started daemon pid 0 lwsts[12960]: static allocation: 4488 + (16 x 1024 fds) = 20872 bytes lwsts[12960]: canonical_hostname = sogo lwsts[12960]: context->protocols[0].name='my other event' lwsts[12960]: lws_callback_openssl_load_extra_client_verify_certs lwsts[12960]: context->protocols[0].name='my other event' lwsts[12960]: lws_callback_protocol_init lwsts[12960]: websocketclient::connect(): connecting localhost:80 ... sslcerts=0 protocolnamelist='my other event' connecting address=localhost , port=80 lwsts[13340]: context->protocols[0].name='my other event' lwsts[13340]: lws_callback_add_poll_fd lwsts[13340]: context->protocols[0].name='my other event' lwsts[13340]: lws_callback_clear_mode_poll_fd lwsts[13340]: context->protocols[0].name='my other event' lwsts[13340]: lws_callback_client_confirm_extension_supported lwsts[13340]: context->protocols[0].name='my other event' lwsts[13340]: lws_callback_client_append_handshake_header lwsts[13340]: websocketclient::connect(): connected localhost:80 lwsts[13340]: context->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->protocols[0].name='my other event' lwsts[13340]: lws_callback_del_poll_fd
Comments
Post a Comment