Skip to content

Commit 958cfad

Browse files
committed
update readme
1 parent 8df330d commit 958cfad

1 file changed

Lines changed: 82 additions & 129 deletions

File tree

README.md

Lines changed: 82 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,26 @@ By virtue of being written in C++, this client works in several different platfo
2323
5. Use `message` and its derived classes to compose complex text/binary messages.
2424

2525
## API
26+
### *Overview*
27+
There're just 3 roles in this library - `socket`,`client` and `message`.
2628

27-
### Constructors
29+
`client` is for physical connection while `socket` is for "namespace"(which is like a logical channel), which means one `socket` paired with one namespace, and one `client` paired with one physical connection.
2830

29-
`client()` default constructor.
31+
Since a physical connection can have multiple namespaces (which is called multiplex), a `client` object may have multiple `socket` objects, each is bind to a distinct `namespace`.
32+
33+
Use `client` to setup the connection to the server, manange the connection status, also session id for the connection.
34+
35+
Use `socket` to send messages under namespace and receives messages in the namespace, also handle special types of message.
36+
37+
The `message` is just about the content you want to send, with text,binary or structured combinations.
38+
39+
### *Socket*
40+
#### Constructors
41+
Sockets are all managed by `client`, no public constructors.
42+
43+
You can get it's pointer by `client.socket(namespace)`.
3044

31-
### Event Emitter
45+
#### Event Emitter
3246
`void emit(std::string const& name, std::string const& message)`
3347

3448
`void emit(std::string const& name, std::string const& message, std::function<void (message::ptr const&)> const& ack)`
@@ -47,34 +61,29 @@ Emit a `message` (explained below) object, along with event's name and a optiona
4761

4862
Emit a single binary buffer, along with event's name and a optional ack callback function if you need server ack.
4963

50-
### Event Bindings
51-
`void bind_event(std::string const& event_name,event_listener const& func)`
64+
#### Event Bindings
65+
`void on(std::string const& event_name,event_listener const& func)`
5266

53-
`void bind_event(std::string const& event_name,event_listener_aux const& func)`
67+
`void on(std::string const& event_name,event_listener_aux const& func)`
5468

5569
Bind a callback to specified event name. Same as `socket.on()` function in JS, `event_listener` is for full content event object,`event_listener_aux` is for convinience.
5670

57-
`void unbind_event(std::string const& event_name)`
71+
`void off(std::string const& event_name)`
5872

5973
Unbind the event callback with specified name.
6074

61-
`void clear_event_bindings()`
75+
`void off_all()`
6276

6377
Clear all event bindings.
6478

65-
`void set_default_event_listener(event_listener const& l)`
66-
67-
`void set_default_event_listener(event_listener_aux const& l)`
68-
69-
Set a default event handler for events with no binding functions.
70-
7179
`void set_error_listener(error_listener const& l)`
7280

7381
Set the error handler for socket.io error messages.
7482

7583
```C++
76-
//event object:
77-
class event
84+
85+
//event object:
86+
class event
7887
{
7988
public:
8089
const std::string& get_nsp() const;
@@ -90,23 +99,46 @@ class event
9099
message::ptr const& get_ack_message() const;
91100
...
92101
};
93-
//event listener declare:
94-
typedef std::function<void(const std::string& name,message::ptr const& message,bool need_ack, message::ptr& ack_message)> event_listener_aux;
95-
96-
typedef std::function<void(event& event)> event_listener;
102+
//event listener declare:
103+
typedef std::function<void(const std::string& name,message::ptr const& message,bool need_ack, message::ptr& ack_message)> event_listener_aux;
104+
105+
typedef std::function<void(event& event)> event_listener;
97106

98-
typedef std::function<void(message::ptr const& message)> error_listener;
107+
typedef std::function<void(message::ptr const& message)> error_listener;
99108

100109
```
101110
102-
### Connection Listeners
103-
`void set_open_listener(con_listener const& l)`
111+
#### Listeners
112+
`void set_connect_listener(con_listener const& l)`
104113
105-
Call when websocket is open, especially means good connectivity.
114+
Set listener for connect event, called when server notify socket is joined the namespace.
106115
107-
`void set_connect_listener(con_listener const& l)`
116+
`void set_close_listener(con_listener const& l)`
117+
118+
Set listener for close event, called when server notify socket is kicked from namespace. `socket` object will be destroyed after close event.
119+
120+
#### Connect and close socket
121+
`connect` will happen for existing `socket`s automatically when `client` have opened up the physical connection.
122+
123+
`socket` opened with connected `client` will connect to its namespace immediately.
124+
125+
`void close()`
126+
127+
Positively disconnect from namespace.
128+
129+
#### Get name of namespace
130+
`std::string const& get_namespace() const`
108131
109-
Call when socket.io `connect` message is received, ready to send socket.io messages.
132+
Get current namespace name which the client is inside.
133+
134+
### *Client*
135+
#### Constructors
136+
`client()` default constructor.
137+
138+
#### Connection Listeners
139+
`void set_open_listener(con_listener const& l)`
140+
141+
Call when websocket is open, especially means good connectivity.
110142
111143
`void set_fail_listener(con_listener const& l)`
112144
@@ -127,7 +159,7 @@ typedef std::function<void(void)> con_listener;
127159
128160
typedef std::function<void(close_reason const& reason)> close_listener;
129161
```
130-
### Connect and Close
162+
#### Connect and Close
131163
`void connect(const std::string& uri)`
132164

133165
Connect to socket.io server, eg. `client.connect("ws://localhost:3000");`
@@ -144,25 +176,21 @@ Close the client, return immediately.
144176

145177
Close the client, return until it is really closed.
146178

147-
`bool connected() const`
179+
`bool opened() const`
148180

149-
Check if client is connected.
181+
Check if client's connection is opened.
150182

151-
### Namespace
152-
`void connect(const std::string& uri)`
153-
154-
Add namespace part `/[any namespaces]` after port, will automatically connect to the namespace you specified.
183+
#### Namespace
184+
`socket::ptr socket(std::string const& nsp)`
155185

156-
`std::string const& get_namespace() const`
186+
Get a pointer to a socket which is paired with the specified namespace.
157187

158-
Get current namespace which the client is inside.
159-
160-
### Session ID
188+
#### Session ID
161189
`std::string const& get_sessionid() const`
162190

163191
Get socket.io session id.
164192

165-
### Message Object
193+
### *Message*
166194
`message` Base class of all message object.
167195

168196
`int_message` message contains a 64-bit integer.
@@ -179,106 +207,31 @@ Get socket.io session id.
179207

180208
All designated constructor of `message` objects is hidden, you need to create message and get the `message::ptr` by `[derived]_message:create()`.
181209

182-
##Example
210+
## Boost setup
211+
* Download boost from [boost.org](http://www.boost.org/).
183212

184-
Simple Console client Login to socket.io [chat room demo](https://github.com/Automattic/socket.io/tree/master/examples/chat).
185-
Find full example file [here](https://github.com/socketio/socket.io-client-cpp/blob/master/examples/Console/main.cpp)
186-
```C++
187-
#define HIGHLIGHT(__O__) std::cout<<"\e[1;31m"<<__O__<<"\e[0m"<<std::endl
188-
#define EM(__O__) std::cout<<"\e[1;30;1m"<<__O__<<"\e[0m"<<std::endl
189-
#include <functional>
190-
#include <iostream>
191-
#include <mutex>
192-
#include <condition_variable>
193-
#include <string>
194-
using namespace sio;
195-
using namespace std;
196-
std::mutex _lock;
197-
198-
std::condition_variable_any _cond;
199-
bool connect_finish = false;
200-
201-
class connection_listener
202-
{
203-
sio::client &handler;
213+
* Unpack boost to some place.
204214

205-
public:
206-
207-
connection_listener(sio::client& h):
208-
handler(h)
209-
{
210-
}
211-
215+
* Run either .\bootstrap.bat (on Windows), or ./bootstrap.sh (on other operating systems) under boost folder.
212216

213-
void on_connected()
214-
{
215-
_lock.lock();
216-
_cond.notify_all();
217-
connect_finish = true;
218-
_lock.unlock();
219-
}
220-
void on_close(client::close_reason const& reason)
221-
{
222-
std::cout<<"sio closed "<<std::endl;
223-
exit(0);
224-
}
225-
226-
void on_fail()
227-
{
228-
std::cout<<"sio failed "<<std::endl;
229-
exit(0);
230-
}
231-
};
217+
## Boost build (Build the necessary subset only)
232218

233-
int main(int argc ,const char* args[])
234-
{
219+
#### Windows:
220+
Run with following script will build the necessary subset:
235221

236-
sio::client h;
237-
connection_listener l(h);
238-
h.set_connect_listener(std::bind(&connection_listener::on_connected, &l));
239-
h.set_close_listener(std::bind(&connection_listener::on_close, &l,std::placeholders::_1));
240-
h.set_fail_listener(std::bind(&connection_listener::on_fail, &l));
241-
h.connect("http://127.0.0.1:3000");
242-
_lock.lock();
243-
if(!connect_finish)
244-
{
245-
_cond.wait(_lock);
246-
}
247-
_lock.unlock();
248-
string nickname;
249-
while (nickname.length() == 0) {
250-
HIGHLIGHT("Type your nickname:");
251-
252-
getline(cin, nickname);
253-
}
254-
h.bind_event("login", [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp){
255-
_lock.lock();
256-
participants = data->get_map()["numUsers"]->get_int();
257-
bool plural = participants !=1;
258-
HIGHLIGHT("Welcome to Socket.IO Chat-\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
259-
_cond.notify_all();
260-
_lock.unlock();
261-
h.unbind_event("login");
262-
});
263-
}
222+
```bash
223+
bjam stage --toolset=msvc --with-system --with-date_time --with-random --stagedir="release" link=static runtime-link=shared threading=multi release
264224
```
265-
## Boost build instructions(Build the necessary subset only)
266-
1. Download boost from [boost.org](http://www.boost.org/).(suppose we downloaded boost 1.55.0)
267-
2. Unpack boost to some place.(such as D:\boost_1_55_0)
268-
3. Run either .\bootstrap.bat (on Windows), or ./bootstrap.sh (on other operating systems) under boost folder(D:\boost_1_55_0).
269-
4. Run ./b2 install --prefix=PREFIX
270-
where PREFIX is a directory where you want Boost.Build to be installed.
271-
5. Optionally, add PREFIX/bin to your PATH environment variable.
272-
6. Build needed boost modules, with following command line as an example:
273-
274-
For Windows:
275-
```shell
276-
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
225+
Optionally You can merge all output .lib files into a fat one, in output folder, run:
226+
227+
```bash
228+
lib.exe /OUT:boost.lib *
277229
```
278-
For iOS and OSX
230+
231+
#### iOS and OSX
279232
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.
280233

281-
Finally, Add boost source folder to `header search path`, and add static libs to link option.
234+
#### Add boost source folder to your `header search path`, and add static libs to link option.
282235

283236
##License
284237
BSD

0 commit comments

Comments
 (0)