diff --git a/README.md b/README.md index db6e64f..5e0b069 100755 --- a/README.md +++ b/README.md @@ -39,6 +39,58 @@ cmake 5. Add `/lib` to library search path, add `boost.lib`(Win32) or `-lboost`(Other) link option. 6. Include `sio_client.h` in your client code where you want to use it. +## Quick start +The APIs are similar with JS client. + +Connect to a server +```C++ +sio::client h; +h.connect("http://127.0.0.1:3000"); +``` + +Emit a event +```C++ +//emit text +h.socket()->emit("add user", username); +//emit binary +char buf[100]; +h.socket()->emit("add user", std::make_shared(&buf,100)); +//emit message object with lambda ack handler +h.socket()->emit("add user", string_message::create(username), [&](message::ptr const& msg) +{ +}); +``` + +Bind a event +```C++ +/**************** bind with function pointer ***************/ +void OnMessage(sio::event &) +{ + +} + +h.socket()->on("new message", &OnMessage); +/********************* bind with lambda ********************/ +h.socket()->on("login", [&](sio::event& ev) +{ + //handle login message + //post to UI thread if any UI updating. +}); + +/**************** bind with member function *****************/ +class MessageHandler +{ +public: + void OnMessage(sio::event &); +}; +MessageHandler mh; +h.socket()->on("new message",std::bind( &MessageHandler::OnMessage,&mh,std::placeholders::_1)); +``` +Send to other namespace +```C++ +h.socket("/chat")->emit("add user", username); +``` + ## API ### *Overview* There're just 3 roles in this library - `socket`,`client` and `message`.