Skip to content

Commit c1005ad

Browse files
committed
add quick start section
1 parent 15552e6 commit c1005ad

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,58 @@ cmake
3939
5. Add `<your boost install folder>/lib` to library search path, add `boost.lib`(Win32) or `-lboost`(Other) link option.
4040
6. Include `sio_client.h` in your client code where you want to use it.
4141

42+
## Quick start
43+
The APIs are similar with JS client.
44+
45+
Connect to a server
46+
```C++
47+
sio::client h;
48+
h.connect("http://127.0.0.1:3000");
49+
```
50+
51+
Emit a event
52+
```C++
53+
//emit text
54+
h.socket()->emit("add user", username);
55+
//emit binary
56+
char buf[100];
57+
h.socket()->emit("add user", std::make_shared<std::string>(&buf,100));
58+
//emit message object with lambda ack handler
59+
h.socket()->emit("add user", string_message::create(username), [&](message::ptr const& msg)
60+
{
61+
});
62+
```
63+
64+
Bind a event
65+
```C++
66+
/**************** bind with function pointer ***************/
67+
void OnMessage(sio::event &)
68+
{
69+
70+
}
71+
72+
h.socket()->on("new message", &OnMessage);
73+
/********************* bind with lambda ********************/
74+
h.socket()->on("login", [&](sio::event& ev)
75+
{
76+
//handle login message
77+
//post to UI thread if any UI updating.
78+
});
79+
80+
/**************** bind with member function *****************/
81+
class MessageHandler
82+
{
83+
public:
84+
void OnMessage(sio::event &);
85+
};
86+
MessageHandler mh;
87+
h.socket()->on("new message",std::bind( &MessageHandler::OnMessage,&mh,std::placeholders::_1));
88+
```
89+
Send to other namespace
90+
```C++
91+
h.socket("/chat")->emit("add user", username);
92+
```
93+
4294
## API
4395
### *Overview*
4496
There're just 3 roles in this library - `socket`,`client` and `message`.

0 commit comments

Comments
 (0)