blob: f649d795b56a02596bc3eb8a22c0d63a93dad9ce [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>
81<h2>libwebsocket_rx_flow_control - Enable and disable socket servicing for receieved packets.</h2>
82<i>int</i>
83<b>libwebsocket_rx_flow_control</b>
84(<i>struct libwebsocket *</i> <b>wsi</b>,
85<i>int</i> <b>enable</b>)
86<h3>Arguments</h3>
87<dl>
88<dt><b>wsi</b>
89<dd>Websocket connection instance to get callback for
90<dt><b>enable</b>
91<dd>0 = disable read servicing for this connection, 1 = enable
92</dl>
93<h3>Description</h3>
94<blockquote>
95<p>
96If the output side of a server process becomes choked, this allows flow
97control for the input side.
98</blockquote>
99<hr>
Andy Green4739e5c2011-01-22 12:51:57 +0000100<h2>libwebsocket_create_context - Create the websocket handler</h2>
Andy Greene92cd172011-01-19 13:11:55 +0000101<i>struct libwebsocket_context *</i>
Andy Green4739e5c2011-01-22 12:51:57 +0000102<b>libwebsocket_create_context</b>
Andy Green62a12932010-11-03 11:19:23 +0000103(<i>int</i> <b>port</b>,
Andy Greenb45993c2010-12-18 15:13:50 +0000104<i>struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Green3faa9c72010-11-08 17:03:03 +0000105<i>const char *</i> <b>ssl_cert_filepath</b>,
106<i>const char *</i> <b>ssl_private_key_filepath</b>,
107<i>int</i> <b>gid</b>,
108<i>int</i> <b>uid</b>)
Andy Green62a12932010-11-03 11:19:23 +0000109<h3>Arguments</h3>
110<dl>
111<dt><b>port</b>
Andy Green4739e5c2011-01-22 12:51:57 +0000112<dd>Port to listen on... you can use 0 to suppress listening on
113any port, that's what you want if you are not running a
114websocket server at all but just using it as a client
Andy Green4f3943a2010-11-12 10:44:16 +0000115<dt><b>protocols</b>
116<dd>Array of structures listing supported protocols and a protocol-
117specific callback for each one. The list is ended with an
118entry that has a NULL callback pointer.
Andy Greenb45993c2010-12-18 15:13:50 +0000119It's not const because we write the owning_server member
Andy Green3faa9c72010-11-08 17:03:03 +0000120<dt><b>ssl_cert_filepath</b>
121<dd>If libwebsockets was compiled to use ssl, and you want
122to listen using SSL, set to the filepath to fetch the
123server cert from, otherwise NULL for unencrypted
124<dt><b>ssl_private_key_filepath</b>
125<dd>filepath to private key if wanting SSL mode,
126else ignored
127<dt><b>gid</b>
128<dd>group id to change to after setting listen socket, or -1.
129<dt><b>uid</b>
130<dd>user id to change to after setting listen socket, or -1.
Andy Green62a12932010-11-03 11:19:23 +0000131</dl>
132<h3>Description</h3>
133<blockquote>
Andy Green47943ae2010-11-12 11:15:49 +0000134This function creates the listening socket and takes care
Andy Green62a12932010-11-03 11:19:23 +0000135of all initialization in one step.
136<p>
Andy Greene92cd172011-01-19 13:11:55 +0000137After initialization, it returns a struct libwebsocket_context * that
138represents this server. After calling, user code needs to take care
139of calling <b>libwebsocket_service</b> with the context pointer to get the
140server's sockets serviced. This can be done in the same process context
141or a forked process, or another thread,
Andy Green47943ae2010-11-12 11:15:49 +0000142<p>
143The protocol callback functions are called for a handful of events
144including http requests coming in, websocket connections becoming
Andy Green62a12932010-11-03 11:19:23 +0000145established, and data arriving; it's also called periodically to allow
146async transmission.
147<p>
Andy Green47943ae2010-11-12 11:15:49 +0000148HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
149at that time websocket protocol has not been negotiated. Other
150protocols after the first one never see any HTTP callack activity.
151<p>
Andy Green62a12932010-11-03 11:19:23 +0000152The server created is a simple http server by default; part of the
153websocket standard is upgrading this http connection to a websocket one.
154<p>
155This allows the same server to provide files like scripts and favicon /
156images or whatever over http and dynamic data over websockets all in
157one place; they're all handled in the user callback.
158</blockquote>
159<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000160<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>
161<i>int</i>
162<b>libwebsockets_fork_service_loop</b>
163(<i>struct libwebsocket_context *</i> <b>this</b>)
164<h3>Arguments</h3>
165<dl>
166<dt><b>this</b>
167<dd>server context returned by creation function
168</dl>
169<hr>
Andy Greenb45993c2010-12-18 15:13:50 +0000170<h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
171<i>const struct libwebsocket_protocols *</i>
172<b>libwebsockets_get_protocol</b>
173(<i>struct libwebsocket *</i> <b>wsi</b>)
174<h3>Arguments</h3>
175<dl>
176<dt><b>wsi</b>
177<dd>pointer to struct websocket you want to know the protocol of
178</dl>
179<h3>Description</h3>
180<blockquote>
181<p>
182This is useful to get the protocol to broadcast back to from inside
183the callback.
184</blockquote>
185<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000186<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 +0000187<i>int</i>
188<b>libwebsockets_broadcast</b>
189(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
190<i>unsigned char *</i> <b>buf</b>,
191<i>size_t</i> <b>len</b>)
192<h3>Arguments</h3>
193<dl>
194<dt><b>protocol</b>
195<dd>pointer to the protocol you will broadcast to all members of
196<dt><b>buf</b>
197<dd>buffer containing the data to be broadcase. NOTE: this has to be
198allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
199the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
200case you are calling this function from callback context.
201<dt><b>len</b>
202<dd>length of payload data in buf, starting from buf.
203</dl>
204<h3>Description</h3>
205<blockquote>
206This function allows bulk sending of a packet to every connection using
207the given protocol. It does not send the data directly; instead it calls
208the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
209wants to actually send the data for that connection, the callback itself
210should call <b>libwebsocket_write</b>.
211<p>
212<b>libwebsockets_broadcast</b> can be called from another fork context without
213having to take any care about data visibility between the processes, it'll
214"just work".
215</blockquote>
216<hr>
Andy Green62a12932010-11-03 11:19:23 +0000217<h2>libwebsocket_write - Apply protocol then write data to client</h2>
218<i>int</i>
219<b>libwebsocket_write</b>
220(<i>struct libwebsocket *</i> <b>wsi</b>,
221<i>unsigned char *</i> <b>buf</b>,
222<i>size_t</i> <b>len</b>,
223<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
224<h3>Arguments</h3>
225<dl>
226<dt><b>wsi</b>
227<dd>Websocket instance (available from user callback)
228<dt><b>buf</b>
229<dd>The data to send. For data being sent on a websocket
230connection (ie, not default http), this buffer MUST have
231LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
232and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
233in the buffer after (buf + len). This is so the protocol
234header and trailer data can be added in-situ.
235<dt><b>len</b>
236<dd>Count of the data bytes in the payload starting from buf
237<dt><b>protocol</b>
238<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
239of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
240data on a websockets connection. Remember to allow the extra
241bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
242are used.
243</dl>
244<h3>Description</h3>
245<blockquote>
246This function provides the way to issue data back to the client
247for both http and websocket protocols.
248<p>
249In the case of sending using websocket protocol, be sure to allocate
250valid storage before and after buf as explained above. This scheme
251allows maximum efficiency of sending data and protocol in a single
252packet while not burdening the user code with any protocol knowledge.
253</blockquote>
254<hr>
255<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
256<i>int</i>
257<b>libwebsockets_serve_http_file</b>
258(<i>struct libwebsocket *</i> <b>wsi</b>,
259<i>const char *</i> <b>file</b>,
260<i>const char *</i> <b>content_type</b>)
261<h3>Arguments</h3>
262<dl>
263<dt><b>wsi</b>
264<dd>Websocket instance (available from user callback)
265<dt><b>file</b>
266<dd>The file to issue over http
267<dt><b>content_type</b>
268<dd>The http content type, eg, text/html
269</dl>
270<h3>Description</h3>
271<blockquote>
272This function is intended to be called from the callback in response
273to http requests from the client. It allows the callback to issue
274local files down the http link in a single step.
275</blockquote>
276<hr>
Andy Green38e57bb2011-01-19 12:20:27 +0000277<h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
278<i>size_t</i>
279<b>libwebsockets_remaining_packet_payload</b>
280(<i>struct libwebsocket *</i> <b>wsi</b>)
281<h3>Arguments</h3>
282<dl>
283<dt><b>wsi</b>
284<dd>Websocket instance (available from user callback)
285</dl>
286<h3>Description</h3>
287<blockquote>
288This function is intended to be called from the callback if the
289user code is interested in "complete packets" from the client.
290libwebsockets just passes through payload as it comes and issues a buffer
291additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
292callback handler can use this API to find out if the buffer it has just
293been given is the last piece of a "complete packet" from the client --
294when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
2950.
296<p>
297Many protocols won't care becuse their packets are always small.
298</blockquote>
299<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000300<h2>libwebsocket_client_connect - Connect to another websocket server</h2>
301<i>struct libwebsocket *</i>
302<b>libwebsocket_client_connect</b>
303(<i>struct libwebsocket_context *</i> <b>this</b>,
304<i>const char *</i> <b>address</b>,
305<i>int</i> <b>port</b>,
306<i>int</i> <b>ssl_connection</b>,
307<i>const char *</i> <b>path</b>,
308<i>const char *</i> <b>host</b>,
309<i>const char *</i> <b>origin</b>,
310<i>const char *</i> <b>protocol</b>)
311<h3>Arguments</h3>
312<dl>
313<dt><b>this</b>
314<dd>Websocket context
315<dt><b>address</b>
316<dd>Remote server address, eg, "myserver.com"
317<dt><b>port</b>
318<dd>Port to connect to on the remote server, eg, 80
319<dt><b>ssl_connection</b>
320<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
321signed certs
322<dt><b>path</b>
323<dd>Websocket path on server
324<dt><b>host</b>
325<dd>Hostname on server
326<dt><b>origin</b>
327<dd>Socket origin name
328<dt><b>protocol</b>
329<dd>Comma-separated list of protocols being asked for from
330the server, or just one. The server will pick the one it
331likes best.
332</dl>
333<h3>Description</h3>
334<blockquote>
335This function creates a connection to a remote server
336</blockquote>
337<hr>
Andy Green8f037e42010-12-19 22:13:26 +0000338<h2>callback - User server actions</h2>
339<i>int</i>
340<b>callback</b>
341(<i>struct libwebsocket *</i> <b>wsi</b>,
342<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
343<i>void *</i> <b>user</b>,
344<i>void *</i> <b>in</b>,
345<i>size_t</i> <b>len</b>)
346<h3>Arguments</h3>
347<dl>
348<dt><b>wsi</b>
349<dd>Opaque websocket instance pointer
350<dt><b>reason</b>
351<dd>The reason for the call
352<dt><b>user</b>
353<dd>Pointer to per-session user data allocated by library
354<dt><b>in</b>
355<dd>Pointer used for some callback reasons
356<dt><b>len</b>
357<dd>Length set for some callback reasons
358</dl>
359<h3>Description</h3>
360<blockquote>
361This callback is the way the user controls what is served. All the
362protocol detail is hidden and handled by the library.
363<p>
364For each connection / session there is user data allocated that is
365pointed to by "user". You set the size of this user data area when
366the library is initialized with libwebsocket_create_server.
367<p>
368You get an opportunity to initialize user data when called back with
369LWS_CALLBACK_ESTABLISHED reason.
370</blockquote>
371<h3>LWS_CALLBACK_ESTABLISHED</h3>
372<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000373after the server completes a handshake with
374an incoming client
375</blockquote>
376<h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
377<blockquote>
378after your client connection completed
379a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000380</blockquote>
381<h3>LWS_CALLBACK_CLOSED</h3>
382<blockquote>
383when the websocket session ends
384</blockquote>
385<h3>LWS_CALLBACK_BROADCAST</h3>
386<blockquote>
387signal to send to client (you would use
388<b>libwebsocket_write</b> taking care about the
389special buffer requirements
390</blockquote>
391<h3>LWS_CALLBACK_RECEIVE</h3>
392<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000393data has appeared for this server endpoint from a
394remote client, it can be found at *in and is
395len bytes long
396</blockquote>
397<h3>LWS_CALLBACK_CLIENT_RECEIVE</h3>
398<blockquote>
399data has appeared from the server for the
400client connection, it can be found at *in and
401is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000402</blockquote>
403<h3>LWS_CALLBACK_HTTP</h3>
404<blockquote>
405an http request has come from a client that is not
406asking to upgrade the connection to a websocket
407one. This is a chance to serve http content,
408for example, to send a script to the client
409which will then open the websockets connection.
Andy Green7619c472011-01-23 17:47:08 +0000410<tt><b>in</b></tt> points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000411<b>libwebsockets_serve_http_file</b> makes it very
412simple to send back a file to the client.
413</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000414<h3>LWS_CALLBACK_CLIENT_WRITEABLE</h3>
415<blockquote>
416if you call
417<b>libwebsocket_callback_on_writable</b> on a connection, you will
418get this callback coming when the connection socket is able to
419accept another write packet without blocking. If it already
420was able to take another packet without blocking, you'll get
421this callback at the next call to the service loop function.
422</blockquote>
Andy Green8f037e42010-12-19 22:13:26 +0000423<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000424<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
425<b>struct libwebsocket_protocols</b> {<br>
426&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
Andy Greene77ddd82010-11-13 10:03:47 +0000427&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 +0000428&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenb45993c2010-12-18 15:13:50 +0000429&nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
430&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
431&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
432&nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000433};<br>
434<h3>Members</h3>
435<dl>
436<dt><b>name</b>
437<dd>Protocol name that must match the one given in the client
438Javascript new WebSocket(url, 'protocol') name
439<dt><b>callback</b>
440<dd>The service callback used for this protocol. It allows the
441service action for an entire protocol to be encapsulated in
442the protocol-specific callback
443<dt><b>per_session_data_size</b>
444<dd>Each new connection using this protocol gets
445this much memory allocated on connection establishment and
446freed on connection takedown. A pointer to this per-connection
447allocation is passed into the callback in the 'user' parameter
Andy Greenb45993c2010-12-18 15:13:50 +0000448<dt><b>owning_server</b>
449<dd>the server init call fills in this opaque pointer when
450registering this protocol with the server.
451<dt><b>broadcast_socket_port</b>
452<dd>the server init call fills this in with the
453localhost port number used to forward broadcasts for this
454protocol
455<dt><b>broadcast_socket_user_fd</b>
456<dd>the server init call fills this in ... the <b>main</b>
457process context can write to this socket to perform broadcasts
458(use the <b>libwebsockets_broadcast</b> api to do this instead,
459it works from any process context)
460<dt><b>protocol_index</b>
461<dd>which protocol we are starting from zero
Andy Green4f3943a2010-11-12 10:44:16 +0000462</dl>
463<h3>Description</h3>
464<blockquote>
465This structure represents one protocol supported by the server. An
466array of these structures is passed to <b>libwebsocket_create_server</b>
467allows as many protocols as you like to be handled by one server.
468</blockquote>
469<hr>