mirror of
https://github.com/socketio/socket.io-client-cpp.git
synced 2026-06-10 20:36:14 +00:00
add quick start section
This commit is contained in:
parent
15552e6ad1
commit
c1005ad8ed
52
README.md
52
README.md
@ -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.
|
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.
|
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
|
## API
|
||||||
### *Overview*
|
### *Overview*
|
||||||
There're just 3 roles in this library - `socket`,`client` and `message`.
|
There're just 3 roles in this library - `socket`,`client` and `message`.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user