2015-12-22 15 views
6

Linux上でC++を使用してWebSocket APIにアクセスしたいとします。私はさまざまなライブラリ(libwebsocketsまたはwebsocketppなど)を見たことがありますが、私はどちらを使うべきかはわかりません。私がしなければならないのは、がAPIに接続し、という文字列を受け取ることだけです。だから私は非常にの基本と簡単な解決策、何も複雑すぎるを探しています。誰かが既にWebSocketライブラリの経験をしているのでしょうか?WebSocketライブラリ

答えて

9

上位レベルのAPIの場合は、cpprestライブラリ{websocketpp}のws_clientを使用できます。

echo serverに対して実行するサンプルアプリケーション:

#include <iostream> 
#include <cpprest/ws_client.h> 

using namespace std; 
using namespace web; 
using namespace web::websockets::client; 

int main() { 
    websocket_client client; 
    client.connect("ws://echo.websocket.org").wait(); 

    websocket_outgoing_message out_msg; 
    out_msg.set_utf8_message("test"); 
    client.send(out_msg).wait(); 

    client.receive().then([](websocket_incoming_message in_msg) { 
    return in_msg.extract_string(); 
    }).then([](string body) { 
    cout << body << endl; // test 
    }).wait(); 

    client.close().wait(); 

    return 0; 
} 

ここ.wait()方法がタスクを待つために使用される、しかし、コードが簡単に非同期な方法でI/Oを行うように変更することができます。

+0

ありがとうございました!チャンネルへの加入もサポートしていますか? – Bobface

+0

また、そのページに** Microsoftプロジェクト**と書かれています。 Linuxシステムではおそらく動作しませんか? – Bobface

+2

これはかなり安定しており、Windows、OS X、iOS、** Linux(Androidを含む)**で動作します。実際には、このサンプルをUbuntu 15.10でコーディングして実行しました。 –

関連する問題