# SIO Client This SIO client depends on [websocket++](https://github.com/zaphoyd/websocketpp) and [rapidjson](https://github.com/miloyip/rapidjson), it provides a C++ client implementation for [Socket.IO](https://github.com/Automattic/socket.io) and is inspired by the [socket.io-clientpp](https://github.com/ebshimizu/socket.io-clientpp) project. This library is able to connect to a Socket.IO 1.0 server. C++ allows amazing possibilities for cross platform development. Here's a screenshot showing example apps (iPhone,QT,Console and web) chatting in one room. ![Clients with iPhone, QT, Console and web](/screenshots/QuadClients.jpg) ## Socket.IO 1.0+ protocol has been implemented! The code is compatible with 1.0+ protocol only, not with prior protocols. ## C++11 only for now C++11 saves much time for me, so this is C++11 only for the first version. I'll do further compatibility efforts on demand. ## Usage 1. Make sure you have the boost libraries installed. 2. Use `git clone --recurse-submodules https://github.com/socketio/socket.io-client-cpp.git` to clone your local repo. 3. Include websocket++, rapidjson and `sio_client.cpp`,`sio_packet.cpp` in your project. 4. Include `sio_client.h` where you want to use it. 5. Use `message` and its derived classes to compose complex text/binary messages. ##API ### Constructors `client()` default constructor. ### Event Emitter `void emit(std::string const& name, std::string const& message)` `void emit(std::string const& name, std::string const& message, std::function const& ack)` Emit a plain text message, along with event's name and a optional ack callback function if you need server ack. `void emit(std::string const& name, message::ptr const& args)` `void emit(std::string const& name, message::ptr const& args, std::function const& ack)` Emit a `message` (explained below) object, along with event's name and a optional ack callback function if you need server ack. `void emit(std::string const& name, std::shared_ptr const& binary_ptr)` `void emit(std::string const& name, std::shared_ptr const& binary_ptr, std::function const& ack)` Emit a single binary buffer, along with event's name and a optional ack callback function if you need server ack. ### Event Bindings `void bind_event(std::string const& event_name,event_listener const& func)` Bind a callback to specified event name. Same as `socket.on` function in JS. `void unbind_event(std::string const& event_name)` Unbind the event callback with specified name. `void clear_event_bindings()` Clear all event bindings. `void set_default_event_listener(event_listener const& l)` Set a default event handler for events with no binding functions. `void set_error_listener(error_listener const& l)` Set the error handler for socket.io error messages. ```C++ //event listener declare: typedef std::function event_listener; typedef std::function error_listener; ``` ### Connection Listeners `void set_open_listener(con_listener const& l)` Call when websocket is open, especially means good connectivity. `void set_connect_listener(con_listener const& l)` Call when socket.io `connect` message is received, ready to send socket.io messages. `void set_fail_listener(con_listener const& l)` Call when failed in connecting. `void set_close_listener(close_listener const& l)` Call when closed or drop. See `client::close_reason` ```C++ //connection listener declare: enum close_reason { close_reason_normal, close_reason_drop }; typedef std::function con_listener; typedef std::function close_listener; ``` ### Connect and Close `void connect(const std::string& uri)` Connect to socket.io server, eg. `client.connect("ws://localhost:3000");` `void reconnect(const std::string& uri)` Try to reconnect with original session id. If `fail listener` triggered, means your session id is already expired, do not keep reconnect again. `void close()` Close the client, return immediately. `void sync_close()` Close the client, return until it is really closed. `bool connected() const` Check if client is connected. ### Session ID `std::string const& get_sessionid() const` Get socket.io session id. ### Message Object `message` Base class of all message object. `int_message` message contains a 64-bit integer. `double_message` message contains a double. `string_message` message contains a string. `array_message` message contains a `vector`. `object_message` message contains a `map`. `message::ptr` pointer to `message` object, it will be one of its derived classes, judge by `message.get_flag()`. ##Example Simple Console client Login to socket.io [chat room demo](https://github.com/Automattic/socket.io/tree/master/examples/chat). Find full example file [here](https://github.com/socketio/socket.io-client-cpp/blob/master/examples/Console/main.cpp) ```C++ #define HIGHLIGHT(__O__) std::cout<<"\e[1;31m"<<__O__<<"\e[0m"< #include #include #include #include using namespace sio; using namespace std; std::mutex _lock; std::condition_variable_any _cond; bool connect_finish = false; class connection_listener { sio::client &handler; public: connection_listener(sio::client& h): handler(h) { } void on_connected() { _lock.lock(); _cond.notify_all(); connect_finish = true; _lock.unlock(); } void on_close(client::close_reason const& reason) { std::cout<<"sio closed "<get_map()["numUsers"]->get_int(); bool plural = participants !=1; HIGHLIGHT("Welcome to Socket.IO Chat-\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant")); _cond.notify_all(); _lock.unlock(); h.unbind_event("login"); }); } ``` ## Boost build instructions(Build the necessary subset only) 1. Download boost from [boost.org](http://www.boost.org/).(suppose we downloaded boost 1.55.0) 2. Unpack boost to some place.(such as D:\boost_1_55_0) 3. Run either .\bootstrap.bat (on Windows), or ./bootstrap.sh (on other operating systems) under boost folder(D:\boost_1_55_0). 4. Run ./b2 install --prefix=PREFIX where PREFIX is a directory where you want Boost.Build to be installed. 5. Optionally, add PREFIX/bin to your PATH environment variable. 6. Build needed boost modules, with following command line as an example: For Windows: ```shell bjam stage --toolset=msvc --with-system --with-date_time --with-thread --with-regex --with-serialization --with-random --stagedir="release" link=static runtime-link=shared threading=multi release ``` For iOS and OSX Use this [shell](https://github.com/socketio/socket.io-client-cpp/blob/master/examples/iOS/SioChatDemo/boost/boost.sh) to download and build boost completely automattically. Finally, Add boost source folder to `header search path`, and add static libs to link option. ##License BSD