|
| 1 | +## API |
| 2 | +### *Overview* |
| 3 | +There're just 3 roles in this library - `socket`,`client` and `message`. |
| 4 | + |
| 5 | +`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. |
| 6 | + |
| 7 | +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`. |
| 8 | + |
| 9 | +Use `client` to setup the connection to the server, manange the connection status, also session id for the connection. |
| 10 | + |
| 11 | +Use `socket` to send messages under namespace and receives messages in the namespace, also handle special types of message. |
| 12 | + |
| 13 | +The `message` is just about the content you want to send, with text,binary or structured combinations. |
| 14 | + |
| 15 | +### *Socket* |
| 16 | +#### Constructors |
| 17 | +Sockets are all managed by `client`, no public constructors. |
| 18 | + |
| 19 | +You can get it's pointer by `client.socket(namespace)`. |
| 20 | + |
| 21 | +#### Event Emitter |
| 22 | +`void emit(std::string const& name, message::list const& msglist, std::function<void (message::ptr const&)> const& ack)` |
| 23 | + |
| 24 | +Universal event emition interface, by applying implicit conversion magic, it is backward compatible with all previous `emit` interfaces. |
| 25 | + |
| 26 | +#### Event Bindings |
| 27 | +`void on(std::string const& event_name,event_listener const& func)` |
| 28 | + |
| 29 | +`void on(std::string const& event_name,event_listener_aux const& func)` |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +`void off(std::string const& event_name)` |
| 34 | + |
| 35 | +Unbind the event callback with specified name. |
| 36 | + |
| 37 | +`void off_all()` |
| 38 | + |
| 39 | +Clear all event bindings (not including the error listener). |
| 40 | + |
| 41 | +`void on_error(error_listener const& l)` |
| 42 | + |
| 43 | +Bind the error handler for socket.io error messages. |
| 44 | + |
| 45 | +`void off_error()` |
| 46 | + |
| 47 | +Unbind the error handler. |
| 48 | + |
| 49 | +```C++ |
| 50 | +//event object: |
| 51 | +class event |
| 52 | +{ |
| 53 | +public: |
| 54 | + const std::string& get_nsp() const; |
| 55 | + |
| 56 | + const std::string& get_name() const; |
| 57 | + |
| 58 | + const message::ptr& get_message() const; |
| 59 | + |
| 60 | + bool need_ack() const; |
| 61 | + |
| 62 | + void put_ack_message(message::ptr const& ack_message); |
| 63 | + |
| 64 | + message::ptr const& get_ack_message() const; |
| 65 | + ... |
| 66 | +}; |
| 67 | +//event listener declare: |
| 68 | +typedef std::function<void(const std::string& name,message::ptr const& message,bool need_ack, message::ptr& ack_message)> event_listener_aux; |
| 69 | + |
| 70 | +typedef std::function<void(event& event)> event_listener; |
| 71 | + |
| 72 | +typedef std::function<void(message::ptr const& message)> error_listener; |
| 73 | + |
| 74 | +``` |
| 75 | + |
| 76 | +#### Connect and close socket |
| 77 | +`connect` will happen for existing `socket`s automatically when `client` have opened up the physical connection. |
| 78 | + |
| 79 | +`socket` opened with connected `client` will connect to its namespace immediately. |
| 80 | + |
| 81 | +`void close()` |
| 82 | + |
| 83 | +Positively disconnect from namespace. |
| 84 | + |
| 85 | +#### Get name of namespace |
| 86 | +`std::string const& get_namespace() const` |
| 87 | + |
| 88 | +Get current namespace name which the client is inside. |
| 89 | + |
| 90 | +### *Client* |
| 91 | +#### Constructors |
| 92 | +`client()` default constructor. |
| 93 | + |
| 94 | +#### Connection Listeners |
| 95 | +`void set_open_listener(con_listener const& l)` |
| 96 | + |
| 97 | +Call when websocket is open, especially means good connectivity. |
| 98 | + |
| 99 | +`void set_fail_listener(con_listener const& l)` |
| 100 | + |
| 101 | +Call when failed in connecting. |
| 102 | + |
| 103 | +`void set_close_listener(close_listener const& l)` |
| 104 | + |
| 105 | +Call when closed or drop. See `client::close_reason` |
| 106 | + |
| 107 | +```C++ |
| 108 | +//connection listener declare: |
| 109 | +enum close_reason |
| 110 | +{ |
| 111 | + close_reason_normal, |
| 112 | + close_reason_drop |
| 113 | +}; |
| 114 | +typedef std::function<void(void)> con_listener; |
| 115 | + |
| 116 | +typedef std::function<void(close_reason const& reason)> close_listener; |
| 117 | +``` |
| 118 | +#### Socket listeners |
| 119 | +`void set_socket_open_listener(socket_listener const& l)` |
| 120 | + |
| 121 | +Set listener for socket connect event, called when any sockets being ready to send message. |
| 122 | + |
| 123 | +`void set_socket_close_listener(socket_listener const& l)` |
| 124 | + |
| 125 | +Set listener for socket close event, called when any sockets being closed, afterward, corresponding `socket` object will be cleared from client. |
| 126 | + |
| 127 | +```C++ |
| 128 | + //socket_listener declare: |
| 129 | + typedef std::function<void(std::string const& nsp)> socket_listener; |
| 130 | +``` |
| 131 | + |
| 132 | +#### Connect and Close |
| 133 | +`void connect(const std::string& uri)` |
| 134 | + |
| 135 | +Connect to socket.io server, eg. `client.connect("ws://localhost:3000");` |
| 136 | + |
| 137 | +`void close()` |
| 138 | + |
| 139 | +Close the client, return immediately. |
| 140 | + |
| 141 | +`void sync_close()` |
| 142 | + |
| 143 | +Close the client, return until it is really closed. |
| 144 | + |
| 145 | +`bool opened() const` |
| 146 | + |
| 147 | +Check if client's connection is opened. |
| 148 | + |
| 149 | +#### Transparent reconnecting |
| 150 | +`void set_reconnect_attempts(int attempts)` |
| 151 | + |
| 152 | +Set max reconnect attempts, set to 0 to disable transparent reconnecting. |
| 153 | + |
| 154 | +`void set_reconnect_delay(unsigned millis)` |
| 155 | + |
| 156 | +Set minimum delay for reconnecting, this is the delay for 1st reconnecting attempt, |
| 157 | +then the delay duration grows by attempts made. |
| 158 | + |
| 159 | +`void set_reconnect_delay_max(unsigned millis)` |
| 160 | + |
| 161 | +Set maximum delay for reconnecting. |
| 162 | + |
| 163 | +`void set_reconnecting_listener(con_listener const& l)` |
| 164 | + |
| 165 | +Set listener for reconnecting is in process. |
| 166 | + |
| 167 | +`void set_reconnect_listener(reconnect_listener const& l)` |
| 168 | + |
| 169 | +Set listener for reconnecting event, called once a delayed connecting is scheduled. |
| 170 | + |
| 171 | +#### Namespace |
| 172 | +`socket::ptr socket(std::string const& nsp)` |
| 173 | + |
| 174 | +Get a pointer to a socket which is paired with the specified namespace. |
| 175 | + |
| 176 | +#### Session ID |
| 177 | +`std::string const& get_sessionid() const` |
| 178 | + |
| 179 | +Get socket.io session id. |
| 180 | + |
| 181 | +### *Message* |
| 182 | +`message` Base class of all message object. |
| 183 | + |
| 184 | +`int_message` message contains a 64-bit integer. |
| 185 | + |
| 186 | +`double_message` message contains a double. |
| 187 | + |
| 188 | +`string_message` message contains a string. |
| 189 | + |
| 190 | +`array_message` message contains a `vector<message::ptr>`. |
| 191 | + |
| 192 | +`object_message` message contains a `map<string,message::ptr>`. |
| 193 | + |
| 194 | +`message::ptr` pointer to `message` object, it will be one of its derived classes, judge by `message.get_flag()`. |
| 195 | + |
| 196 | +All designated constructor of `message` objects is hidden, you need to create message and get the `message::ptr` by `[derived]_message:create()`. |
0 commit comments