Data sent from node server using broadcast emit from another client is not able to be read by c++ socket io client

强颜欢笑 提交于 2019-12-23 05:13:57

问题


Node server

'use strict';

const express     = require('express');
const app         = express();
const serverHttp  = require('http').Server(app); 
const io = require('socket.io')(serverHttp);

const port = 8081;

socket.on('JogCartesianButton', data => {
    socket.broadcast.emit('JogCartesianButton', data)
    console.log(data)
  })
serverHttp.listen(port, function() {  
    console.log("init!!!");    
});

data looks like this

{   
  q_value: [ 0, 0, 0, 0, 0, 0 ],  
  status: false,  
  joint: true,  
  cartesian: false,  
  freedrive: false,  
  button: true,  
  slider: false,   
  goto: false,  
  threed: false   
}

now im accepting this data from server in c++ client. By broadcast emitting it.

My c++ client code is below.

        sio::client io;
        io.set_open_listener([&]() {
            std::string nickName = "asdad";
            io.socket()->emit("key", nickName);
            io.socket()->on("JogCartesianButton", [&](sio::event& ev)
                    {
                       //Here how do i get the **data** from server
                    });
        });
        io.connect("http://localhost:8081/");

How do i accept that data inside c++ which was broadcasted from node server. i tried ev.get_messages() 0x7ff174008130 this was the output, but need { key : value } as output .


回答1:


The documentation is very incomplete, but something like the following should work: (taken from this example)

io.socket()->on("JogCartesianButton", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck, message::list &ack_resp) {
    string user = data->get_map()["name"]->get_string();
    int64_t age = data->get_map()["age"]->get_int();
    // Do something with user and age
}));

As I remarked in the comments, the on() method is polymorphic in its callback, and the event_listener_aux form is a bit more easy to use. Note that this example does zero error checking up front, so you should inspect every part of data using message::get_flag before you access it.



来源:https://stackoverflow.com/questions/59053214/data-sent-from-node-server-using-broadcast-emit-from-another-client-is-not-able

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!