| 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 | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 61 | <h2>libwebsocket_create_context - Create the websocket handler</h2> | 
| Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 62 | <i>struct libwebsocket_context *</i> | 
| Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 63 | <b>libwebsocket_create_context</b> | 
| Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 64 | (<i>int</i> <b>port</b>, | 
| Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 65 | <i>struct libwebsocket_protocols *</i> <b>protocols</b>, | 
| Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 66 | <i>const char *</i> <b>ssl_cert_filepath</b>, | 
 | 67 | <i>const char *</i> <b>ssl_private_key_filepath</b>, | 
 | 68 | <i>int</i> <b>gid</b>, | 
 | 69 | <i>int</i> <b>uid</b>) | 
| Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 70 | <h3>Arguments</h3> | 
 | 71 | <dl> | 
 | 72 | <dt><b>port</b> | 
| Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 73 | <dd>Port to listen on... you can use 0 to suppress listening on | 
 | 74 | any port, that's what you want if you are not running a | 
 | 75 | websocket server at all but just using it as a client | 
| Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 76 | <dt><b>protocols</b> | 
 | 77 | <dd>Array of structures listing supported protocols and a protocol- | 
 | 78 | specific callback for each one.  The list is ended with an | 
 | 79 | entry that has a NULL callback pointer. | 
| Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 80 | It's not const because we write the owning_server member | 
| Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 81 | <dt><b>ssl_cert_filepath</b> | 
 | 82 | <dd>If libwebsockets was compiled to use ssl, and you want | 
 | 83 | to listen using SSL, set to the filepath to fetch the | 
 | 84 | server cert from, otherwise NULL for unencrypted | 
 | 85 | <dt><b>ssl_private_key_filepath</b> | 
 | 86 | <dd>filepath to private key if wanting SSL mode, | 
 | 87 | else ignored | 
 | 88 | <dt><b>gid</b> | 
 | 89 | <dd>group id to change to after setting listen socket, or -1. | 
 | 90 | <dt><b>uid</b> | 
 | 91 | <dd>user id to change to after setting listen socket, or -1. | 
| Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 92 | </dl> | 
 | 93 | <h3>Description</h3> | 
 | 94 | <blockquote> | 
| Andy Green | 47943ae | 2010-11-12 11:15:49 +0000 | [diff] [blame] | 95 | This function creates the listening socket and takes care | 
| Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 96 | of all initialization in one step. | 
 | 97 | <p> | 
| Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 98 | After initialization, it returns a struct libwebsocket_context * that | 
 | 99 | represents this server.  After calling, user code needs to take care | 
 | 100 | of calling <b>libwebsocket_service</b> with the context pointer to get the | 
 | 101 | server's sockets serviced.  This can be done in the same process context | 
 | 102 | or a forked process, or another thread, | 
| Andy Green | 47943ae | 2010-11-12 11:15:49 +0000 | [diff] [blame] | 103 | <p> | 
 | 104 | The protocol callback functions are called for a handful of events | 
 | 105 | including http requests coming in, websocket connections becoming | 
| Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 106 | established, and data arriving; it's also called periodically to allow | 
 | 107 | async transmission. | 
 | 108 | <p> | 
| Andy Green | 47943ae | 2010-11-12 11:15:49 +0000 | [diff] [blame] | 109 | HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since | 
 | 110 | at that time websocket protocol has not been negotiated.  Other | 
 | 111 | protocols after the first one never see any HTTP callack activity. | 
 | 112 | <p> | 
| Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 113 | The server created is a simple http server by default; part of the | 
 | 114 | websocket standard is upgrading this http connection to a websocket one. | 
 | 115 | <p> | 
 | 116 | This allows the same server to provide files like scripts and favicon / | 
 | 117 | images or whatever over http and dynamic data over websockets all in | 
 | 118 | one place; they're all handled in the user callback. | 
 | 119 | </blockquote> | 
 | 120 | <hr> | 
| Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 121 | <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> | 
 | 122 | <i>int</i> | 
 | 123 | <b>libwebsockets_fork_service_loop</b> | 
 | 124 | (<i>struct libwebsocket_context *</i> <b>this</b>) | 
 | 125 | <h3>Arguments</h3> | 
 | 126 | <dl> | 
 | 127 | <dt><b>this</b> | 
 | 128 | <dd>server context returned by creation function | 
 | 129 | </dl> | 
 | 130 | <hr> | 
| Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 131 | <h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2> | 
 | 132 | <i>const struct libwebsocket_protocols *</i> | 
 | 133 | <b>libwebsockets_get_protocol</b> | 
 | 134 | (<i>struct libwebsocket *</i> <b>wsi</b>) | 
 | 135 | <h3>Arguments</h3> | 
 | 136 | <dl> | 
 | 137 | <dt><b>wsi</b> | 
 | 138 | <dd>pointer to struct websocket you want to know the protocol of | 
 | 139 | </dl> | 
 | 140 | <h3>Description</h3> | 
 | 141 | <blockquote> | 
 | 142 | <p> | 
 | 143 | This is useful to get the protocol to broadcast back to from inside | 
 | 144 | the callback. | 
 | 145 | </blockquote> | 
 | 146 | <hr> | 
| Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 147 | <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] | 148 | <i>int</i> | 
 | 149 | <b>libwebsockets_broadcast</b> | 
 | 150 | (<i>const struct libwebsocket_protocols *</i> <b>protocol</b>, | 
 | 151 | <i>unsigned char *</i> <b>buf</b>, | 
 | 152 | <i>size_t</i> <b>len</b>) | 
 | 153 | <h3>Arguments</h3> | 
 | 154 | <dl> | 
 | 155 | <dt><b>protocol</b> | 
 | 156 | <dd>pointer to the protocol you will broadcast to all members of | 
 | 157 | <dt><b>buf</b> | 
 | 158 | <dd>buffer containing the data to be broadcase.  NOTE: this has to be | 
 | 159 | allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before | 
 | 160 | the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the | 
 | 161 | case you are calling this function from callback context. | 
 | 162 | <dt><b>len</b> | 
 | 163 | <dd>length of payload data in buf, starting from buf. | 
 | 164 | </dl> | 
 | 165 | <h3>Description</h3> | 
 | 166 | <blockquote> | 
 | 167 | This function allows bulk sending of a packet to every connection using | 
 | 168 | the given protocol.  It does not send the data directly; instead it calls | 
 | 169 | the callback with a reason type of LWS_CALLBACK_BROADCAST.  If the callback | 
 | 170 | wants to actually send the data for that connection, the callback itself | 
 | 171 | should call <b>libwebsocket_write</b>. | 
 | 172 | <p> | 
 | 173 | <b>libwebsockets_broadcast</b> can be called from another fork context without | 
 | 174 | having to take any care about data visibility between the processes, it'll | 
 | 175 | "just work". | 
 | 176 | </blockquote> | 
 | 177 | <hr> | 
| Andy Green | 62a1293 | 2010-11-03 11:19:23 +0000 | [diff] [blame] | 178 | <h2>libwebsocket_write - Apply protocol then write data to client</h2> | 
 | 179 | <i>int</i> | 
 | 180 | <b>libwebsocket_write</b> | 
 | 181 | (<i>struct libwebsocket *</i> <b>wsi</b>, | 
 | 182 | <i>unsigned char *</i> <b>buf</b>, | 
 | 183 | <i>size_t</i> <b>len</b>, | 
 | 184 | <i>enum libwebsocket_write_protocol</i> <b>protocol</b>) | 
 | 185 | <h3>Arguments</h3> | 
 | 186 | <dl> | 
 | 187 | <dt><b>wsi</b> | 
 | 188 | <dd>Websocket instance (available from user callback) | 
 | 189 | <dt><b>buf</b> | 
 | 190 | <dd>The data to send.  For data being sent on a websocket | 
 | 191 | connection (ie, not default http), this buffer MUST have | 
 | 192 | LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer | 
 | 193 | and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid | 
 | 194 | in the buffer after (buf + len).  This is so the protocol | 
 | 195 | header and trailer data can be added in-situ. | 
 | 196 | <dt><b>len</b> | 
 | 197 | <dd>Count of the data bytes in the payload starting from buf | 
 | 198 | <dt><b>protocol</b> | 
 | 199 | <dd>Use LWS_WRITE_HTTP to reply to an http connection, and one | 
 | 200 | of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate | 
 | 201 | data on a websockets connection.  Remember to allow the extra | 
 | 202 | bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT | 
 | 203 | are used. | 
 | 204 | </dl> | 
 | 205 | <h3>Description</h3> | 
 | 206 | <blockquote> | 
 | 207 | This function provides the way to issue data back to the client | 
 | 208 | for both http and websocket protocols. | 
 | 209 | <p> | 
 | 210 | In the case of sending using websocket protocol, be sure to allocate | 
 | 211 | valid storage before and after buf as explained above.  This scheme | 
 | 212 | allows maximum efficiency of sending data and protocol in a single | 
 | 213 | packet while not burdening the user code with any protocol knowledge. | 
 | 214 | </blockquote> | 
 | 215 | <hr> | 
 | 216 | <h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2> | 
 | 217 | <i>int</i> | 
 | 218 | <b>libwebsockets_serve_http_file</b> | 
 | 219 | (<i>struct libwebsocket *</i> <b>wsi</b>, | 
 | 220 | <i>const char *</i> <b>file</b>, | 
 | 221 | <i>const char *</i> <b>content_type</b>) | 
 | 222 | <h3>Arguments</h3> | 
 | 223 | <dl> | 
 | 224 | <dt><b>wsi</b> | 
 | 225 | <dd>Websocket instance (available from user callback) | 
 | 226 | <dt><b>file</b> | 
 | 227 | <dd>The file to issue over http | 
 | 228 | <dt><b>content_type</b> | 
 | 229 | <dd>The http content type, eg, text/html | 
 | 230 | </dl> | 
 | 231 | <h3>Description</h3> | 
 | 232 | <blockquote> | 
 | 233 | This function is intended to be called from the callback in response | 
 | 234 | to http requests from the client.  It allows the callback to issue | 
 | 235 | local files down the http link in a single step. | 
 | 236 | </blockquote> | 
 | 237 | <hr> | 
| Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 238 | <h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2> | 
 | 239 | <i>size_t</i> | 
 | 240 | <b>libwebsockets_remaining_packet_payload</b> | 
 | 241 | (<i>struct libwebsocket *</i> <b>wsi</b>) | 
 | 242 | <h3>Arguments</h3> | 
 | 243 | <dl> | 
 | 244 | <dt><b>wsi</b> | 
 | 245 | <dd>Websocket instance (available from user callback) | 
 | 246 | </dl> | 
 | 247 | <h3>Description</h3> | 
 | 248 | <blockquote> | 
 | 249 | This function is intended to be called from the callback if the | 
 | 250 | user code is interested in "complete packets" from the client. | 
 | 251 | libwebsockets just passes through payload as it comes and issues a buffer | 
 | 252 | additionally when it hits a built-in limit.  The LWS_CALLBACK_RECEIVE | 
 | 253 | callback handler can use this API to find out if the buffer it has just | 
 | 254 | been given is the last piece of a "complete packet" from the client -- | 
 | 255 | when that is the case <b>libwebsockets_remaining_packet_payload</b> will return | 
 | 256 | 0. | 
 | 257 | <p> | 
 | 258 | Many protocols won't care becuse their packets are always small. | 
 | 259 | </blockquote> | 
 | 260 | <hr> | 
| Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 261 | <h2>callback - User server actions</h2> | 
 | 262 | <i>int</i> | 
 | 263 | <b>callback</b> | 
 | 264 | (<i>struct libwebsocket *</i> <b>wsi</b>, | 
 | 265 | <i>enum libwebsocket_callback_reasons</i> <b>reason</b>, | 
 | 266 | <i>void *</i> <b>user</b>, | 
 | 267 | <i>void *</i> <b>in</b>, | 
 | 268 | <i>size_t</i> <b>len</b>) | 
 | 269 | <h3>Arguments</h3> | 
 | 270 | <dl> | 
 | 271 | <dt><b>wsi</b> | 
 | 272 | <dd>Opaque websocket instance pointer | 
 | 273 | <dt><b>reason</b> | 
 | 274 | <dd>The reason for the call | 
 | 275 | <dt><b>user</b> | 
 | 276 | <dd>Pointer to per-session user data allocated by library | 
 | 277 | <dt><b>in</b> | 
 | 278 | <dd>Pointer used for some callback reasons | 
 | 279 | <dt><b>len</b> | 
 | 280 | <dd>Length set for some callback reasons | 
 | 281 | </dl> | 
 | 282 | <h3>Description</h3> | 
 | 283 | <blockquote> | 
 | 284 | This callback is the way the user controls what is served.  All the | 
 | 285 | protocol detail is hidden and handled by the library. | 
 | 286 | <p> | 
 | 287 | For each connection / session there is user data allocated that is | 
 | 288 | pointed to by "user".  You set the size of this user data area when | 
 | 289 | the library is initialized with libwebsocket_create_server. | 
 | 290 | <p> | 
 | 291 | You get an opportunity to initialize user data when called back with | 
 | 292 | LWS_CALLBACK_ESTABLISHED reason. | 
 | 293 | </blockquote> | 
 | 294 | <h3>LWS_CALLBACK_ESTABLISHED</h3> | 
 | 295 | <blockquote> | 
 | 296 | after successful websocket handshake | 
 | 297 | </blockquote> | 
 | 298 | <h3>LWS_CALLBACK_CLOSED</h3> | 
 | 299 | <blockquote> | 
 | 300 | when the websocket session ends | 
 | 301 | </blockquote> | 
 | 302 | <h3>LWS_CALLBACK_BROADCAST</h3> | 
 | 303 | <blockquote> | 
 | 304 | signal to send to client (you would use | 
 | 305 | <b>libwebsocket_write</b> taking care about the | 
 | 306 | special buffer requirements | 
 | 307 | </blockquote> | 
 | 308 | <h3>LWS_CALLBACK_RECEIVE</h3> | 
 | 309 | <blockquote> | 
 | 310 | data has appeared for the server, it can be | 
 | 311 | found at *in and is len bytes long | 
 | 312 | </blockquote> | 
 | 313 | <h3>LWS_CALLBACK_HTTP</h3> | 
 | 314 | <blockquote> | 
 | 315 | an http request has come from a client that is not | 
 | 316 | asking to upgrade the connection to a websocket | 
 | 317 | one.  This is a chance to serve http content, | 
 | 318 | for example, to send a script to the client | 
 | 319 | which will then open the websockets connection. | 
 | 320 | <tt><b>in</b></tt> points to the URI path requested and  | 
 | 321 | <b>libwebsockets_serve_http_file</b> makes it very | 
 | 322 | simple to send back a file to the client. | 
 | 323 | </blockquote> | 
 | 324 | <hr> | 
| Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 325 | <h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2> | 
 | 326 | <b>struct libwebsocket_protocols</b> {<br> | 
 | 327 |     <i>const char *</i> <b>name</b>;<br> | 
| Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 328 |     <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] | 329 |     <i>size_t</i> <b>per_session_data_size</b>;<br> | 
| Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 330 |     <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br> | 
 | 331 |     <i>int</i> <b>broadcast_socket_port</b>;<br> | 
 | 332 |     <i>int</i> <b>broadcast_socket_user_fd</b>;<br> | 
 | 333 |     <i>int</i> <b>protocol_index</b>;<br> | 
| Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 334 | };<br> | 
 | 335 | <h3>Members</h3> | 
 | 336 | <dl> | 
 | 337 | <dt><b>name</b> | 
 | 338 | <dd>Protocol name that must match the one given in the client | 
 | 339 | Javascript new WebSocket(url, 'protocol') name | 
 | 340 | <dt><b>callback</b> | 
 | 341 | <dd>The service callback used for this protocol.  It allows the | 
 | 342 | service action for an entire protocol to be encapsulated in | 
 | 343 | the protocol-specific callback | 
 | 344 | <dt><b>per_session_data_size</b> | 
 | 345 | <dd>Each new connection using this protocol gets | 
 | 346 | this much memory allocated on connection establishment and | 
 | 347 | freed on connection takedown.  A pointer to this per-connection | 
 | 348 | allocation is passed into the callback in the 'user' parameter | 
| Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 349 | <dt><b>owning_server</b> | 
 | 350 | <dd>the server init call fills in this opaque pointer when | 
 | 351 | registering this protocol with the server. | 
 | 352 | <dt><b>broadcast_socket_port</b> | 
 | 353 | <dd>the server init call fills this in with the | 
 | 354 | localhost port number used to forward broadcasts for this | 
 | 355 | protocol | 
 | 356 | <dt><b>broadcast_socket_user_fd</b> | 
 | 357 | <dd>the server init call fills this in ... the <b>main</b> | 
 | 358 | process context can write to this socket to perform broadcasts | 
 | 359 | (use the <b>libwebsockets_broadcast</b> api to do this instead, | 
 | 360 | it works from any process context) | 
 | 361 | <dt><b>protocol_index</b> | 
 | 362 | <dd>which protocol we are starting from zero | 
| Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 363 | </dl> | 
 | 364 | <h3>Description</h3> | 
 | 365 | <blockquote> | 
 | 366 | This structure represents one protocol supported by the server.  An | 
 | 367 | array of these structures is passed to <b>libwebsocket_create_server</b> | 
 | 368 | allows as many protocols as you like to be handled by one server. | 
 | 369 | </blockquote> | 
 | 370 | <hr> |