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