add quick start section

This commit is contained in:
melode11 2015-04-08 17:02:10 +08:00
parent 15552e6ad1
commit c1005ad8ed

View File

@ -39,6 +39,58 @@ cmake
5. Add `<your boost install folder>/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<std::string>(&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`.