blob: 0016684d359bd84f0c1aac631ba247d85042d6bb [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 Green4739e5c2011-01-22 12:51:57 +000061<h2>libwebsocket_create_context - Create the websocket handler</h2>
Andy Greene92cd172011-01-19 13:11:55 +000062<i>struct libwebsocket_context *</i>
Andy Green4739e5c2011-01-22 12:51:57 +000063<b>libwebsocket_create_context</b>
Andy Green62a12932010-11-03 11:19:23 +000064(<i>int</i> <b>port</b>,
Andy Greenb45993c2010-12-18 15:13:50 +000065<i>struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Green3faa9c72010-11-08 17:03:03 +000066<i>const char *</i> <b>ssl_cert_filepath</b>,
67<i>const char *</i> <b>ssl_private_key_filepath</b>,
68<i>int</i> <b>gid</b>,
69<i>int</i> <b>uid</b>)
Andy Green62a12932010-11-03 11:19:23 +000070<h3>Arguments</h3>
71<dl>
72<dt><b>port</b>
Andy Green4739e5c2011-01-22 12:51:57 +000073<dd>Port to listen on... you can use 0 to suppress listening on
74any port, that's what you want if you are not running a
75websocket server at all but just using it as a client
Andy Green4f3943a2010-11-12 10:44:16 +000076<dt><b>protocols</b>
77<dd>Array of structures listing supported protocols and a protocol-
78specific callback for each one. The list is ended with an
79entry that has a NULL callback pointer.
Andy Greenb45993c2010-12-18 15:13:50 +000080It's not const because we write the owning_server member
Andy Green3faa9c72010-11-08 17:03:03 +000081<dt><b>ssl_cert_filepath</b>
82<dd>If libwebsockets was compiled to use ssl, and you want
83to listen using SSL, set to the filepath to fetch the
84server cert from, otherwise NULL for unencrypted
85<dt><b>ssl_private_key_filepath</b>
86<dd>filepath to private key if wanting SSL mode,
87else ignored
88<dt><b>gid</b>
89<dd>group id to change to after setting listen socket, or -1.
90<dt><b>uid</b>
91<dd>user id to change to after setting listen socket, or -1.
Andy Green62a12932010-11-03 11:19:23 +000092</dl>
93<h3>Description</h3>
94<blockquote>
Andy Green47943ae2010-11-12 11:15:49 +000095This function creates the listening socket and takes care
Andy Green62a12932010-11-03 11:19:23 +000096of all initialization in one step.
97<p>
Andy Greene92cd172011-01-19 13:11:55 +000098After initialization, it returns a struct libwebsocket_context * that
99represents this server. After calling, user code needs to take care
100of calling <b>libwebsocket_service</b> with the context pointer to get the
101server's sockets serviced. This can be done in the same process context
102or a forked process, or another thread,
Andy Green47943ae2010-11-12 11:15:49 +0000103<p>
104The protocol callback functions are called for a handful of events
105including http requests coming in, websocket connections becoming
Andy Green62a12932010-11-03 11:19:23 +0000106established, and data arriving; it's also called periodically to allow
107async transmission.
108<p>
Andy Green47943ae2010-11-12 11:15:49 +0000109HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
110at that time websocket protocol has not been negotiated. Other
111protocols after the first one never see any HTTP callack activity.
112<p>
Andy Green62a12932010-11-03 11:19:23 +0000113The server created is a simple http server by default; part of the
114websocket standard is upgrading this http connection to a websocket one.
115<p>
116This allows the same server to provide files like scripts and favicon /
117images or whatever over http and dynamic data over websockets all in
118one place; they're all handled in the user callback.
119</blockquote>
120<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000121<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>
122<i>int</i>
123<b>libwebsockets_fork_service_loop</b>
124(<i>struct libwebsocket_context *</i> <b>this</b>)
125<h3>Arguments</h3>
126<dl>
127<dt><b>this</b>
128<dd>server context returned by creation function
129</dl>
130<hr>
Andy Greenb45993c2010-12-18 15:13:50 +0000131<h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
132<i>const struct libwebsocket_protocols *</i>
133<b>libwebsockets_get_protocol</b>
134(<i>struct libwebsocket *</i> <b>wsi</b>)
135<h3>Arguments</h3>
136<dl>
137<dt><b>wsi</b>
138<dd>pointer to struct websocket you want to know the protocol of
139</dl>
140<h3>Description</h3>
141<blockquote>
142<p>
143This is useful to get the protocol to broadcast back to from inside
144the callback.
145</blockquote>
146<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000147<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 +0000148<i>int</i>
149<b>libwebsockets_broadcast</b>
150(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
151<i>unsigned char *</i> <b>buf</b>,
152<i>size_t</i> <b>len</b>)
153<h3>Arguments</h3>
154<dl>
155<dt><b>protocol</b>
156<dd>pointer to the protocol you will broadcast to all members of
157<dt><b>buf</b>
158<dd>buffer containing the data to be broadcase. NOTE: this has to be
159allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
160the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
161case you are calling this function from callback context.
162<dt><b>len</b>
163<dd>length of payload data in buf, starting from buf.
164</dl>
165<h3>Description</h3>
166<blockquote>
167This function allows bulk sending of a packet to every connection using
168the given protocol. It does not send the data directly; instead it calls
169the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
170wants to actually send the data for that connection, the callback itself
171should call <b>libwebsocket_write</b>.
172<p>
173<b>libwebsockets_broadcast</b> can be called from another fork context without
174having to take any care about data visibility between the processes, it'll
175"just work".
176</blockquote>
177<hr>
Andy Green62a12932010-11-03 11:19:23 +0000178<h2>libwebsocket_write - Apply protocol then write data to client</h2>
179<i>int</i>
180<b>libwebsocket_write</b>
181(<i>struct libwebsocket *</i> <b>wsi</b>,
182<i>unsigned char *</i> <b>buf</b>,
183<i>size_t</i> <b>len</b>,
184<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
185<h3>Arguments</h3>
186<dl>
187<dt><b>wsi</b>
188<dd>Websocket instance (available from user callback)
189<dt><b>buf</b>
190<dd>The data to send. For data being sent on a websocket
191connection (ie, not default http), this buffer MUST have
192LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
193and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
194in the buffer after (buf + len). This is so the protocol
195header and trailer data can be added in-situ.
196<dt><b>len</b>
197<dd>Count of the data bytes in the payload starting from buf
198<dt><b>protocol</b>
199<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
200of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
201data on a websockets connection. Remember to allow the extra
202bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
203are used.
204</dl>
205<h3>Description</h3>
206<blockquote>
207This function provides the way to issue data back to the client
208for both http and websocket protocols.
209<p>
210In the case of sending using websocket protocol, be sure to allocate
211valid storage before and after buf as explained above. This scheme
212allows maximum efficiency of sending data and protocol in a single
213packet while not burdening the user code with any protocol knowledge.
214</blockquote>
215<hr>
216<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
217<i>int</i>
218<b>libwebsockets_serve_http_file</b>
219(<i>struct libwebsocket *</i> <b>wsi</b>,
220<i>const char *</i> <b>file</b>,
221<i>const char *</i> <b>content_type</b>)
222<h3>Arguments</h3>
223<dl>
224<dt><b>wsi</b>
225<dd>Websocket instance (available from user callback)
226<dt><b>file</b>
227<dd>The file to issue over http
228<dt><b>content_type</b>
229<dd>The http content type, eg, text/html
230</dl>
231<h3>Description</h3>
232<blockquote>
233This function is intended to be called from the callback in response
234to http requests from the client. It allows the callback to issue
235local files down the http link in a single step.
236</blockquote>
237<hr>
Andy Green38e57bb2011-01-19 12:20:27 +0000238<h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
239<i>size_t</i>
240<b>libwebsockets_remaining_packet_payload</b>
241(<i>struct libwebsocket *</i> <b>wsi</b>)
242<h3>Arguments</h3>
243<dl>
244<dt><b>wsi</b>
245<dd>Websocket instance (available from user callback)
246</dl>
247<h3>Description</h3>
248<blockquote>
249This function is intended to be called from the callback if the
250user code is interested in "complete packets" from the client.
251libwebsockets just passes through payload as it comes and issues a buffer
252additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
253callback handler can use this API to find out if the buffer it has just
254been given is the last piece of a "complete packet" from the client --
255when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
2560.
257<p>
258Many protocols won't care becuse their packets are always small.
259</blockquote>
260<hr>
Andy Green8f037e42010-12-19 22:13:26 +0000261<h2>callback - User server actions</h2>
262<i>int</i>
263<b>callback</b>
264(<i>struct libwebsocket *</i> <b>wsi</b>,
265<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
266<i>void *</i> <b>user</b>,
267<i>void *</i> <b>in</b>,
268<i>size_t</i> <b>len</b>)
269<h3>Arguments</h3>
270<dl>
271<dt><b>wsi</b>
272<dd>Opaque websocket instance pointer
273<dt><b>reason</b>
274<dd>The reason for the call
275<dt><b>user</b>
276<dd>Pointer to per-session user data allocated by library
277<dt><b>in</b>
278<dd>Pointer used for some callback reasons
279<dt><b>len</b>
280<dd>Length set for some callback reasons
281</dl>
282<h3>Description</h3>
283<blockquote>
284This callback is the way the user controls what is served. All the
285protocol detail is hidden and handled by the library.
286<p>
287For each connection / session there is user data allocated that is
288pointed to by "user". You set the size of this user data area when
289the library is initialized with libwebsocket_create_server.
290<p>
291You get an opportunity to initialize user data when called back with
292LWS_CALLBACK_ESTABLISHED reason.
293</blockquote>
294<h3>LWS_CALLBACK_ESTABLISHED</h3>
295<blockquote>
296after successful websocket handshake
297</blockquote>
298<h3>LWS_CALLBACK_CLOSED</h3>
299<blockquote>
300when the websocket session ends
301</blockquote>
302<h3>LWS_CALLBACK_BROADCAST</h3>
303<blockquote>
304signal to send to client (you would use
305<b>libwebsocket_write</b> taking care about the
306special buffer requirements
307</blockquote>
308<h3>LWS_CALLBACK_RECEIVE</h3>
309<blockquote>
310data has appeared for the server, it can be
311found at *in and is len bytes long
312</blockquote>
313<h3>LWS_CALLBACK_HTTP</h3>
314<blockquote>
315an http request has come from a client that is not
316asking to upgrade the connection to a websocket
317one. This is a chance to serve http content,
318for example, to send a script to the client
319which will then open the websockets connection.
Andy Green7619c472011-01-23 17:47:08 +0000320<tt><b>in</b></tt> points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000321<b>libwebsockets_serve_http_file</b> makes it very
322simple to send back a file to the client.
323</blockquote>
324<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000325<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
326<b>struct libwebsocket_protocols</b> {<br>
327&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
Andy Greene77ddd82010-11-13 10:03:47 +0000328&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 +0000329&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenb45993c2010-12-18 15:13:50 +0000330&nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
331&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
332&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
333&nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000334};<br>
335<h3>Members</h3>
336<dl>
337<dt><b>name</b>
338<dd>Protocol name that must match the one given in the client
339Javascript new WebSocket(url, 'protocol') name
340<dt><b>callback</b>
341<dd>The service callback used for this protocol. It allows the
342service action for an entire protocol to be encapsulated in
343the protocol-specific callback
344<dt><b>per_session_data_size</b>
345<dd>Each new connection using this protocol gets
346this much memory allocated on connection establishment and
347freed on connection takedown. A pointer to this per-connection
348allocation is passed into the callback in the 'user' parameter
Andy Greenb45993c2010-12-18 15:13:50 +0000349<dt><b>owning_server</b>
350<dd>the server init call fills in this opaque pointer when
351registering this protocol with the server.
352<dt><b>broadcast_socket_port</b>
353<dd>the server init call fills this in with the
354localhost port number used to forward broadcasts for this
355protocol
356<dt><b>broadcast_socket_user_fd</b>
357<dd>the server init call fills this in ... the <b>main</b>
358process context can write to this socket to perform broadcasts
359(use the <b>libwebsockets_broadcast</b> api to do this instead,
360it works from any process context)
361<dt><b>protocol_index</b>
362<dd>which protocol we are starting from zero
Andy Green4f3943a2010-11-12 10:44:16 +0000363</dl>
364<h3>Description</h3>
365<blockquote>
366This structure represents one protocol supported by the server. An
367array of these structures is passed to <b>libwebsocket_create_server</b>
368allows as many protocols as you like to be handled by one server.
369</blockquote>
370<hr>