blob: e908d143827c032c2b4abcb8c2a8ef6f37c5766c [file] [log] [blame]
Andy Greenf7ee5492011-02-13 09:04:21 +00001<h2>libwebsockets_hangup_on_client - Server calls to terminate client connection</h2>
2<i>void</i>
3<b>libwebsockets_hangup_on_client</b>
Peter Hinz56885f32011-03-02 22:03:47 +00004(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Greenf7ee5492011-02-13 09:04:21 +00005<i>int</i> <b>fd</b>)
6<h3>Arguments</h3>
7<dl>
Peter Hinz56885f32011-03-02 22:03:47 +00008<dt><b>context</b>
Andy Greenf7ee5492011-02-13 09:04:21 +00009<dd>libwebsockets context
10<dt><b>fd</b>
11<dd>Connection socket descriptor
12</dl>
13<hr>
Andy Green07034092011-02-13 08:37:12 +000014<h2>libwebsockets_get_peer_addresses - Get client address information</h2>
15<i>void</i>
16<b>libwebsockets_get_peer_addresses</b>
17(<i>int</i> <b>fd</b>,
18<i>char *</i> <b>name</b>,
19<i>int</i> <b>name_len</b>,
20<i>char *</i> <b>rip</b>,
21<i>int</i> <b>rip_len</b>)
22<h3>Arguments</h3>
23<dl>
24<dt><b>fd</b>
25<dd>Connection socket descriptor
26<dt><b>name</b>
27<dd>Buffer to take client address name
28<dt><b>name_len</b>
29<dd>Length of client address name buffer
30<dt><b>rip</b>
31<dd>Buffer to take client address IP qotted quad
32<dt><b>rip_len</b>
33<dd>Length of client address IP buffer
34</dl>
35<h3>Description</h3>
36<blockquote>
37This function fills in <tt><b>name</b></tt> and <tt><b>rip</b></tt> with the name and IP of
38the client connected with socket descriptor <tt><b>fd</b></tt>. Names may be
39truncated if there is not enough room. If either cannot be
40determined, they will be returned as valid zero-length strings.
41</blockquote>
42<hr>
Andy Green9f990342011-02-12 11:57:45 +000043<h2>libwebsocket_service_fd - Service polled socket with something waiting</h2>
44<i>int</i>
45<b>libwebsocket_service_fd</b>
Peter Hinz56885f32011-03-02 22:03:47 +000046(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green9f990342011-02-12 11:57:45 +000047<i>struct pollfd *</i> <b>pollfd</b>)
48<h3>Arguments</h3>
49<dl>
Peter Hinz56885f32011-03-02 22:03:47 +000050<dt><b>context</b>
Andy Green9f990342011-02-12 11:57:45 +000051<dd>Websocket context
52<dt><b>pollfd</b>
53<dd>The pollfd entry describing the socket fd and which events
54happened.
55</dl>
56<h3>Description</h3>
57<blockquote>
58This function closes any active connections and then frees the
59context. After calling this, any further use of the context is
60undefined.
61</blockquote>
62<hr>
Andy Green6964bb52011-01-23 16:50:33 +000063<h2>libwebsocket_context_destroy - Destroy the websocket context</h2>
64<i>void</i>
65<b>libwebsocket_context_destroy</b>
Peter Hinz56885f32011-03-02 22:03:47 +000066(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Green6964bb52011-01-23 16:50:33 +000067<h3>Arguments</h3>
68<dl>
Peter Hinz56885f32011-03-02 22:03:47 +000069<dt><b>context</b>
Andy Green6964bb52011-01-23 16:50:33 +000070<dd>Websocket context
71</dl>
72<h3>Description</h3>
73<blockquote>
74This function closes any active connections and then frees the
75context. After calling this, any further use of the context is
76undefined.
77</blockquote>
78<hr>
79<h2>libwebsocket_service - Service any pending websocket activity</h2>
80<i>int</i>
81<b>libwebsocket_service</b>
Peter Hinz56885f32011-03-02 22:03:47 +000082(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green6964bb52011-01-23 16:50:33 +000083<i>int</i> <b>timeout_ms</b>)
84<h3>Arguments</h3>
85<dl>
Peter Hinz56885f32011-03-02 22:03:47 +000086<dt><b>context</b>
Andy Green6964bb52011-01-23 16:50:33 +000087<dd>Websocket context
88<dt><b>timeout_ms</b>
89<dd>Timeout for poll; 0 means return immediately if nothing needed
90service otherwise block and service immediately, returning
91after the timeout if nothing needed service.
92</dl>
93<h3>Description</h3>
94<blockquote>
95This function deals with any pending websocket traffic, for three
96kinds of event. It handles these events on both server and client
97types of connection the same.
98<p>
991) Accept new connections to our context's server
100<p>
1012) Perform pending broadcast writes initiated from other forked
102processes (effectively serializing asynchronous broadcasts)
103<p>
1043) Call the receive callback for incoming frame data received by
105server or client connections.
106<p>
107You need to call this service function periodically to all the above
108functions to happen; if your application is single-threaded you can
109just call it in your main event loop.
110<p>
111Alternatively you can fork a new process that asynchronously handles
112calling this service in a loop. In that case you are happy if this
113call blocks your thread until it needs to take care of something and
114would call it with a large nonzero timeout. Your loop then takes no
115CPU while there is nothing happening.
116<p>
117If you are calling it in a single-threaded app, you don't want it to
118wait around blocking other things in your loop from happening, so you
119would call it with a timeout_ms of 0, so it returns immediately if
120nothing is pending, or as soon as it services whatever was pending.
121</blockquote>
122<hr>
Andy Green32375b72011-02-19 08:32:53 +0000123<h2>libwebsocket_callback_on_writable - Request a callback when this socket becomes able to be written to without blocking</h2>
Andy Green90c7cbc2011-01-27 06:26:52 +0000124<i>int</i>
125<b>libwebsocket_callback_on_writable</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000126(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green62c54d22011-02-14 09:14:25 +0000127<i>struct libwebsocket *</i> <b>wsi</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000128<h3>Arguments</h3>
129<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000130<dt><b>context</b>
Andy Green32375b72011-02-19 08:32:53 +0000131<dd>libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +0000132<dt><b>wsi</b>
133<dd>Websocket connection instance to get callback for
134</dl>
135<hr>
136<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>
137<i>int</i>
138<b>libwebsocket_callback_on_writable_all_protocol</b>
139(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>)
140<h3>Arguments</h3>
141<dl>
142<dt><b>protocol</b>
143<dd>Protocol whose connections will get callbacks
144</dl>
145<hr>
Andy Greenbe93fef2011-02-14 20:25:43 +0000146<h2>libwebsocket_set_timeout - marks the wsi as subject to a timeout</h2>
147<i>void</i>
148<b>libwebsocket_set_timeout</b>
149(<i>struct libwebsocket *</i> <b>wsi</b>,
150<i>enum pending_timeout</i> <b>reason</b>,
151<i>int</i> <b>secs</b>)
152<h3>Arguments</h3>
153<dl>
154<dt><b>wsi</b>
155<dd>Websocket connection instance
156<dt><b>reason</b>
157<dd>timeout reason
158<dt><b>secs</b>
159<dd>how many seconds
160</dl>
161<h3>Description</h3>
162<blockquote>
163<p>
164You will not need this unless you are doing something special
165</blockquote>
166<hr>
Andy Greena6cbece2011-01-27 20:06:03 +0000167<h2>libwebsocket_get_socket_fd - returns the socket file descriptor</h2>
168<i>int</i>
169<b>libwebsocket_get_socket_fd</b>
170(<i>struct libwebsocket *</i> <b>wsi</b>)
171<h3>Arguments</h3>
172<dl>
173<dt><b>wsi</b>
174<dd>Websocket connection instance
175</dl>
176<h3>Description</h3>
177<blockquote>
178<p>
179You will not need this unless you are doing something special
180</blockquote>
181<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000182<h2>libwebsocket_rx_flow_control - Enable and disable socket servicing for receieved packets.</h2>
183<i>int</i>
184<b>libwebsocket_rx_flow_control</b>
185(<i>struct libwebsocket *</i> <b>wsi</b>,
186<i>int</i> <b>enable</b>)
187<h3>Arguments</h3>
188<dl>
189<dt><b>wsi</b>
190<dd>Websocket connection instance to get callback for
191<dt><b>enable</b>
192<dd>0 = disable read servicing for this connection, 1 = enable
193</dl>
194<h3>Description</h3>
195<blockquote>
196<p>
197If the output side of a server process becomes choked, this allows flow
198control for the input side.
199</blockquote>
200<hr>
Andy Green2ac5a6f2011-01-28 10:00:18 +0000201<h2>libwebsocket_canonical_hostname - returns this host's hostname</h2>
202<i>const char *</i>
203<b>libwebsocket_canonical_hostname</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000204(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Green2ac5a6f2011-01-28 10:00:18 +0000205<h3>Arguments</h3>
206<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000207<dt><b>context</b>
Andy Green2ac5a6f2011-01-28 10:00:18 +0000208<dd>Websocket context
209</dl>
210<h3>Description</h3>
211<blockquote>
212<p>
213This is typically used by client code to fill in the host parameter
214when making a client connection. You can only call it after the context
215has been created.
216</blockquote>
217<hr>
Andy Green4739e5c2011-01-22 12:51:57 +0000218<h2>libwebsocket_create_context - Create the websocket handler</h2>
Andy Greene92cd172011-01-19 13:11:55 +0000219<i>struct libwebsocket_context *</i>
Andy Green4739e5c2011-01-22 12:51:57 +0000220<b>libwebsocket_create_context</b>
Andy Green62a12932010-11-03 11:19:23 +0000221(<i>int</i> <b>port</b>,
Peter Hinz56885f32011-03-02 22:03:47 +0000222<i>const char *</i> <b>interf</b>,
Andy Greenb45993c2010-12-18 15:13:50 +0000223<i>struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Greend6e09112011-03-05 16:12:15 +0000224<i>struct libwebsocket_extension *</i> <b>extensions</b>,
Andy Green3faa9c72010-11-08 17:03:03 +0000225<i>const char *</i> <b>ssl_cert_filepath</b>,
226<i>const char *</i> <b>ssl_private_key_filepath</b>,
227<i>int</i> <b>gid</b>,
Andy Green8014b292011-01-30 20:57:25 +0000228<i>int</i> <b>uid</b>,
Alon Levy0291eb32012-10-19 11:21:56 +0200229<i>unsigned int</i> <b>options</b>,
230<i>void *</i> <b>user</b>)
Andy Green62a12932010-11-03 11:19:23 +0000231<h3>Arguments</h3>
232<dl>
233<dt><b>port</b>
Andy Green4739e5c2011-01-22 12:51:57 +0000234<dd>Port to listen on... you can use 0 to suppress listening on
235any port, that's what you want if you are not running a
236websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +0000237<dt><b>interf</b>
Andy Green32375b72011-02-19 08:32:53 +0000238<dd>NULL to bind the listen socket to all interfaces, or the
239interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +0000240<dt><b>protocols</b>
241<dd>Array of structures listing supported protocols and a protocol-
242specific callback for each one. The list is ended with an
243entry that has a NULL callback pointer.
Andy Greenb45993c2010-12-18 15:13:50 +0000244It's not const because we write the owning_server member
Andy Greenc5114822011-03-06 10:29:35 +0000245<dt><b>extensions</b>
246<dd>NULL or array of libwebsocket_extension structs listing the
247extensions this context supports
Andy Green3faa9c72010-11-08 17:03:03 +0000248<dt><b>ssl_cert_filepath</b>
249<dd>If libwebsockets was compiled to use ssl, and you want
250to listen using SSL, set to the filepath to fetch the
251server cert from, otherwise NULL for unencrypted
252<dt><b>ssl_private_key_filepath</b>
253<dd>filepath to private key if wanting SSL mode,
254else ignored
255<dt><b>gid</b>
256<dd>group id to change to after setting listen socket, or -1.
257<dt><b>uid</b>
258<dd>user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +0000259<dt><b>options</b>
260<dd>0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green788c4a82012-10-22 12:29:57 +0100261<dt><b>user</b>
262<dd>optional user pointer that can be recovered via the context
263pointer using libwebsocket_context_user
Andy Green62a12932010-11-03 11:19:23 +0000264</dl>
265<h3>Description</h3>
266<blockquote>
Andy Green47943ae2010-11-12 11:15:49 +0000267This function creates the listening socket and takes care
Andy Green62a12932010-11-03 11:19:23 +0000268of all initialization in one step.
269<p>
Andy Greene92cd172011-01-19 13:11:55 +0000270After initialization, it returns a struct libwebsocket_context * that
271represents this server. After calling, user code needs to take care
272of calling <b>libwebsocket_service</b> with the context pointer to get the
273server's sockets serviced. This can be done in the same process context
274or a forked process, or another thread,
Andy Green47943ae2010-11-12 11:15:49 +0000275<p>
276The protocol callback functions are called for a handful of events
277including http requests coming in, websocket connections becoming
Andy Green62a12932010-11-03 11:19:23 +0000278established, and data arriving; it's also called periodically to allow
279async transmission.
280<p>
Andy Green47943ae2010-11-12 11:15:49 +0000281HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
282at that time websocket protocol has not been negotiated. Other
283protocols after the first one never see any HTTP callack activity.
284<p>
Andy Green62a12932010-11-03 11:19:23 +0000285The server created is a simple http server by default; part of the
286websocket standard is upgrading this http connection to a websocket one.
287<p>
288This allows the same server to provide files like scripts and favicon /
289images or whatever over http and dynamic data over websockets all in
290one place; they're all handled in the user callback.
291</blockquote>
292<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000293<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>
294<i>int</i>
295<b>libwebsockets_fork_service_loop</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000296(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Greene92cd172011-01-19 13:11:55 +0000297<h3>Arguments</h3>
298<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000299<dt><b>context</b>
Andy Greene92cd172011-01-19 13:11:55 +0000300<dd>server context returned by creation function
301</dl>
302<hr>
Andy Greenb45993c2010-12-18 15:13:50 +0000303<h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
304<i>const struct libwebsocket_protocols *</i>
305<b>libwebsockets_get_protocol</b>
306(<i>struct libwebsocket *</i> <b>wsi</b>)
307<h3>Arguments</h3>
308<dl>
309<dt><b>wsi</b>
310<dd>pointer to struct websocket you want to know the protocol of
311</dl>
312<h3>Description</h3>
313<blockquote>
314<p>
315This is useful to get the protocol to broadcast back to from inside
316the callback.
317</blockquote>
318<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000319<h2>libwebsockets_broadcast - Sends a buffer to the callback for all active connections of the given protocol.</h2>
Andy Greenb45993c2010-12-18 15:13:50 +0000320<i>int</i>
321<b>libwebsockets_broadcast</b>
322(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
323<i>unsigned char *</i> <b>buf</b>,
324<i>size_t</i> <b>len</b>)
325<h3>Arguments</h3>
326<dl>
327<dt><b>protocol</b>
328<dd>pointer to the protocol you will broadcast to all members of
329<dt><b>buf</b>
330<dd>buffer containing the data to be broadcase. NOTE: this has to be
331allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
332the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
333case you are calling this function from callback context.
334<dt><b>len</b>
335<dd>length of payload data in buf, starting from buf.
336</dl>
337<h3>Description</h3>
338<blockquote>
339This function allows bulk sending of a packet to every connection using
340the given protocol. It does not send the data directly; instead it calls
341the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
342wants to actually send the data for that connection, the callback itself
343should call <b>libwebsocket_write</b>.
344<p>
345<b>libwebsockets_broadcast</b> can be called from another fork context without
346having to take any care about data visibility between the processes, it'll
347"just work".
348</blockquote>
349<hr>
Andy Green62a12932010-11-03 11:19:23 +0000350<h2>libwebsocket_write - Apply protocol then write data to client</h2>
351<i>int</i>
352<b>libwebsocket_write</b>
353(<i>struct libwebsocket *</i> <b>wsi</b>,
354<i>unsigned char *</i> <b>buf</b>,
355<i>size_t</i> <b>len</b>,
356<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
357<h3>Arguments</h3>
358<dl>
359<dt><b>wsi</b>
360<dd>Websocket instance (available from user callback)
361<dt><b>buf</b>
362<dd>The data to send. For data being sent on a websocket
363connection (ie, not default http), this buffer MUST have
364LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
365and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
366in the buffer after (buf + len). This is so the protocol
367header and trailer data can be added in-situ.
368<dt><b>len</b>
369<dd>Count of the data bytes in the payload starting from buf
370<dt><b>protocol</b>
371<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
372of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
373data on a websockets connection. Remember to allow the extra
374bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
375are used.
376</dl>
377<h3>Description</h3>
378<blockquote>
379This function provides the way to issue data back to the client
380for both http and websocket protocols.
381<p>
382In the case of sending using websocket protocol, be sure to allocate
383valid storage before and after buf as explained above. This scheme
384allows maximum efficiency of sending data and protocol in a single
385packet while not burdening the user code with any protocol knowledge.
386</blockquote>
387<hr>
388<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
389<i>int</i>
390<b>libwebsockets_serve_http_file</b>
391(<i>struct libwebsocket *</i> <b>wsi</b>,
392<i>const char *</i> <b>file</b>,
393<i>const char *</i> <b>content_type</b>)
394<h3>Arguments</h3>
395<dl>
396<dt><b>wsi</b>
397<dd>Websocket instance (available from user callback)
398<dt><b>file</b>
399<dd>The file to issue over http
400<dt><b>content_type</b>
401<dd>The http content type, eg, text/html
402</dl>
403<h3>Description</h3>
404<blockquote>
405This function is intended to be called from the callback in response
406to http requests from the client. It allows the callback to issue
407local files down the http link in a single step.
408</blockquote>
409<hr>
Andy Green38e57bb2011-01-19 12:20:27 +0000410<h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
411<i>size_t</i>
412<b>libwebsockets_remaining_packet_payload</b>
413(<i>struct libwebsocket *</i> <b>wsi</b>)
414<h3>Arguments</h3>
415<dl>
416<dt><b>wsi</b>
417<dd>Websocket instance (available from user callback)
418</dl>
419<h3>Description</h3>
420<blockquote>
421This function is intended to be called from the callback if the
422user code is interested in "complete packets" from the client.
423libwebsockets just passes through payload as it comes and issues a buffer
424additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
425callback handler can use this API to find out if the buffer it has just
426been given is the last piece of a "complete packet" from the client --
427when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
4280.
429<p>
430Many protocols won't care becuse their packets are always small.
431</blockquote>
432<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000433<h2>libwebsocket_client_connect - Connect to another websocket server</h2>
434<i>struct libwebsocket *</i>
435<b>libwebsocket_client_connect</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000436(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green90c7cbc2011-01-27 06:26:52 +0000437<i>const char *</i> <b>address</b>,
438<i>int</i> <b>port</b>,
439<i>int</i> <b>ssl_connection</b>,
440<i>const char *</i> <b>path</b>,
441<i>const char *</i> <b>host</b>,
442<i>const char *</i> <b>origin</b>,
Andy Greenbfb051f2011-02-09 08:49:14 +0000443<i>const char *</i> <b>protocol</b>,
444<i>int</i> <b>ietf_version_or_minus_one</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000445<h3>Arguments</h3>
446<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000447<dt><b>context</b>
Andy Green90c7cbc2011-01-27 06:26:52 +0000448<dd>Websocket context
449<dt><b>address</b>
450<dd>Remote server address, eg, "myserver.com"
451<dt><b>port</b>
452<dd>Port to connect to on the remote server, eg, 80
453<dt><b>ssl_connection</b>
454<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
455signed certs
456<dt><b>path</b>
457<dd>Websocket path on server
458<dt><b>host</b>
459<dd>Hostname on server
460<dt><b>origin</b>
461<dd>Socket origin name
462<dt><b>protocol</b>
463<dd>Comma-separated list of protocols being asked for from
464the server, or just one. The server will pick the one it
465likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000466<dt><b>ietf_version_or_minus_one</b>
467<dd>-1 to ask to connect using the default, latest
468protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000469</dl>
470<h3>Description</h3>
471<blockquote>
472This function creates a connection to a remote server
473</blockquote>
474<hr>
David Brooks2c60d952012-04-20 12:19:01 +0800475<h2>libwebsocket_client_connect_extended - Connect to another websocket server</h2>
476<i>struct libwebsocket *</i>
477<b>libwebsocket_client_connect_extended</b>
478(<i>struct libwebsocket_context *</i> <b>context</b>,
479<i>const char *</i> <b>address</b>,
480<i>int</i> <b>port</b>,
481<i>int</i> <b>ssl_connection</b>,
482<i>const char *</i> <b>path</b>,
483<i>const char *</i> <b>host</b>,
484<i>const char *</i> <b>origin</b>,
485<i>const char *</i> <b>protocol</b>,
486<i>int</i> <b>ietf_version_or_minus_one</b>,
487<i>void *</i> <b>userdata</b>)
488<h3>Arguments</h3>
489<dl>
490<dt><b>context</b>
491<dd>Websocket context
492<dt><b>address</b>
493<dd>Remote server address, eg, "myserver.com"
494<dt><b>port</b>
495<dd>Port to connect to on the remote server, eg, 80
496<dt><b>ssl_connection</b>
497<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
498signed certs
499<dt><b>path</b>
500<dd>Websocket path on server
501<dt><b>host</b>
502<dd>Hostname on server
503<dt><b>origin</b>
504<dd>Socket origin name
505<dt><b>protocol</b>
506<dd>Comma-separated list of protocols being asked for from
507the server, or just one. The server will pick the one it
508likes best.
509<dt><b>ietf_version_or_minus_one</b>
510<dd>-1 to ask to connect using the default, latest
511protocol supported, or the specific protocol ordinal
512<dt><b>userdata</b>
513<dd>Pre-allocated user data
514</dl>
515<h3>Description</h3>
516<blockquote>
517This function creates a connection to a remote server
518</blockquote>
519<hr>
Andy Green8f037e42010-12-19 22:13:26 +0000520<h2>callback - User server actions</h2>
Andy Green07b56e62011-10-03 19:30:22 +0800521<i>LWS_EXTERN int</i>
Andy Green8f037e42010-12-19 22:13:26 +0000522<b>callback</b>
Darin Willitsc19456f2011-02-14 17:52:39 +0000523(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green62c54d22011-02-14 09:14:25 +0000524<i>struct libwebsocket *</i> <b>wsi</b>,
Andy Green8f037e42010-12-19 22:13:26 +0000525<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
526<i>void *</i> <b>user</b>,
527<i>void *</i> <b>in</b>,
528<i>size_t</i> <b>len</b>)
529<h3>Arguments</h3>
530<dl>
Andy Green32375b72011-02-19 08:32:53 +0000531<dt><b>context</b>
532<dd>Websockets context
Andy Green8f037e42010-12-19 22:13:26 +0000533<dt><b>wsi</b>
534<dd>Opaque websocket instance pointer
535<dt><b>reason</b>
536<dd>The reason for the call
537<dt><b>user</b>
538<dd>Pointer to per-session user data allocated by library
539<dt><b>in</b>
540<dd>Pointer used for some callback reasons
541<dt><b>len</b>
542<dd>Length set for some callback reasons
543</dl>
544<h3>Description</h3>
545<blockquote>
546This callback is the way the user controls what is served. All the
547protocol detail is hidden and handled by the library.
548<p>
549For each connection / session there is user data allocated that is
550pointed to by "user". You set the size of this user data area when
551the library is initialized with libwebsocket_create_server.
552<p>
553You get an opportunity to initialize user data when called back with
554LWS_CALLBACK_ESTABLISHED reason.
555</blockquote>
556<h3>LWS_CALLBACK_ESTABLISHED</h3>
557<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000558after the server completes a handshake with
559an incoming client
560</blockquote>
David Brooks2c60d952012-04-20 12:19:01 +0800561<h3>LWS_CALLBACK_CLIENT_CONNECTION_ERROR</h3>
562<blockquote>
563the request client connection has
564been unable to complete a handshake with the remote server
565</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000566<h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
567<blockquote>
568after your client connection completed
569a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000570</blockquote>
571<h3>LWS_CALLBACK_CLOSED</h3>
572<blockquote>
573when the websocket session ends
574</blockquote>
575<h3>LWS_CALLBACK_BROADCAST</h3>
576<blockquote>
577signal to send to client (you would use
578<b>libwebsocket_write</b> taking care about the
579special buffer requirements
580</blockquote>
581<h3>LWS_CALLBACK_RECEIVE</h3>
582<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000583data has appeared for this server endpoint from a
584remote client, it can be found at *in and is
585len bytes long
586</blockquote>
Andy Greena6cbece2011-01-27 20:06:03 +0000587<h3>LWS_CALLBACK_CLIENT_RECEIVE_PONG</h3>
588<blockquote>
589if you elected to see PONG packets,
590they appear with this callback reason. PONG
591packets only exist in 04+ protocol
592</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000593<h3>LWS_CALLBACK_CLIENT_RECEIVE</h3>
594<blockquote>
595data has appeared from the server for the
596client connection, it can be found at *in and
597is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000598</blockquote>
599<h3>LWS_CALLBACK_HTTP</h3>
600<blockquote>
601an http request has come from a client that is not
602asking to upgrade the connection to a websocket
603one. This is a chance to serve http content,
604for example, to send a script to the client
605which will then open the websockets connection.
Andy Green7619c472011-01-23 17:47:08 +0000606<tt><b>in</b></tt> points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000607<b>libwebsockets_serve_http_file</b> makes it very
608simple to send back a file to the client.
609</blockquote>
Andy Greene9739ed2011-03-07 21:40:59 +0000610<h3>LWS_CALLBACK_SERVER_WRITEABLE</h3>
Andy Green90c7cbc2011-01-27 06:26:52 +0000611<blockquote>
Andy Greene9739ed2011-03-07 21:40:59 +0000612If you call
Andy Green90c7cbc2011-01-27 06:26:52 +0000613<b>libwebsocket_callback_on_writable</b> on a connection, you will
Andy Greene9739ed2011-03-07 21:40:59 +0000614get one of these callbacks coming when the connection socket
615is able to accept another write packet without blocking.
616If it already was able to take another packet without blocking,
617you'll get this callback at the next call to the service loop
618function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
619and servers get LWS_CALLBACK_SERVER_WRITEABLE.
Andy Green90c7cbc2011-01-27 06:26:52 +0000620</blockquote>
Andy Green07034092011-02-13 08:37:12 +0000621<h3>LWS_CALLBACK_FILTER_NETWORK_CONNECTION</h3>
622<blockquote>
623called when a client connects to
624the server at network level; the connection is accepted but then
625passed to this callback to decide whether to hang up immediately
626or not, based on the client IP. <tt><b>user</b></tt> contains the connection
627socket's descriptor. Return non-zero to terminate
628the connection before sending or receiving anything.
629Because this happens immediately after the network connection
630from the client, there's no websocket protocol selected yet so
631this callback is issued only to protocol 0.
632</blockquote>
Andy Greenc85619d2011-02-13 08:25:26 +0000633<h3>LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION</h3>
634<blockquote>
635called when the handshake has
636been received and parsed from the client, but the response is
637not sent yet. Return non-zero to disallow the connection.
Andy Green07034092011-02-13 08:37:12 +0000638<tt><b>user</b></tt> is a pointer to an array of struct lws_tokens, you can
639use the header enums lws_token_indexes from libwebsockets.h
640to check for and read the supported header presence and
641content before deciding to allow the handshake to proceed or
642to kill the connection.
Andy Green0894bda2011-02-19 09:09:11 +0000643</blockquote>
644<h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS</h3>
645<blockquote>
Andy Green6901cb32011-02-21 08:06:47 +0000646if configured for
Andy Green0894bda2011-02-19 09:09:11 +0000647including OpenSSL support, this callback allows your user code
648to perform extra <b>SSL_CTX_load_verify_locations</b> or similar
649calls to direct OpenSSL where to find certificates the client
650can use to confirm the remote server identity. <tt><b>user</b></tt> is the
651OpenSSL SSL_CTX*
Andy Green6901cb32011-02-21 08:06:47 +0000652</blockquote>
653<h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS</h3>
654<blockquote>
655if configured for
656including OpenSSL support, this callback allows your user code
657to load extra certifcates into the server which allow it to
658verify the validity of certificates returned by clients. <tt><b>user</b></tt>
659is the server's OpenSSL SSL_CTX*
660</blockquote>
661<h3>LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION</h3>
662<blockquote>
663if the
664libwebsockets context was created with the option
665LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
666callback is generated during OpenSSL verification of the cert
667sent from the client. It is sent to protocol[0] callback as
668no protocol has been negotiated on the connection yet.
669Notice that the libwebsockets context and wsi are both NULL
670during this callback. See
671</blockquote>
672<h3>http</h3>
673<blockquote>
674//www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
675to understand more detail about the OpenSSL callback that
676generates this libwebsockets callback and the meanings of the
677arguments passed. In this callback, <tt><b>user</b></tt> is the x509_ctx,
678<tt><b>in</b></tt> is the ssl pointer and <tt><b>len</b></tt> is preverify_ok
679Notice that this callback maintains libwebsocket return
680conventions, return 0 to mean the cert is OK or 1 to fail it.
681This also means that if you don't handle this callback then
682the default callback action of returning 0 allows the client
683certificates.
Andy Green385e7ad2011-03-01 21:06:02 +0000684</blockquote>
685<h3>LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER</h3>
686<blockquote>
687this callback happens
688when a client handshake is being compiled. <tt><b>user</b></tt> is NULL,
689<tt><b>in</b></tt> is a char **, it's pointing to a char * which holds the
690next location in the header buffer where you can add
691headers, and <tt><b>len</b></tt> is the remaining space in the header buffer,
692which is typically some hundreds of bytes. So, to add a canned
693cookie, your handler code might look similar to:
694<p>
695char **p = (char **)in;
696<p>
697if (len &lt; 100)
698return 1;
699<p>
700*p += sprintf(*p, "Cookie: a=b\x0d\x0a");
701<p>
702return 0;
703<p>
704Notice if you add anything, you just have to take care about
705the CRLF on the line you added. Obviously this callback is
706optional, if you don't handle it everything is fine.
707<p>
708Notice the callback is coming to protocols[0] all the time,
709because there is no specific protocol handshook yet.
Andy Greenc5114822011-03-06 10:29:35 +0000710</blockquote>
711<h3>LWS_CALLBACK_CONFIRM_EXTENSION_OKAY</h3>
712<blockquote>
713When the server handshake code
714sees that it does support a requested extension, before
715accepting the extension by additing to the list sent back to
716the client it gives this callback just to check that it's okay
717to use that extension. It calls back to the requested protocol
718and with <tt><b>in</b></tt> being the extension name, <tt><b>len</b></tt> is 0 and <tt><b>user</b></tt> is
719valid. Note though at this time the ESTABLISHED callback hasn't
720happened yet so if you initialize <tt><b>user</b></tt> content there, <tt><b>user</b></tt>
721content during this callback might not be useful for anything.
722Notice this callback comes to protocols[0].
Andy Greenc6517fa2011-03-06 13:15:29 +0000723</blockquote>
724<h3>LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED</h3>
725<blockquote>
726When a client
727connection is being prepared to start a handshake to a server,
728each supported extension is checked with protocols[0] callback
729with this reason, giving the user code a chance to suppress the
730claim to support that extension by returning non-zero. If
731unhandled, by default 0 will be returned and the extension
732support included in the header to the server. Notice this
733callback comes to protocols[0].
Andy Greenc85619d2011-02-13 08:25:26 +0000734<p>
735The next four reasons are optional and only need taking care of if you
736will be integrating libwebsockets sockets into an external polling
737array.
738</blockquote>
739<h3>LWS_CALLBACK_ADD_POLL_FD</h3>
740<blockquote>
741libwebsocket deals with its <b>poll</b> loop
742internally, but in the case you are integrating with another
743server you will need to have libwebsocket sockets share a
744polling array with the other server. This and the other
745POLL_FD related callbacks let you put your specialized
746poll array interface code in the callback for protocol 0, the
747first protocol you support, usually the HTTP protocol in the
748serving case. This callback happens when a socket needs to be
749</blockquote>
750<h3>added to the polling loop</h3>
751<blockquote>
752<tt><b>user</b></tt> contains the fd, and
753<tt><b>len</b></tt> is the events bitmap (like, POLLIN). If you are using the
754internal polling loop (the "service" callback), you can just
755ignore these callbacks.
756</blockquote>
757<h3>LWS_CALLBACK_DEL_POLL_FD</h3>
758<blockquote>
759This callback happens when a socket descriptor
760needs to be removed from an external polling array. <tt><b>user</b></tt> is
761the socket desricptor. If you are using the internal polling
762loop, you can just ignore it.
763</blockquote>
764<h3>LWS_CALLBACK_SET_MODE_POLL_FD</h3>
765<blockquote>
766This callback happens when libwebsockets
767wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
768The handler should OR <tt><b>len</b></tt> on to the events member of the pollfd
769struct for this socket descriptor. If you are using the
770internal polling loop, you can just ignore it.
771</blockquote>
772<h3>LWS_CALLBACK_CLEAR_MODE_POLL_FD</h3>
773<blockquote>
774This callback occurs when libwebsockets
775wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
776The handler should AND ~<tt><b>len</b></tt> on to the events member of the
777pollfd struct for this socket descriptor. If you are using the
778internal polling loop, you can just ignore it.
779</blockquote>
Andy Green8f037e42010-12-19 22:13:26 +0000780<hr>
Andy Green57b4e9a2011-03-06 13:14:46 +0000781<h2>extension_callback - Hooks to allow extensions to operate</h2>
Andy Green07b56e62011-10-03 19:30:22 +0800782<i>LWS_EXTERN int</i>
Andy Green57b4e9a2011-03-06 13:14:46 +0000783<b>extension_callback</b>
784(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green46c2ea02011-03-22 09:04:01 +0000785<i>struct libwebsocket_extension *</i> <b>ext</b>,
Andy Green57b4e9a2011-03-06 13:14:46 +0000786<i>struct libwebsocket *</i> <b>wsi</b>,
David Brooks2c60d952012-04-20 12:19:01 +0800787<i>enum libwebsocket_extension_callback_reasons</i> <b>reason</b>,
Andy Green57b4e9a2011-03-06 13:14:46 +0000788<i>void *</i> <b>user</b>,
789<i>void *</i> <b>in</b>,
790<i>size_t</i> <b>len</b>)
791<h3>Arguments</h3>
792<dl>
793<dt><b>context</b>
794<dd>Websockets context
Andy Green46c2ea02011-03-22 09:04:01 +0000795<dt><b>ext</b>
796<dd>This extension
Andy Green57b4e9a2011-03-06 13:14:46 +0000797<dt><b>wsi</b>
798<dd>Opaque websocket instance pointer
799<dt><b>reason</b>
800<dd>The reason for the call
801<dt><b>user</b>
802<dd>Pointer to per-session user data allocated by library
803<dt><b>in</b>
804<dd>Pointer used for some callback reasons
805<dt><b>len</b>
806<dd>Length set for some callback reasons
807</dl>
808<h3>Description</h3>
809<blockquote>
810Each extension that is active on a particular connection receives
811callbacks during the connection lifetime to allow the extension to
812operate on websocket data and manage itself.
813<p>
814Libwebsockets takes care of allocating and freeing "user" memory for
815each active extension on each connection. That is what is pointed to
816by the <tt><b>user</b></tt> parameter.
817</blockquote>
818<h3>LWS_EXT_CALLBACK_CONSTRUCT</h3>
819<blockquote>
820called when the server has decided to
821select this extension from the list provided by the client,
822just before the server will send back the handshake accepting
823the connection with this extension active. This gives the
824extension a chance to initialize its connection context found
825in <tt><b>user</b></tt>.
826</blockquote>
Andy Green2366b1c2011-03-06 13:15:31 +0000827<h3>LWS_EXT_CALLBACK_CLIENT_CONSTRUCT</h3>
828<blockquote>
829same as LWS_EXT_CALLBACK_CONSTRUCT
830but called when client is instantiating this extension. Some
831extensions will work the same on client and server side and then
832you can just merge handlers for both CONSTRUCTS.
833</blockquote>
Andy Green57b4e9a2011-03-06 13:14:46 +0000834<h3>LWS_EXT_CALLBACK_DESTROY</h3>
835<blockquote>
836called when the connection the extension was
837being used on is about to be closed and deallocated. It's the
838last chance for the extension to deallocate anything it has
839allocated in the user data (pointed to by <tt><b>user</b></tt>) before the
Andy Green2366b1c2011-03-06 13:15:31 +0000840user data is deleted. This same callback is used whether you
841are in client or server instantiation context.
Andy Green57b4e9a2011-03-06 13:14:46 +0000842</blockquote>
843<h3>LWS_EXT_CALLBACK_PACKET_RX_PREPARSE</h3>
844<blockquote>
845when this extension was active on
846a connection, and a packet of data arrived at the connection,
847it is passed to this callback to give the extension a chance to
848change the data, eg, decompress it. <tt><b>user</b></tt> is pointing to the
849extension's private connection context data, <tt><b>in</b></tt> is pointing
850to an lws_tokens struct, it consists of a char * pointer called
851token, and an int called token_len. At entry, these are
852set to point to the received buffer and set to the content
853length. If the extension will grow the content, it should use
854a new buffer allocated in its private user context data and
855set the pointed-to lws_tokens members to point to its buffer.
856</blockquote>
857<h3>LWS_EXT_CALLBACK_PACKET_TX_PRESEND</h3>
858<blockquote>
859this works the same way as
860LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
861extension a chance to change websocket data just before it will
862be sent out. Using the same lws_token pointer scheme in <tt><b>in</b></tt>,
863the extension can change the buffer and the length to be
864transmitted how it likes. Again if it wants to grow the
865buffer safely, it should copy the data into its own buffer and
866set the lws_tokens token pointer to it.
867</blockquote>
868<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000869<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
870<b>struct libwebsocket_protocols</b> {<br>
871&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
David Brooks2c60d952012-04-20 12:19:01 +0800872&nbsp; &nbsp; <i>callback_function *</i> <b>callback</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000873&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenb45993c2010-12-18 15:13:50 +0000874&nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
875&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
876&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
877&nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000878};<br>
879<h3>Members</h3>
880<dl>
881<dt><b>name</b>
882<dd>Protocol name that must match the one given in the client
883Javascript new WebSocket(url, 'protocol') name
884<dt><b>callback</b>
885<dd>The service callback used for this protocol. It allows the
886service action for an entire protocol to be encapsulated in
887the protocol-specific callback
888<dt><b>per_session_data_size</b>
889<dd>Each new connection using this protocol gets
890this much memory allocated on connection establishment and
891freed on connection takedown. A pointer to this per-connection
892allocation is passed into the callback in the 'user' parameter
Andy Greenb45993c2010-12-18 15:13:50 +0000893<dt><b>owning_server</b>
894<dd>the server init call fills in this opaque pointer when
895registering this protocol with the server.
896<dt><b>broadcast_socket_port</b>
897<dd>the server init call fills this in with the
898localhost port number used to forward broadcasts for this
899protocol
900<dt><b>broadcast_socket_user_fd</b>
901<dd>the server init call fills this in ... the <b>main</b>
902process context can write to this socket to perform broadcasts
903(use the <b>libwebsockets_broadcast</b> api to do this instead,
904it works from any process context)
905<dt><b>protocol_index</b>
906<dd>which protocol we are starting from zero
Andy Green4f3943a2010-11-12 10:44:16 +0000907</dl>
908<h3>Description</h3>
909<blockquote>
910This structure represents one protocol supported by the server. An
911array of these structures is passed to <b>libwebsocket_create_server</b>
912allows as many protocols as you like to be handled by one server.
913</blockquote>
914<hr>
Andy Greend6e09112011-03-05 16:12:15 +0000915<h2>struct libwebsocket_extension - An extension we know how to cope with</h2>
916<b>struct libwebsocket_extension</b> {<br>
917&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
David Brooks2c60d952012-04-20 12:19:01 +0800918&nbsp; &nbsp; <i>extension_callback_function *</i> <b>callback</b>;<br>
Andy Greend6e09112011-03-05 16:12:15 +0000919&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenaa6fc442012-04-12 13:26:49 +0800920&nbsp; &nbsp; <i>void *</i> <b>per_context_private_data</b>;<br>
Andy Greend6e09112011-03-05 16:12:15 +0000921};<br>
922<h3>Members</h3>
923<dl>
924<dt><b>name</b>
925<dd>Formal extension name, eg, "deflate-stream"
926<dt><b>callback</b>
927<dd>Service callback
928<dt><b>per_session_data_size</b>
929<dd>Libwebsockets will auto-malloc this much
930memory for the use of the extension, a pointer
931to it comes in the <tt><b>user</b></tt> callback parameter
Andy Greenaa6fc442012-04-12 13:26:49 +0800932<dt><b>per_context_private_data</b>
933<dd>Optional storage for this externsion that
934is per-context, so it can track stuff across
935all sessions, etc, if it wants
Andy Greend6e09112011-03-05 16:12:15 +0000936</dl>
937<hr>