Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1 | <h2>libwebsocket_context_destroy - Destroy the websocket context</h2> |
| 2 | <i>void</i> |
| 3 | <b>libwebsocket_context_destroy</b> |
| 4 | (<i>struct libwebsocket_context *</i> <b>this</b>) |
| 5 | <h3>Arguments</h3> |
| 6 | <dl> |
| 7 | <dt><b>this</b> |
| 8 | <dd>Websocket context |
| 9 | </dl> |
| 10 | <h3>Description</h3> |
| 11 | <blockquote> |
| 12 | This function closes any active connections and then frees the |
| 13 | context. After calling this, any further use of the context is |
| 14 | undefined. |
| 15 | </blockquote> |
| 16 | <hr> |
| 17 | <h2>libwebsocket_service - Service any pending websocket activity</h2> |
| 18 | <i>int</i> |
| 19 | <b>libwebsocket_service</b> |
| 20 | (<i>struct libwebsocket_context *</i> <b>this</b>, |
| 21 | <i>int</i> <b>timeout_ms</b>) |
| 22 | <h3>Arguments</h3> |
| 23 | <dl> |
| 24 | <dt><b>this</b> |
| 25 | <dd>Websocket context |
| 26 | <dt><b>timeout_ms</b> |
| 27 | <dd>Timeout for poll; 0 means return immediately if nothing needed |
| 28 | service otherwise block and service immediately, returning |
| 29 | after the timeout if nothing needed service. |
| 30 | </dl> |
| 31 | <h3>Description</h3> |
| 32 | <blockquote> |
| 33 | This function deals with any pending websocket traffic, for three |
| 34 | kinds of event. It handles these events on both server and client |
| 35 | types of connection the same. |
| 36 | <p> |
| 37 | 1) Accept new connections to our context's server |
| 38 | <p> |
| 39 | 2) Perform pending broadcast writes initiated from other forked |
| 40 | processes (effectively serializing asynchronous broadcasts) |
| 41 | <p> |
| 42 | 3) Call the receive callback for incoming frame data received by |
| 43 | server or client connections. |
| 44 | <p> |
| 45 | You need to call this service function periodically to all the above |
| 46 | functions to happen; if your application is single-threaded you can |
| 47 | just call it in your main event loop. |
| 48 | <p> |
| 49 | Alternatively you can fork a new process that asynchronously handles |
| 50 | calling this service in a loop. In that case you are happy if this |
| 51 | call blocks your thread until it needs to take care of something and |
| 52 | would call it with a large nonzero timeout. Your loop then takes no |
| 53 | CPU while there is nothing happening. |
| 54 | <p> |
| 55 | If you are calling it in a single-threaded app, you don't want it to |
| 56 | wait around blocking other things in your loop from happening, so you |
| 57 | would call it with a timeout_ms of 0, so it returns immediately if |
| 58 | nothing is pending, or as soon as it services whatever was pending. |
| 59 | </blockquote> |
| 60 | <hr> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 61 | <h2>libwebsocket_callback_on_writable - Request a callback when this socket becomes able to be written to without blocking</h2> |
| 62 | <i>int</i> |
| 63 | <b>libwebsocket_callback_on_writable</b> |
| 64 | (<i>struct libwebsocket *</i> <b>wsi</b>) |
| 65 | <h3>Arguments</h3> |
| 66 | <dl> |
| 67 | <dt><b>wsi</b> |
| 68 | <dd>Websocket connection instance to get callback for |
| 69 | </dl> |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame^] | 70 | <h3>Description</h3> |
| 71 | <blockquote> |
| 72 | <p> |
| 73 | This only works for internal <b>poll</b> management, (ie, calling the libwebsocket |
| 74 | service loop, you will have to make your own arrangements if your <b>poll</b> |
| 75 | loop is managed externally. |
| 76 | </blockquote> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 77 | <hr> |
| 78 | <h2>libwebsocket_callback_on_writable_all_protocol - Request a callback for all connections using the given protocol when it becomes possible to write to each socket without blocking in turn.</h2> |
| 79 | <i>int</i> |
| 80 | <b>libwebsocket_callback_on_writable_all_protocol</b> |
| 81 | (<i>const struct libwebsocket_protocols *</i> <b>protocol</b>) |
| 82 | <h3>Arguments</h3> |
| 83 | <dl> |
| 84 | <dt><b>protocol</b> |
| 85 | <dd>Protocol whose connections will get callbacks |
| 86 | </dl> |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame^] | 87 | <h3>Description</h3> |
| 88 | <blockquote> |
| 89 | <p> |
| 90 | This only works for internal <b>poll</b> management, (ie, calling the libwebsocket |
| 91 | service loop, you will have to make your own arrangements if your <b>poll</b> |
| 92 | loop is managed externally. |
| 93 | </blockquote> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 94 | <hr> |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 95 | <h2>libwebsocket_get_socket_fd - returns the socket file descriptor</h2> |
| 96 | <i>int</i> |
| 97 | <b>libwebsocket_get_socket_fd</b> |
| 98 | (<i>struct libwebsocket *</i> <b>wsi</b>) |
| 99 | <h3>Arguments</h3> |
| 100 | <dl> |
| 101 | <dt><b>wsi</b> |
| 102 | <dd>Websocket connection instance |
| 103 | </dl> |
| 104 | <h3>Description</h3> |
| 105 | <blockquote> |
| 106 | <p> |
| 107 | You will not need this unless you are doing something special |
| 108 | </blockquote> |
| 109 | <hr> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 110 | <h2>libwebsocket_rx_flow_control - Enable and disable socket servicing for receieved packets.</h2> |
| 111 | <i>int</i> |
| 112 | <b>libwebsocket_rx_flow_control</b> |
| 113 | (<i>struct libwebsocket *</i> <b>wsi</b>, |
| 114 | <i>int</i> <b>enable</b>) |
| 115 | <h3>Arguments</h3> |
| 116 | <dl> |
| 117 | <dt><b>wsi</b> |
| 118 | <dd>Websocket connection instance to get callback for |
| 119 | <dt><b>enable</b> |
| 120 | <dd>0 = disable read servicing for this connection, 1 = enable |
| 121 | </dl> |
| 122 | <h3>Description</h3> |
| 123 | <blockquote> |
| 124 | <p> |
| 125 | If the output side of a server process becomes choked, this allows flow |
| 126 | control for the input side. |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame^] | 127 | <p> |
| 128 | This only works for internal <b>poll</b> management, (ie, calling the libwebsocket |
| 129 | service loop, you will have to make your own arrangements if your <b>poll</b> |
| 130 | loop is managed externally. |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 131 | </blockquote> |
| 132 | <hr> |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 133 | <h2>libwebsocket_canonical_hostname - returns this host's hostname</h2> |
| 134 | <i>const char *</i> |
| 135 | <b>libwebsocket_canonical_hostname</b> |
| 136 | (<i>struct libwebsocket_context *</i> <b>this</b>) |
| 137 | <h3>Arguments</h3> |
| 138 | <dl> |
| 139 | <dt><b>this</b> |
| 140 | <dd>Websocket context |
| 141 | </dl> |
| 142 | <h3>Description</h3> |
| 143 | <blockquote> |
| 144 | <p> |
| 145 | This is typically used by client code to fill in the host parameter |
| 146 | when making a client connection. You can only call it after the context |
| 147 | has been created. |
| 148 | </blockquote> |
| 149 | <hr> |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 150 | <h2>libwebsocket_create_context - Create the websocket handler</h2> |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 151 | <i>struct libwebsocket_context *</i> |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 152 | <b>libwebsocket_create_context</b> |
Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 153 | (<i>int</i> <b>port</b>, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 154 | <i>struct libwebsocket_protocols *</i> <b>protocols</b>, |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 155 | <i>const char *</i> <b>ssl_cert_filepath</b>, |
| 156 | <i>const char *</i> <b>ssl_private_key_filepath</b>, |
| 157 | <i>int</i> <b>gid</b>, |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 158 | <i>int</i> <b>uid</b>, |
| 159 | <i>unsigned int</i> <b>options</b>) |
Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 160 | <h3>Arguments</h3> |
| 161 | <dl> |
| 162 | <dt><b>port</b> |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 163 | <dd>Port to listen on... you can use 0 to suppress listening on |
| 164 | any port, that's what you want if you are not running a |
| 165 | websocket server at all but just using it as a client |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 166 | <dt><b>protocols</b> |
| 167 | <dd>Array of structures listing supported protocols and a protocol- |
| 168 | specific callback for each one. The list is ended with an |
| 169 | entry that has a NULL callback pointer. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 170 | It's not const because we write the owning_server member |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 171 | <dt><b>ssl_cert_filepath</b> |
| 172 | <dd>If libwebsockets was compiled to use ssl, and you want |
| 173 | to listen using SSL, set to the filepath to fetch the |
| 174 | server cert from, otherwise NULL for unencrypted |
| 175 | <dt><b>ssl_private_key_filepath</b> |
| 176 | <dd>filepath to private key if wanting SSL mode, |
| 177 | else ignored |
| 178 | <dt><b>gid</b> |
| 179 | <dd>group id to change to after setting listen socket, or -1. |
| 180 | <dt><b>uid</b> |
| 181 | <dd>user id to change to after setting listen socket, or -1. |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 182 | <dt><b>options</b> |
| 183 | <dd>0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK |
Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 184 | </dl> |
| 185 | <h3>Description</h3> |
| 186 | <blockquote> |
Andy Green | 47943ae | 2010-11-12 11:15:49 +0000 | [diff] [blame] | 187 | This function creates the listening socket and takes care |
Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 188 | of all initialization in one step. |
| 189 | <p> |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 190 | After initialization, it returns a struct libwebsocket_context * that |
| 191 | represents this server. After calling, user code needs to take care |
| 192 | of calling <b>libwebsocket_service</b> with the context pointer to get the |
| 193 | server's sockets serviced. This can be done in the same process context |
| 194 | or a forked process, or another thread, |
Andy Green | 47943ae | 2010-11-12 11:15:49 +0000 | [diff] [blame] | 195 | <p> |
| 196 | The protocol callback functions are called for a handful of events |
| 197 | including http requests coming in, websocket connections becoming |
Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 198 | established, and data arriving; it's also called periodically to allow |
| 199 | async transmission. |
| 200 | <p> |
Andy Green | 47943ae | 2010-11-12 11:15:49 +0000 | [diff] [blame] | 201 | HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since |
| 202 | at that time websocket protocol has not been negotiated. Other |
| 203 | protocols after the first one never see any HTTP callack activity. |
| 204 | <p> |
Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 205 | The server created is a simple http server by default; part of the |
| 206 | websocket standard is upgrading this http connection to a websocket one. |
| 207 | <p> |
| 208 | This allows the same server to provide files like scripts and favicon / |
| 209 | images or whatever over http and dynamic data over websockets all in |
| 210 | one place; they're all handled in the user callback. |
| 211 | </blockquote> |
| 212 | <hr> |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 213 | <h2>libwebsockets_fork_service_loop - Optional helper function forks off a process for the websocket server loop. You don't have to use this but if not, you have to make sure you are calling libwebsocket_service periodically to service the websocket traffic</h2> |
| 214 | <i>int</i> |
| 215 | <b>libwebsockets_fork_service_loop</b> |
| 216 | (<i>struct libwebsocket_context *</i> <b>this</b>) |
| 217 | <h3>Arguments</h3> |
| 218 | <dl> |
| 219 | <dt><b>this</b> |
| 220 | <dd>server context returned by creation function |
| 221 | </dl> |
| 222 | <hr> |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 223 | <h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2> |
| 224 | <i>const struct libwebsocket_protocols *</i> |
| 225 | <b>libwebsockets_get_protocol</b> |
| 226 | (<i>struct libwebsocket *</i> <b>wsi</b>) |
| 227 | <h3>Arguments</h3> |
| 228 | <dl> |
| 229 | <dt><b>wsi</b> |
| 230 | <dd>pointer to struct websocket you want to know the protocol of |
| 231 | </dl> |
| 232 | <h3>Description</h3> |
| 233 | <blockquote> |
| 234 | <p> |
| 235 | This is useful to get the protocol to broadcast back to from inside |
| 236 | the callback. |
| 237 | </blockquote> |
| 238 | <hr> |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 239 | <h2>libwebsockets_broadcast - Sends a buffer to the callback for all active connections of the given protocol.</h2> |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 240 | <i>int</i> |
| 241 | <b>libwebsockets_broadcast</b> |
| 242 | (<i>const struct libwebsocket_protocols *</i> <b>protocol</b>, |
| 243 | <i>unsigned char *</i> <b>buf</b>, |
| 244 | <i>size_t</i> <b>len</b>) |
| 245 | <h3>Arguments</h3> |
| 246 | <dl> |
| 247 | <dt><b>protocol</b> |
| 248 | <dd>pointer to the protocol you will broadcast to all members of |
| 249 | <dt><b>buf</b> |
| 250 | <dd>buffer containing the data to be broadcase. NOTE: this has to be |
| 251 | allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before |
| 252 | the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the |
| 253 | case you are calling this function from callback context. |
| 254 | <dt><b>len</b> |
| 255 | <dd>length of payload data in buf, starting from buf. |
| 256 | </dl> |
| 257 | <h3>Description</h3> |
| 258 | <blockquote> |
| 259 | This function allows bulk sending of a packet to every connection using |
| 260 | the given protocol. It does not send the data directly; instead it calls |
| 261 | the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback |
| 262 | wants to actually send the data for that connection, the callback itself |
| 263 | should call <b>libwebsocket_write</b>. |
| 264 | <p> |
| 265 | <b>libwebsockets_broadcast</b> can be called from another fork context without |
| 266 | having to take any care about data visibility between the processes, it'll |
| 267 | "just work". |
| 268 | </blockquote> |
| 269 | <hr> |
Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 270 | <h2>libwebsocket_write - Apply protocol then write data to client</h2> |
| 271 | <i>int</i> |
| 272 | <b>libwebsocket_write</b> |
| 273 | (<i>struct libwebsocket *</i> <b>wsi</b>, |
| 274 | <i>unsigned char *</i> <b>buf</b>, |
| 275 | <i>size_t</i> <b>len</b>, |
| 276 | <i>enum libwebsocket_write_protocol</i> <b>protocol</b>) |
| 277 | <h3>Arguments</h3> |
| 278 | <dl> |
| 279 | <dt><b>wsi</b> |
| 280 | <dd>Websocket instance (available from user callback) |
| 281 | <dt><b>buf</b> |
| 282 | <dd>The data to send. For data being sent on a websocket |
| 283 | connection (ie, not default http), this buffer MUST have |
| 284 | LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer |
| 285 | and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid |
| 286 | in the buffer after (buf + len). This is so the protocol |
| 287 | header and trailer data can be added in-situ. |
| 288 | <dt><b>len</b> |
| 289 | <dd>Count of the data bytes in the payload starting from buf |
| 290 | <dt><b>protocol</b> |
| 291 | <dd>Use LWS_WRITE_HTTP to reply to an http connection, and one |
| 292 | of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate |
| 293 | data on a websockets connection. Remember to allow the extra |
| 294 | bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT |
| 295 | are used. |
| 296 | </dl> |
| 297 | <h3>Description</h3> |
| 298 | <blockquote> |
| 299 | This function provides the way to issue data back to the client |
| 300 | for both http and websocket protocols. |
| 301 | <p> |
| 302 | In the case of sending using websocket protocol, be sure to allocate |
| 303 | valid storage before and after buf as explained above. This scheme |
| 304 | allows maximum efficiency of sending data and protocol in a single |
| 305 | packet while not burdening the user code with any protocol knowledge. |
| 306 | </blockquote> |
| 307 | <hr> |
| 308 | <h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2> |
| 309 | <i>int</i> |
| 310 | <b>libwebsockets_serve_http_file</b> |
| 311 | (<i>struct libwebsocket *</i> <b>wsi</b>, |
| 312 | <i>const char *</i> <b>file</b>, |
| 313 | <i>const char *</i> <b>content_type</b>) |
| 314 | <h3>Arguments</h3> |
| 315 | <dl> |
| 316 | <dt><b>wsi</b> |
| 317 | <dd>Websocket instance (available from user callback) |
| 318 | <dt><b>file</b> |
| 319 | <dd>The file to issue over http |
| 320 | <dt><b>content_type</b> |
| 321 | <dd>The http content type, eg, text/html |
| 322 | </dl> |
| 323 | <h3>Description</h3> |
| 324 | <blockquote> |
| 325 | This function is intended to be called from the callback in response |
| 326 | to http requests from the client. It allows the callback to issue |
| 327 | local files down the http link in a single step. |
| 328 | </blockquote> |
| 329 | <hr> |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 330 | <h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2> |
| 331 | <i>size_t</i> |
| 332 | <b>libwebsockets_remaining_packet_payload</b> |
| 333 | (<i>struct libwebsocket *</i> <b>wsi</b>) |
| 334 | <h3>Arguments</h3> |
| 335 | <dl> |
| 336 | <dt><b>wsi</b> |
| 337 | <dd>Websocket instance (available from user callback) |
| 338 | </dl> |
| 339 | <h3>Description</h3> |
| 340 | <blockquote> |
| 341 | This function is intended to be called from the callback if the |
| 342 | user code is interested in "complete packets" from the client. |
| 343 | libwebsockets just passes through payload as it comes and issues a buffer |
| 344 | additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE |
| 345 | callback handler can use this API to find out if the buffer it has just |
| 346 | been given is the last piece of a "complete packet" from the client -- |
| 347 | when that is the case <b>libwebsockets_remaining_packet_payload</b> will return |
| 348 | 0. |
| 349 | <p> |
| 350 | Many protocols won't care becuse their packets are always small. |
| 351 | </blockquote> |
| 352 | <hr> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 353 | <h2>libwebsocket_client_connect - Connect to another websocket server</h2> |
| 354 | <i>struct libwebsocket *</i> |
| 355 | <b>libwebsocket_client_connect</b> |
| 356 | (<i>struct libwebsocket_context *</i> <b>this</b>, |
| 357 | <i>const char *</i> <b>address</b>, |
| 358 | <i>int</i> <b>port</b>, |
| 359 | <i>int</i> <b>ssl_connection</b>, |
| 360 | <i>const char *</i> <b>path</b>, |
| 361 | <i>const char *</i> <b>host</b>, |
| 362 | <i>const char *</i> <b>origin</b>, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 363 | <i>const char *</i> <b>protocol</b>, |
| 364 | <i>int</i> <b>ietf_version_or_minus_one</b>) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 365 | <h3>Arguments</h3> |
| 366 | <dl> |
| 367 | <dt><b>this</b> |
| 368 | <dd>Websocket context |
| 369 | <dt><b>address</b> |
| 370 | <dd>Remote server address, eg, "myserver.com" |
| 371 | <dt><b>port</b> |
| 372 | <dd>Port to connect to on the remote server, eg, 80 |
| 373 | <dt><b>ssl_connection</b> |
| 374 | <dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self |
| 375 | signed certs |
| 376 | <dt><b>path</b> |
| 377 | <dd>Websocket path on server |
| 378 | <dt><b>host</b> |
| 379 | <dd>Hostname on server |
| 380 | <dt><b>origin</b> |
| 381 | <dd>Socket origin name |
| 382 | <dt><b>protocol</b> |
| 383 | <dd>Comma-separated list of protocols being asked for from |
| 384 | the server, or just one. The server will pick the one it |
| 385 | likes best. |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 386 | <dt><b>ietf_version_or_minus_one</b> |
| 387 | <dd>-1 to ask to connect using the default, latest |
| 388 | protocol supported, or the specific protocol ordinal |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 389 | </dl> |
| 390 | <h3>Description</h3> |
| 391 | <blockquote> |
| 392 | This function creates a connection to a remote server |
| 393 | </blockquote> |
| 394 | <hr> |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 395 | <h2>callback - User server actions</h2> |
| 396 | <i>int</i> |
| 397 | <b>callback</b> |
| 398 | (<i>struct libwebsocket *</i> <b>wsi</b>, |
| 399 | <i>enum libwebsocket_callback_reasons</i> <b>reason</b>, |
| 400 | <i>void *</i> <b>user</b>, |
| 401 | <i>void *</i> <b>in</b>, |
| 402 | <i>size_t</i> <b>len</b>) |
| 403 | <h3>Arguments</h3> |
| 404 | <dl> |
| 405 | <dt><b>wsi</b> |
| 406 | <dd>Opaque websocket instance pointer |
| 407 | <dt><b>reason</b> |
| 408 | <dd>The reason for the call |
| 409 | <dt><b>user</b> |
| 410 | <dd>Pointer to per-session user data allocated by library |
| 411 | <dt><b>in</b> |
| 412 | <dd>Pointer used for some callback reasons |
| 413 | <dt><b>len</b> |
| 414 | <dd>Length set for some callback reasons |
| 415 | </dl> |
| 416 | <h3>Description</h3> |
| 417 | <blockquote> |
| 418 | This callback is the way the user controls what is served. All the |
| 419 | protocol detail is hidden and handled by the library. |
| 420 | <p> |
| 421 | For each connection / session there is user data allocated that is |
| 422 | pointed to by "user". You set the size of this user data area when |
| 423 | the library is initialized with libwebsocket_create_server. |
| 424 | <p> |
| 425 | You get an opportunity to initialize user data when called back with |
| 426 | LWS_CALLBACK_ESTABLISHED reason. |
| 427 | </blockquote> |
| 428 | <h3>LWS_CALLBACK_ESTABLISHED</h3> |
| 429 | <blockquote> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 430 | after the server completes a handshake with |
| 431 | an incoming client |
| 432 | </blockquote> |
| 433 | <h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3> |
| 434 | <blockquote> |
| 435 | after your client connection completed |
| 436 | a handshake with the remote server |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 437 | </blockquote> |
| 438 | <h3>LWS_CALLBACK_CLOSED</h3> |
| 439 | <blockquote> |
| 440 | when the websocket session ends |
| 441 | </blockquote> |
| 442 | <h3>LWS_CALLBACK_BROADCAST</h3> |
| 443 | <blockquote> |
| 444 | signal to send to client (you would use |
| 445 | <b>libwebsocket_write</b> taking care about the |
| 446 | special buffer requirements |
| 447 | </blockquote> |
| 448 | <h3>LWS_CALLBACK_RECEIVE</h3> |
| 449 | <blockquote> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 450 | data has appeared for this server endpoint from a |
| 451 | remote client, it can be found at *in and is |
| 452 | len bytes long |
| 453 | </blockquote> |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 454 | <h3>LWS_CALLBACK_CLIENT_RECEIVE_PONG</h3> |
| 455 | <blockquote> |
| 456 | if you elected to see PONG packets, |
| 457 | they appear with this callback reason. PONG |
| 458 | packets only exist in 04+ protocol |
| 459 | </blockquote> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 460 | <h3>LWS_CALLBACK_CLIENT_RECEIVE</h3> |
| 461 | <blockquote> |
| 462 | data has appeared from the server for the |
| 463 | client connection, it can be found at *in and |
| 464 | is len bytes long |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 465 | </blockquote> |
| 466 | <h3>LWS_CALLBACK_HTTP</h3> |
| 467 | <blockquote> |
| 468 | an http request has come from a client that is not |
| 469 | asking to upgrade the connection to a websocket |
| 470 | one. This is a chance to serve http content, |
| 471 | for example, to send a script to the client |
| 472 | which will then open the websockets connection. |
Andy Green | 7619c47 | 2011-01-23 17:47:08 +0000 | [diff] [blame] | 473 | <tt><b>in</b></tt> points to the URI path requested and |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 474 | <b>libwebsockets_serve_http_file</b> makes it very |
| 475 | simple to send back a file to the client. |
| 476 | </blockquote> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 477 | <h3>LWS_CALLBACK_CLIENT_WRITEABLE</h3> |
| 478 | <blockquote> |
| 479 | if you call |
| 480 | <b>libwebsocket_callback_on_writable</b> on a connection, you will |
| 481 | get this callback coming when the connection socket is able to |
| 482 | accept another write packet without blocking. If it already |
| 483 | was able to take another packet without blocking, you'll get |
| 484 | this callback at the next call to the service loop function. |
| 485 | </blockquote> |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 486 | <hr> |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 487 | <h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2> |
| 488 | <b>struct libwebsocket_protocols</b> {<br> |
| 489 | <i>const char *</i> <b>name</b>;<br> |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 490 | <i>int (*</i><b>callback</b>) <i>(struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len)</i>;<br> |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 491 | <i>size_t</i> <b>per_session_data_size</b>;<br> |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 492 | <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br> |
| 493 | <i>int</i> <b>broadcast_socket_port</b>;<br> |
| 494 | <i>int</i> <b>broadcast_socket_user_fd</b>;<br> |
| 495 | <i>int</i> <b>protocol_index</b>;<br> |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 496 | };<br> |
| 497 | <h3>Members</h3> |
| 498 | <dl> |
| 499 | <dt><b>name</b> |
| 500 | <dd>Protocol name that must match the one given in the client |
| 501 | Javascript new WebSocket(url, 'protocol') name |
| 502 | <dt><b>callback</b> |
| 503 | <dd>The service callback used for this protocol. It allows the |
| 504 | service action for an entire protocol to be encapsulated in |
| 505 | the protocol-specific callback |
| 506 | <dt><b>per_session_data_size</b> |
| 507 | <dd>Each new connection using this protocol gets |
| 508 | this much memory allocated on connection establishment and |
| 509 | freed on connection takedown. A pointer to this per-connection |
| 510 | allocation is passed into the callback in the 'user' parameter |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 511 | <dt><b>owning_server</b> |
| 512 | <dd>the server init call fills in this opaque pointer when |
| 513 | registering this protocol with the server. |
| 514 | <dt><b>broadcast_socket_port</b> |
| 515 | <dd>the server init call fills this in with the |
| 516 | localhost port number used to forward broadcasts for this |
| 517 | protocol |
| 518 | <dt><b>broadcast_socket_user_fd</b> |
| 519 | <dd>the server init call fills this in ... the <b>main</b> |
| 520 | process context can write to this socket to perform broadcasts |
| 521 | (use the <b>libwebsockets_broadcast</b> api to do this instead, |
| 522 | it works from any process context) |
| 523 | <dt><b>protocol_index</b> |
| 524 | <dd>which protocol we are starting from zero |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 525 | </dl> |
| 526 | <h3>Description</h3> |
| 527 | <blockquote> |
| 528 | This structure represents one protocol supported by the server. An |
| 529 | array of these structures is passed to <b>libwebsocket_create_server</b> |
| 530 | allows as many protocols as you like to be handled by one server. |
| 531 | </blockquote> |
| 532 | <hr> |