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