Created
September 21, 2016 13:12
-
-
Save nasitra/66f4cf405a59a92d57c577bbbb22623e to your computer and use it in GitHub Desktop.
WebSocket client implemented with C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "websocket-client.h" | |
#include <iostream> | |
using websocketpp::lib::placeholders::_1; | |
using websocketpp::lib::placeholders::_2; | |
using websocketpp::lib::bind; | |
WebSocketClient::WebSocketClient(std::string uri) { | |
mUri = uri; | |
mClient.set_access_channels(websocketpp::log::alevel::all); | |
mClient.set_error_channels(websocketpp::log::elevel::all); | |
mClient.clear_access_channels(websocketpp::log::alevel::frame_payload); | |
mClient.init_asio(); | |
mClient.set_message_handler(bind(&type::on_message,this,::_1,::_2)); | |
} | |
void WebSocketClient::connect() { | |
try { | |
websocketpp::lib::error_code ec; | |
client::connection_ptr con = mClient.get_connection(mUri, ec); | |
if (ec) { | |
mClient.get_alog().write(websocketpp::log::alevel::app, ec.message()); | |
return; | |
} | |
mClient.connect(con); | |
mClient.run(); | |
} catch (websocketpp::exception const & e) { | |
std::cout << e.what() << std::endl; | |
} | |
} | |
void WebSocketClient::on_message(websocketpp::connection_hdl hdl, message_ptr msg) { | |
std::cout << msg->get_payload() << std::endl; | |
} | |
void WebSocketClient::print() { | |
std::cout << mUri << std::endl; | |
} | |
extern "C" void WebSocketClient_print() { | |
WebSocketClient wsc("ws://localhost:8000/"); | |
wsc.print(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _WEBSOCKET_CLIENT_H | |
#define _WEBSOCKET_CLIENT_H | |
#ifdef __cplusplus | |
#include <string> | |
#include <websocketpp/config/asio_client.hpp> | |
#include <websocketpp/client.hpp> | |
typedef websocketpp::client<websocketpp::config::asio_client> client; | |
typedef websocketpp::config::asio_client::message_type::ptr message_ptr; | |
class WebSocketClient { | |
typedef WebSocketClient type; | |
public: | |
WebSocketClient(std::string uri); | |
void connect(); | |
void print(); | |
protected: | |
void on_message(websocketpp::connection_hdl hdl, message_ptr msg); | |
private: | |
client mClient; | |
std::string mUri; | |
}; | |
extern "C" { | |
#endif | |
void WebSocketClient_print(); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package websocket | |
/* | |
#cgo CXXFLAGS: -I /usr/local/include/websocketpp/ | |
#cgo LDFLAGS: -lboost_system -lboost_random -lboost_timer -lboost_chrono -lrt -lssl -lcrypto | |
#include "websocket-client.h" | |
*/ | |
import "C" | |
func print() { | |
C.WebSocketClient_print() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package websocket | |
import "testing" | |
func TestPrint(t *testing.T) { | |
print() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment