2015-11-07 96 views
0

我修改了“扩展连接存储”示例http://www.zaphoyd.com/websocketpp/manual/common-patterns/storing-connection-specificsession-information,并可以更新从客户端发送的某些数据。当数据发生变化时,我想将其传播给所有连接的客户端。我想迭代所有的连接,但在文档底部说明'注意,这个例子没有能力枚举所有连接。'。迭代websocketpp服务器中的所有连接

我应该用http://www.zaphoyd.com/websocketpp/manual/common-patterns/server-initiated-messages的例子来代替吗?

伪代码:

void on_message(connection_hdl hdl, server::message_ptr msg) { 
    if (jdata["type"] == "update") { 
     for (auto it : connections) { 
      m_server.send(hdl, msg); 
     } 
    } 
} 

回答

0

时间过看电视与孩子们的美好时光。在那里我突然想起websocketpp上的其他例子:

private: 
    typedef std::set<connection_hdl, std::owner_less<connection_hdl>> con_list; 
    con_list m_connections; 

void on_open(connection_hdl hdl) { 
    m_connections.insert(hdl); 
} 

void on_close(connection_hdl hdl) { 
    m_connections.erase(hdl); 
} 


if (jdata["type"] == "update") { 
    for (auto it : m_connections) { 
     msg->set_payload(table.dump()); 
      m_server.send(it, msg); 
     } 
    } 
}