blob: ecb17ca7c402ddf39219c52da4ed749bdea65138 [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>
4(<i>struct libwebsocket_context *</i> <b>this</b>,
5<i>int</i> <b>fd</b>)
6<h3>Arguments</h3>
7<dl>
8<dt><b>this</b>
9<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>
46(<i>struct libwebsocket_context *</i> <b>this</b>,
47<i>struct pollfd *</i> <b>pollfd</b>)
48<h3>Arguments</h3>
49<dl>
50<dt><b>this</b>
51<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>
66(<i>struct libwebsocket_context *</i> <b>this</b>)
67<h3>Arguments</h3>
68<dl>
69<dt><b>this</b>
70<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>
82(<i>struct libwebsocket_context *</i> <b>this</b>,
83<i>int</i> <b>timeout_ms</b>)
84<h3>Arguments</h3>
85<dl>
86<dt><b>this</b>
87<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>
Andy Green62c54d22011-02-14 09:14:25 +0000126(<i>struct libwebsocket_context *</i> <b>this</b>,
127<i>struct libwebsocket *</i> <b>wsi</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000128<h3>Arguments</h3>
129<dl>
Andy Green32375b72011-02-19 08:32:53 +0000130<dt><b>this</b>
131<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>
204(<i>struct libwebsocket_context *</i> <b>this</b>)
205<h3>Arguments</h3>
206<dl>
207<dt><b>this</b>
208<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>,
Andy Green32375b72011-02-19 08:32:53 +0000222<i>const char *</i> <b>interface</b>,
Andy Greenb45993c2010-12-18 15:13:50 +0000223<i>struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Green3faa9c72010-11-08 17:03:03 +0000224<i>const char *</i> <b>ssl_cert_filepath</b>,
225<i>const char *</i> <b>ssl_private_key_filepath</b>,
226<i>int</i> <b>gid</b>,
Andy Green8014b292011-01-30 20:57:25 +0000227<i>int</i> <b>uid</b>,
228<i>unsigned int</i> <b>options</b>)
Andy Green62a12932010-11-03 11:19:23 +0000229<h3>Arguments</h3>
230<dl>
231<dt><b>port</b>
Andy Green4739e5c2011-01-22 12:51:57 +0000232<dd>Port to listen on... you can use 0 to suppress listening on
233any port, that's what you want if you are not running a
234websocket server at all but just using it as a client
Andy Green32375b72011-02-19 08:32:53 +0000235<dt><b>interface</b>
236<dd>NULL to bind the listen socket to all interfaces, or the
237interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +0000238<dt><b>protocols</b>
239<dd>Array of structures listing supported protocols and a protocol-
240specific callback for each one. The list is ended with an
241entry that has a NULL callback pointer.
Andy Greenb45993c2010-12-18 15:13:50 +0000242It's not const because we write the owning_server member
Andy Green3faa9c72010-11-08 17:03:03 +0000243<dt><b>ssl_cert_filepath</b>
244<dd>If libwebsockets was compiled to use ssl, and you want
245to listen using SSL, set to the filepath to fetch the
246server cert from, otherwise NULL for unencrypted
247<dt><b>ssl_private_key_filepath</b>
248<dd>filepath to private key if wanting SSL mode,
249else ignored
250<dt><b>gid</b>
251<dd>group id to change to after setting listen socket, or -1.
252<dt><b>uid</b>
253<dd>user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +0000254<dt><b>options</b>
255<dd>0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green62a12932010-11-03 11:19:23 +0000256</dl>
257<h3>Description</h3>
258<blockquote>
Andy Green47943ae2010-11-12 11:15:49 +0000259This function creates the listening socket and takes care
Andy Green62a12932010-11-03 11:19:23 +0000260of all initialization in one step.
261<p>
Andy Greene92cd172011-01-19 13:11:55 +0000262After initialization, it returns a struct libwebsocket_context * that
263represents this server. After calling, user code needs to take care
264of calling <b>libwebsocket_service</b> with the context pointer to get the
265server's sockets serviced. This can be done in the same process context
266or a forked process, or another thread,
Andy Green47943ae2010-11-12 11:15:49 +0000267<p>
268The protocol callback functions are called for a handful of events
269including http requests coming in, websocket connections becoming
Andy Green62a12932010-11-03 11:19:23 +0000270established, and data arriving; it's also called periodically to allow
271async transmission.
272<p>
Andy Green47943ae2010-11-12 11:15:49 +0000273HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
274at that time websocket protocol has not been negotiated. Other
275protocols after the first one never see any HTTP callack activity.
276<p>
Andy Green62a12932010-11-03 11:19:23 +0000277The server created is a simple http server by default; part of the
278websocket standard is upgrading this http connection to a websocket one.
279<p>
280This allows the same server to provide files like scripts and favicon /
281images or whatever over http and dynamic data over websockets all in
282one place; they're all handled in the user callback.
283</blockquote>
284<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000285<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>
286<i>int</i>
287<b>libwebsockets_fork_service_loop</b>
288(<i>struct libwebsocket_context *</i> <b>this</b>)
289<h3>Arguments</h3>
290<dl>
291<dt><b>this</b>
292<dd>server context returned by creation function
293</dl>
294<hr>
Andy Greenb45993c2010-12-18 15:13:50 +0000295<h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
296<i>const struct libwebsocket_protocols *</i>
297<b>libwebsockets_get_protocol</b>
298(<i>struct libwebsocket *</i> <b>wsi</b>)
299<h3>Arguments</h3>
300<dl>
301<dt><b>wsi</b>
302<dd>pointer to struct websocket you want to know the protocol of
303</dl>
304<h3>Description</h3>
305<blockquote>
306<p>
307This is useful to get the protocol to broadcast back to from inside
308the callback.
309</blockquote>
310<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000311<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 +0000312<i>int</i>
313<b>libwebsockets_broadcast</b>
314(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
315<i>unsigned char *</i> <b>buf</b>,
316<i>size_t</i> <b>len</b>)
317<h3>Arguments</h3>
318<dl>
319<dt><b>protocol</b>
320<dd>pointer to the protocol you will broadcast to all members of
321<dt><b>buf</b>
322<dd>buffer containing the data to be broadcase. NOTE: this has to be
323allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
324the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
325case you are calling this function from callback context.
326<dt><b>len</b>
327<dd>length of payload data in buf, starting from buf.
328</dl>
329<h3>Description</h3>
330<blockquote>
331This function allows bulk sending of a packet to every connection using
332the given protocol. It does not send the data directly; instead it calls
333the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
334wants to actually send the data for that connection, the callback itself
335should call <b>libwebsocket_write</b>.
336<p>
337<b>libwebsockets_broadcast</b> can be called from another fork context without
338having to take any care about data visibility between the processes, it'll
339"just work".
340</blockquote>
341<hr>
Andy Green62a12932010-11-03 11:19:23 +0000342<h2>libwebsocket_write - Apply protocol then write data to client</h2>
343<i>int</i>
344<b>libwebsocket_write</b>
345(<i>struct libwebsocket *</i> <b>wsi</b>,
346<i>unsigned char *</i> <b>buf</b>,
347<i>size_t</i> <b>len</b>,
348<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
349<h3>Arguments</h3>
350<dl>
351<dt><b>wsi</b>
352<dd>Websocket instance (available from user callback)
353<dt><b>buf</b>
354<dd>The data to send. For data being sent on a websocket
355connection (ie, not default http), this buffer MUST have
356LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
357and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
358in the buffer after (buf + len). This is so the protocol
359header and trailer data can be added in-situ.
360<dt><b>len</b>
361<dd>Count of the data bytes in the payload starting from buf
362<dt><b>protocol</b>
363<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
364of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
365data on a websockets connection. Remember to allow the extra
366bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
367are used.
368</dl>
369<h3>Description</h3>
370<blockquote>
371This function provides the way to issue data back to the client
372for both http and websocket protocols.
373<p>
374In the case of sending using websocket protocol, be sure to allocate
375valid storage before and after buf as explained above. This scheme
376allows maximum efficiency of sending data and protocol in a single
377packet while not burdening the user code with any protocol knowledge.
378</blockquote>
379<hr>
380<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
381<i>int</i>
382<b>libwebsockets_serve_http_file</b>
383(<i>struct libwebsocket *</i> <b>wsi</b>,
384<i>const char *</i> <b>file</b>,
385<i>const char *</i> <b>content_type</b>)
386<h3>Arguments</h3>
387<dl>
388<dt><b>wsi</b>
389<dd>Websocket instance (available from user callback)
390<dt><b>file</b>
391<dd>The file to issue over http
392<dt><b>content_type</b>
393<dd>The http content type, eg, text/html
394</dl>
395<h3>Description</h3>
396<blockquote>
397This function is intended to be called from the callback in response
398to http requests from the client. It allows the callback to issue
399local files down the http link in a single step.
400</blockquote>
401<hr>
Andy Green38e57bb2011-01-19 12:20:27 +0000402<h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
403<i>size_t</i>
404<b>libwebsockets_remaining_packet_payload</b>
405(<i>struct libwebsocket *</i> <b>wsi</b>)
406<h3>Arguments</h3>
407<dl>
408<dt><b>wsi</b>
409<dd>Websocket instance (available from user callback)
410</dl>
411<h3>Description</h3>
412<blockquote>
413This function is intended to be called from the callback if the
414user code is interested in "complete packets" from the client.
415libwebsockets just passes through payload as it comes and issues a buffer
416additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
417callback handler can use this API to find out if the buffer it has just
418been given is the last piece of a "complete packet" from the client --
419when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
4200.
421<p>
422Many protocols won't care becuse their packets are always small.
423</blockquote>
424<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000425<h2>libwebsocket_client_connect - Connect to another websocket server</h2>
426<i>struct libwebsocket *</i>
427<b>libwebsocket_client_connect</b>
428(<i>struct libwebsocket_context *</i> <b>this</b>,
429<i>const char *</i> <b>address</b>,
430<i>int</i> <b>port</b>,
431<i>int</i> <b>ssl_connection</b>,
432<i>const char *</i> <b>path</b>,
433<i>const char *</i> <b>host</b>,
434<i>const char *</i> <b>origin</b>,
Andy Greenbfb051f2011-02-09 08:49:14 +0000435<i>const char *</i> <b>protocol</b>,
436<i>int</i> <b>ietf_version_or_minus_one</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000437<h3>Arguments</h3>
438<dl>
439<dt><b>this</b>
440<dd>Websocket context
441<dt><b>address</b>
442<dd>Remote server address, eg, "myserver.com"
443<dt><b>port</b>
444<dd>Port to connect to on the remote server, eg, 80
445<dt><b>ssl_connection</b>
446<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
447signed certs
448<dt><b>path</b>
449<dd>Websocket path on server
450<dt><b>host</b>
451<dd>Hostname on server
452<dt><b>origin</b>
453<dd>Socket origin name
454<dt><b>protocol</b>
455<dd>Comma-separated list of protocols being asked for from
456the server, or just one. The server will pick the one it
457likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000458<dt><b>ietf_version_or_minus_one</b>
459<dd>-1 to ask to connect using the default, latest
460protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000461</dl>
462<h3>Description</h3>
463<blockquote>
464This function creates a connection to a remote server
465</blockquote>
466<hr>
Andy Green8f037e42010-12-19 22:13:26 +0000467<h2>callback - User server actions</h2>
468<i>int</i>
469<b>callback</b>
Darin Willitsc19456f2011-02-14 17:52:39 +0000470(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green62c54d22011-02-14 09:14:25 +0000471<i>struct libwebsocket *</i> <b>wsi</b>,
Andy Green8f037e42010-12-19 22:13:26 +0000472<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
473<i>void *</i> <b>user</b>,
474<i>void *</i> <b>in</b>,
475<i>size_t</i> <b>len</b>)
476<h3>Arguments</h3>
477<dl>
Andy Green32375b72011-02-19 08:32:53 +0000478<dt><b>context</b>
479<dd>Websockets context
Andy Green8f037e42010-12-19 22:13:26 +0000480<dt><b>wsi</b>
481<dd>Opaque websocket instance pointer
482<dt><b>reason</b>
483<dd>The reason for the call
484<dt><b>user</b>
485<dd>Pointer to per-session user data allocated by library
486<dt><b>in</b>
487<dd>Pointer used for some callback reasons
488<dt><b>len</b>
489<dd>Length set for some callback reasons
490</dl>
491<h3>Description</h3>
492<blockquote>
493This callback is the way the user controls what is served. All the
494protocol detail is hidden and handled by the library.
495<p>
496For each connection / session there is user data allocated that is
497pointed to by "user". You set the size of this user data area when
498the library is initialized with libwebsocket_create_server.
499<p>
500You get an opportunity to initialize user data when called back with
501LWS_CALLBACK_ESTABLISHED reason.
502</blockquote>
503<h3>LWS_CALLBACK_ESTABLISHED</h3>
504<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000505after the server completes a handshake with
506an incoming client
507</blockquote>
508<h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
509<blockquote>
510after your client connection completed
511a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000512</blockquote>
513<h3>LWS_CALLBACK_CLOSED</h3>
514<blockquote>
515when the websocket session ends
516</blockquote>
517<h3>LWS_CALLBACK_BROADCAST</h3>
518<blockquote>
519signal to send to client (you would use
520<b>libwebsocket_write</b> taking care about the
521special buffer requirements
522</blockquote>
523<h3>LWS_CALLBACK_RECEIVE</h3>
524<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000525data has appeared for this server endpoint from a
526remote client, it can be found at *in and is
527len bytes long
528</blockquote>
Andy Greena6cbece2011-01-27 20:06:03 +0000529<h3>LWS_CALLBACK_CLIENT_RECEIVE_PONG</h3>
530<blockquote>
531if you elected to see PONG packets,
532they appear with this callback reason. PONG
533packets only exist in 04+ protocol
534</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000535<h3>LWS_CALLBACK_CLIENT_RECEIVE</h3>
536<blockquote>
537data has appeared from the server for the
538client connection, it can be found at *in and
539is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000540</blockquote>
541<h3>LWS_CALLBACK_HTTP</h3>
542<blockquote>
543an http request has come from a client that is not
544asking to upgrade the connection to a websocket
545one. This is a chance to serve http content,
546for example, to send a script to the client
547which will then open the websockets connection.
Andy Green7619c472011-01-23 17:47:08 +0000548<tt><b>in</b></tt> points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000549<b>libwebsockets_serve_http_file</b> makes it very
550simple to send back a file to the client.
551</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000552<h3>LWS_CALLBACK_CLIENT_WRITEABLE</h3>
553<blockquote>
554if you call
555<b>libwebsocket_callback_on_writable</b> on a connection, you will
556get this callback coming when the connection socket is able to
557accept another write packet without blocking. If it already
558was able to take another packet without blocking, you'll get
559this callback at the next call to the service loop function.
560</blockquote>
Andy Green07034092011-02-13 08:37:12 +0000561<h3>LWS_CALLBACK_FILTER_NETWORK_CONNECTION</h3>
562<blockquote>
563called when a client connects to
564the server at network level; the connection is accepted but then
565passed to this callback to decide whether to hang up immediately
566or not, based on the client IP. <tt><b>user</b></tt> contains the connection
567socket's descriptor. Return non-zero to terminate
568the connection before sending or receiving anything.
569Because this happens immediately after the network connection
570from the client, there's no websocket protocol selected yet so
571this callback is issued only to protocol 0.
572</blockquote>
Andy Greenc85619d2011-02-13 08:25:26 +0000573<h3>LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION</h3>
574<blockquote>
575called when the handshake has
576been received and parsed from the client, but the response is
577not sent yet. Return non-zero to disallow the connection.
Andy Green07034092011-02-13 08:37:12 +0000578<tt><b>user</b></tt> is a pointer to an array of struct lws_tokens, you can
579use the header enums lws_token_indexes from libwebsockets.h
580to check for and read the supported header presence and
581content before deciding to allow the handshake to proceed or
582to kill the connection.
Andy Green0894bda2011-02-19 09:09:11 +0000583</blockquote>
584<h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS</h3>
585<blockquote>
586if configure for
587including OpenSSL support, this callback allows your user code
588to perform extra <b>SSL_CTX_load_verify_locations</b> or similar
589calls to direct OpenSSL where to find certificates the client
590can use to confirm the remote server identity. <tt><b>user</b></tt> is the
591OpenSSL SSL_CTX*
Andy Greenc85619d2011-02-13 08:25:26 +0000592<p>
593The next four reasons are optional and only need taking care of if you
594will be integrating libwebsockets sockets into an external polling
595array.
596</blockquote>
597<h3>LWS_CALLBACK_ADD_POLL_FD</h3>
598<blockquote>
599libwebsocket deals with its <b>poll</b> loop
600internally, but in the case you are integrating with another
601server you will need to have libwebsocket sockets share a
602polling array with the other server. This and the other
603POLL_FD related callbacks let you put your specialized
604poll array interface code in the callback for protocol 0, the
605first protocol you support, usually the HTTP protocol in the
606serving case. This callback happens when a socket needs to be
607</blockquote>
608<h3>added to the polling loop</h3>
609<blockquote>
610<tt><b>user</b></tt> contains the fd, and
611<tt><b>len</b></tt> is the events bitmap (like, POLLIN). If you are using the
612internal polling loop (the "service" callback), you can just
613ignore these callbacks.
614</blockquote>
615<h3>LWS_CALLBACK_DEL_POLL_FD</h3>
616<blockquote>
617This callback happens when a socket descriptor
618needs to be removed from an external polling array. <tt><b>user</b></tt> is
619the socket desricptor. If you are using the internal polling
620loop, you can just ignore it.
621</blockquote>
622<h3>LWS_CALLBACK_SET_MODE_POLL_FD</h3>
623<blockquote>
624This callback happens when libwebsockets
625wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
626The handler should OR <tt><b>len</b></tt> on to the events member of the pollfd
627struct for this socket descriptor. If you are using the
628internal polling loop, you can just ignore it.
629</blockquote>
630<h3>LWS_CALLBACK_CLEAR_MODE_POLL_FD</h3>
631<blockquote>
632This callback occurs when libwebsockets
633wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
634The handler should AND ~<tt><b>len</b></tt> on to the events member of the
635pollfd struct for this socket descriptor. If you are using the
636internal polling loop, you can just ignore it.
637</blockquote>
Andy Green8f037e42010-12-19 22:13:26 +0000638<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000639<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
640<b>struct libwebsocket_protocols</b> {<br>
641&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
Darin Willitsc19456f2011-02-14 17:52:39 +0000642&nbsp; &nbsp; <i>int (*</i><b>callback</b>) <i>(struct libwebsocket_context * context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len)</i>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000643&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenb45993c2010-12-18 15:13:50 +0000644&nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
645&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
646&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
647&nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000648};<br>
649<h3>Members</h3>
650<dl>
651<dt><b>name</b>
652<dd>Protocol name that must match the one given in the client
653Javascript new WebSocket(url, 'protocol') name
654<dt><b>callback</b>
655<dd>The service callback used for this protocol. It allows the
656service action for an entire protocol to be encapsulated in
657the protocol-specific callback
658<dt><b>per_session_data_size</b>
659<dd>Each new connection using this protocol gets
660this much memory allocated on connection establishment and
661freed on connection takedown. A pointer to this per-connection
662allocation is passed into the callback in the 'user' parameter
Andy Greenb45993c2010-12-18 15:13:50 +0000663<dt><b>owning_server</b>
664<dd>the server init call fills in this opaque pointer when
665registering this protocol with the server.
666<dt><b>broadcast_socket_port</b>
667<dd>the server init call fills this in with the
668localhost port number used to forward broadcasts for this
669protocol
670<dt><b>broadcast_socket_user_fd</b>
671<dd>the server init call fills this in ... the <b>main</b>
672process context can write to this socket to perform broadcasts
673(use the <b>libwebsockets_broadcast</b> api to do this instead,
674it works from any process context)
675<dt><b>protocol_index</b>
676<dd>which protocol we are starting from zero
Andy Green4f3943a2010-11-12 10:44:16 +0000677</dl>
678<h3>Description</h3>
679<blockquote>
680This structure represents one protocol supported by the server. An
681array of these structures is passed to <b>libwebsocket_create_server</b>
682allows as many protocols as you like to be handled by one server.
683</blockquote>
684<hr>