2016-04-12 23 views
2

İstemciye Javascript ile yazdım ve harika çalışıyor. Sunucu kodu da çalışır. Ancak, sunucudan istemciye mesaj göndermeyi veya bağlantıyı nasıl sonlandıracağımı bilmiyorum. Bunu yapmayı bilen var mı? Ayrıca, tam bir başlangıç ​​olduğumdan, kodumda eksik olduğum bir şey var mı? Olabildiğince basit tutmaya çalıştım. Hala o kod ile mücadele ediyorsanızWebsocketpp ile mesajlar nasıl gönderilir

#include <iostream> 
#include <websocketpp/config/asio_no_tls.hpp> 
#include <websocketpp/server.hpp> 

typedef websocketpp::server<websocketpp::config::asio> server; 

void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg) 
{ 
    std::cout << "Message received:" << std::endl; 
    std::cout << msg->get_payload() << std::endl; 
} 

int main() 
{ 
    server s; 
    s.set_message_handler(&on_message); 

    s.init_asio(); 
    s.listen(57252); 
    s.start_accept(); 
    std::cout << "Server Started." << std::endl; 
    s.run(); 
} 

cevap

3

, orada kullanmak için daha kolay olabilir burada başka bir büyük C++ WebSocket kütüphanesi onun yalnızca üstbilgi, var ve sadece artırmak kullanır. http://vinniefalco.github.io/

İşte yankı sunucuya bir mesaj gönderir tam bir program: Bu örnek kodu ve belgelerle birlikte gelir

#include <beast/websocket.hpp> 
#include <beast/buffers_debug.hpp> 
#include <boost/asio.hpp> 
#include <iostream> 
#include <string> 

int main() 
{ 
    // Normal boost::asio setup 
    std::string const host = "echo.websocket.org"; 
    boost::asio::io_service ios; 
    boost::asio::ip::tcp::resolver r(ios); 
    boost::asio::ip::tcp::socket sock(ios); 
    boost::asio::connect(sock, 
     r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"})); 

    using namespace beast::websocket; 

    // WebSocket connect and send message using beast 
    stream<boost::asio::ip::tcp::socket&> ws(sock); 
    ws.handshake(host, "/"); 
    ws.write(boost::asio::buffer("Hello, world!")); 

    // Receive WebSocket message, print and close using beast 
    beast::streambuf sb; 
    opcode op; 
    ws.read(op, sb); 
    ws.close(close_code::normal); 
    std::cout << 
     beast::debug::buffers_to_string(sb.data()) << "\n"; 
} 
0

yaptığım bu konunun günlüklerinin birini kapalı tabanlı:

  server::connection_ptr con = s.get_con_from_hdl(hdl); 
      std::string resp("BAD"); 
      con->send(resp, websocketpp::frame::opcode::text); 
Bir ikili yanıtı kontrol etmek isteyen herkes için

: https://github.com/zaphoyd/websocketpp/issues/572

İlgili konular