|
| 1 | +# WebsocketRailsClient++ (v0.7.1) |
| 2 | + |
| 3 | +WebsocketRailsClient++ is a C++ library that uses the implementation of RFC6455 (The WebSocket Protocol) |
| 4 | +implemented in the WebSocket++ library, the Json++ light-weight JSON parser and the Boost library. It allows |
| 5 | +connections to the Ruby Websocket-Rails server and supports the Websocket-Rails protocol. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* Boost library up and running, http://www.boost.org/ (>= 1.48 | see own license) |
| 10 | +* WebSocket++ library, https://github.com/zaphoyd/websocketpp (>= 0.3.0 | see own license) |
| 11 | +* Jsonxx library, https://github.com/hjiang/jsonxx (latest | see own license) |
| 12 | +* Websocket-Rails server, https://github.com/websocket-rails/websocket-rails (v0.7.0) |
| 13 | + |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +* Full support for RFC6455 |
| 18 | +* Full Websocket-Rails protocol support |
| 19 | +* Message/event based interface |
| 20 | +* Supports secure WebSockets (TLS), IPv6, and explicit proxies. |
| 21 | +* Callbacks with member and non-member functions |
| 22 | +* Thread-safe |
| 23 | + |
| 24 | + |
| 25 | +## Websocket-Rails Protocol Implementation |
| 26 | + |
| 27 | + |
| 28 | +### Event Dispatcher |
| 29 | + |
| 30 | +#### Connection |
| 31 | + |
| 32 | + * ```connect(std::string url)``` : Start connection of the client. |
| 33 | + * ```disconnect()``` : Disconnect client. |
| 34 | + * ```reconnect()``` : Re-connect the client with all registered channels. |
| 35 | + |
| 36 | +#### Connection Callbacks |
| 37 | + |
| 38 | + * ```on_open(boost::bind cb)``` : callback on open connection. |
| 39 | + * ```on_close(boost::bind cb)``` : callback on close connection. |
| 40 | + * ```on_fail(boost::bind cb)``` : callback on fail connection. |
| 41 | + |
| 42 | +#### Trigger an Event on Server |
| 43 | + |
| 44 | +* ```trigger(std::string event_name, jsonxx::Object event_data)``` : trigger event with data without callback. |
| 45 | +* ```trigger(std::string event_name, jsonxx::Object event_data, boost::bind cb_succ, boost::bind cb_fail)``` : trigger event with data and callbacks. |
| 46 | + |
| 47 | +#### Bind to an Incoming Event |
| 48 | + |
| 49 | +* ```bind(std::string event_name, boost::bind cb)``` : Bind to an event name with callback. |
| 50 | + |
| 51 | +#### Unbind an Incoming Event |
| 52 | + |
| 53 | +* ```unbindAll(std::string event_name)``` : Unbind all callbacks on a specific event name. |
| 54 | + |
| 55 | +### Channel Event Dispatcher |
| 56 | + |
| 57 | +#### Channel Management |
| 58 | + |
| 59 | +* ```subscribe(std::string channel_name)``` : Subscribe to a channel. |
| 60 | +* ```subscribePrivate(std::string channel_name, boost::bind cb_succ, boost::bind cb_fail)``` : Subscribe to a private channel. |
| 61 | +* ```unsubscribe(std::string channel_name)``` : Unsubscribe a channel. |
| 62 | + |
| 63 | +#### Trigger a Channel-Event on Server |
| 64 | + |
| 65 | +* ```trigger(std::string event_name, jsonxx::Object event_data)``` : trigger channel event with data without callback. |
| 66 | +* ```trigger(std::string event_name, jsonxx::Object event_data, boost::bind cb_succ, boost::bind cb_fail)``` : trigger channel event with data and callbacks. |
| 67 | + |
| 68 | +#### Bind to an Incoming Channel-Event |
| 69 | + |
| 70 | +* ```bind(std::string event_name, boost::bind cb)``` : Bind to a channel event with callback. |
| 71 | + |
| 72 | +## Compile |
| 73 | + |
| 74 | +### C++ Linker |
| 75 | + |
| 76 | +#### Linker Flag -l |
| 77 | + |
| 78 | +* **Booost libraries:** boost_system, boost_thread |
| 79 | +* **System libraries:** pthread, rt |
| 80 | +* **TSL libraries:** ssl, crypto |
| 81 | + |
| 82 | +### C++ Compiler |
| 83 | + |
| 84 | +#### Compiler Flag -D |
| 85 | + |
| 86 | +* ```-D_WEBSOCKETPP_CPP11_STL_``` |
| 87 | + |
| 88 | +#### Compiler Flag -I |
| 89 | + |
| 90 | +* ```-I"/path/to/includes"``` |
| 91 | + |
| 92 | +#### Compiler Flag -std |
| 93 | + |
| 94 | +* ```-std=c++0x``` |
| 95 | + |
| 96 | + |
| 97 | +## Usage |
| 98 | + |
| 99 | +* Includes |
| 100 | + |
| 101 | +```cpp |
| 102 | +#include <iostream> |
| 103 | +#include "websocket-rails-client/websocket_rails.hpp" |
| 104 | +``` |
| 105 | + |
| 106 | +* Callback definitions |
| 107 | + |
| 108 | +```cpp |
| 109 | +/* Non-member callback function for onOpen */ |
| 110 | +void on_open(jsonxx::Object data) { |
| 111 | + std::cout << "Function on_open called" << std::endl; |
| 112 | + std::cout << data.json() << std::endl; |
| 113 | +} |
| 114 | + |
| 115 | +/* Non-member callback functions */ |
| 116 | +void callback(jsonxx::Object data) { |
| 117 | + std::cout << "Function callback called" << std::endl; |
| 118 | + std::cout << data.json() << std::endl; |
| 119 | +} |
| 120 | +void success_func(jsonxx::Object data) { ... } |
| 121 | +void failure_func(jsonxx::Object data) { ... } |
| 122 | +void bind_func_with_params(jsonxx::Object data, int number, bool valid) { ... } |
| 123 | +``` |
| 124 | + |
| 125 | +* Initialization |
| 126 | + |
| 127 | +```cpp |
| 128 | +WebsocketRails dispatcher("ws://localhost:3000/websocket"); |
| 129 | + |
| 130 | +/* Connection callbacks definitions */ |
| 131 | +dispatcher.onOpen(boost::bind(on_open, _1)); |
| 132 | +dispatcher.onClose(boost::bind(on_close, _1)); |
| 133 | +dispatcher.onFail(boost::bind(on_fail, _1)); |
| 134 | + |
| 135 | +/* Define binds before connect. */ |
| 136 | + |
| 137 | +/* Start Websocket-Rails connection handshake */ |
| 138 | +dispatcher.connect(); |
| 139 | +``` |
| 140 | + |
| 141 | +* Bind events to callbacks |
| 142 | + |
| 143 | +```cpp |
| 144 | +/* Event bind callback definition */ |
| 145 | +dispatcher.bind("users.pool", boost::bind(callback, _1)); |
| 146 | + |
| 147 | +/* Event bind callback definition with member function callback */ |
| 148 | +Foo my_foo; |
| 149 | +Event event3 = dispatcher.bind("users.pool", boost::bind(&Foo::success_func, my_foo, _1)); |
| 150 | +``` |
| 151 | + |
| 152 | +/* Event unbind callback definition */ |
| 153 | +dispatcher.unbindAll("users.pool"); |
| 154 | + |
| 155 | +* Trigger events |
| 156 | + |
| 157 | +```cpp |
| 158 | +/* Trigger an event */ |
| 159 | +Event event1 = dispatcher.trigger("users.create", jsonxx::Object("name", "Hans Mustermann")); |
| 160 | +Event event2 = dispatcher.trigger("users.create", jsonxx::Object("name", "Frau Mustermann"), boost::bind(success_func, _1), boost::bind(failure_func, _1)); |
| 161 | + |
| 162 | +/* Trigger an event with member function callback */ |
| 163 | +Bar my_bar; |
| 164 | +Event event3 = dispatcher.trigger("users.pool", boost::bind(&Foo::success_func, my_bar, _1), boost::bind(&Foo::failure_func, my_bar, _1)); |
| 165 | +``` |
| 166 | + |
| 167 | +* Use channels |
| 168 | + |
| 169 | +```cpp |
| 170 | +/* Subscribe to channels */ |
| 171 | +Channel channel1 = dispatcher.subscribe("Authors"); |
| 172 | +Channel channel2 = dispatcher.subscribe("Users", boost::bind(success_func, _1), boost::bind(failure_func, _1)); |
| 173 | +Channel private_channel1 = dispatcher.subscribePrivate("Administrators"); |
| 174 | +Channel private_channel2 = dispatcher.subscribePrivate("Moderators", boost::bind(success_func, _1), boost::bind(failure_func, _1)); |
| 175 | +``` |
| 176 | + |
| 177 | +* Trigger and bind channel-events |
| 178 | + |
| 179 | +```cpp |
| 180 | +/* Trigger an event on channel */ |
| 181 | +Event event = channel1.trigger("users.create", jsonxx::Object("name", "Hans Mustermann")); |
| 182 | +Event event = channel2.trigger("users.create", jsonxx::Object("name", "Frau Mustermann"), boost::bind(success_func, _1), boost::bind(failure_func, _1)); |
| 183 | + |
| 184 | +/* Channel event bind callback definition */ |
| 185 | +channel1.bind("users.pool", boost::bind(callback, _1)); |
| 186 | +``` |
| 187 | + |
| 188 | +* Callback additional parameters |
| 189 | + |
| 190 | +```cpp |
| 191 | +/* Define boost binds with additional parameters for the callback function */ |
| 192 | +boost::bind(bind_func_with_params, _1, param1, param2); |
| 193 | +boost::bind(bind_func_with_params, _1, 123, true); |
| 194 | +boost::bind(bind_func_with_params, _1, 123, param2); |
| 195 | + |
| 196 | +``` |
| 197 | + |
| 198 | +* Other |
| 199 | + |
| 200 | +To authenticate a user a separate C++ HTTP client library is required. |
| 201 | + |
| 202 | + |
| 203 | +## TODO |
| 204 | + |
| 205 | +* Add timestamp to events so that it is possible to clean the events queue with old events without response. |
| 206 | +* Implement a clean-up method for the queue. |
| 207 | +* See how to persist the connection object (```this->conn```) in the dispatcher so that the function ```trigger()``` can put the event trigger in event_queue if disconnected. |
| 208 | + |
| 209 | + |
| 210 | +## Author |
| 211 | + |
| 212 | +Egon Zemmer, Phlegx Systems - @phlegx |
| 213 | + |
| 214 | + |
| 215 | +## License |
| 216 | + |
| 217 | +``` |
| 218 | +The MIT License (http://opensource.org/licenses/MIT) |
| 219 | + |
| 220 | +Copyright (C) 2015 Egon Zemmer, Phlegx Systems |
| 221 | + |
| 222 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 223 | +to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 224 | +and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 225 | + |
| 226 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 227 | + |
| 228 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 229 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 230 | +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 231 | +OTHER DEALINGS IN THE SOFTWARE. |
| 232 | +``` |
0 commit comments