blob: 77aea3e10c4432d79866ab8aaa116920228c9a61 [file] [log] [blame]
Andy Green62a12932010-11-03 11:19:23 +00001<h2>libwebsocket_create_server - Create the listening websockets server</h2>
Andy Greene92cd172011-01-19 13:11:55 +00002<i>struct libwebsocket_context *</i>
Andy Green62a12932010-11-03 11:19:23 +00003<b>libwebsocket_create_server</b>
4(<i>int</i> <b>port</b>,
Andy Greenb45993c2010-12-18 15:13:50 +00005<i>struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Green3faa9c72010-11-08 17:03:03 +00006<i>const char *</i> <b>ssl_cert_filepath</b>,
7<i>const char *</i> <b>ssl_private_key_filepath</b>,
8<i>int</i> <b>gid</b>,
9<i>int</i> <b>uid</b>)
Andy Green62a12932010-11-03 11:19:23 +000010<h3>Arguments</h3>
11<dl>
12<dt><b>port</b>
13<dd>Port to listen on
Andy Green4f3943a2010-11-12 10:44:16 +000014<dt><b>protocols</b>
15<dd>Array of structures listing supported protocols and a protocol-
16specific callback for each one. The list is ended with an
17entry that has a NULL callback pointer.
Andy Greenb45993c2010-12-18 15:13:50 +000018It's not const because we write the owning_server member
Andy Green3faa9c72010-11-08 17:03:03 +000019<dt><b>ssl_cert_filepath</b>
20<dd>If libwebsockets was compiled to use ssl, and you want
21to listen using SSL, set to the filepath to fetch the
22server cert from, otherwise NULL for unencrypted
23<dt><b>ssl_private_key_filepath</b>
24<dd>filepath to private key if wanting SSL mode,
25else ignored
26<dt><b>gid</b>
27<dd>group id to change to after setting listen socket, or -1.
28<dt><b>uid</b>
29<dd>user id to change to after setting listen socket, or -1.
Andy Green62a12932010-11-03 11:19:23 +000030</dl>
31<h3>Description</h3>
32<blockquote>
Andy Green47943ae2010-11-12 11:15:49 +000033This function creates the listening socket and takes care
Andy Green62a12932010-11-03 11:19:23 +000034of all initialization in one step.
35<p>
Andy Greene92cd172011-01-19 13:11:55 +000036After initialization, it returns a struct libwebsocket_context * that
37represents this server. After calling, user code needs to take care
38of calling <b>libwebsocket_service</b> with the context pointer to get the
39server's sockets serviced. This can be done in the same process context
40or a forked process, or another thread,
Andy Green47943ae2010-11-12 11:15:49 +000041<p>
42The protocol callback functions are called for a handful of events
43including http requests coming in, websocket connections becoming
Andy Green62a12932010-11-03 11:19:23 +000044established, and data arriving; it's also called periodically to allow
45async transmission.
46<p>
Andy Green47943ae2010-11-12 11:15:49 +000047HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
48at that time websocket protocol has not been negotiated. Other
49protocols after the first one never see any HTTP callack activity.
50<p>
Andy Green62a12932010-11-03 11:19:23 +000051The server created is a simple http server by default; part of the
52websocket standard is upgrading this http connection to a websocket one.
53<p>
54This allows the same server to provide files like scripts and favicon /
55images or whatever over http and dynamic data over websockets all in
56one place; they're all handled in the user callback.
57</blockquote>
58<hr>
Andy Greene92cd172011-01-19 13:11:55 +000059<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>
60<i>int</i>
61<b>libwebsockets_fork_service_loop</b>
62(<i>struct libwebsocket_context *</i> <b>this</b>)
63<h3>Arguments</h3>
64<dl>
65<dt><b>this</b>
66<dd>server context returned by creation function
67</dl>
68<hr>
Andy Greenb45993c2010-12-18 15:13:50 +000069<h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
70<i>const struct libwebsocket_protocols *</i>
71<b>libwebsockets_get_protocol</b>
72(<i>struct libwebsocket *</i> <b>wsi</b>)
73<h3>Arguments</h3>
74<dl>
75<dt><b>wsi</b>
76<dd>pointer to struct websocket you want to know the protocol of
77</dl>
78<h3>Description</h3>
79<blockquote>
80<p>
81This is useful to get the protocol to broadcast back to from inside
82the callback.
83</blockquote>
84<hr>
Andy Greene92cd172011-01-19 13:11:55 +000085<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 +000086<i>int</i>
87<b>libwebsockets_broadcast</b>
88(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
89<i>unsigned char *</i> <b>buf</b>,
90<i>size_t</i> <b>len</b>)
91<h3>Arguments</h3>
92<dl>
93<dt><b>protocol</b>
94<dd>pointer to the protocol you will broadcast to all members of
95<dt><b>buf</b>
96<dd>buffer containing the data to be broadcase. NOTE: this has to be
97allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
98the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
99case you are calling this function from callback context.
100<dt><b>len</b>
101<dd>length of payload data in buf, starting from buf.
102</dl>
103<h3>Description</h3>
104<blockquote>
105This function allows bulk sending of a packet to every connection using
106the given protocol. It does not send the data directly; instead it calls
107the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
108wants to actually send the data for that connection, the callback itself
109should call <b>libwebsocket_write</b>.
110<p>
111<b>libwebsockets_broadcast</b> can be called from another fork context without
112having to take any care about data visibility between the processes, it'll
113"just work".
114</blockquote>
115<hr>
Andy Green62a12932010-11-03 11:19:23 +0000116<h2>libwebsocket_write - Apply protocol then write data to client</h2>
117<i>int</i>
118<b>libwebsocket_write</b>
119(<i>struct libwebsocket *</i> <b>wsi</b>,
120<i>unsigned char *</i> <b>buf</b>,
121<i>size_t</i> <b>len</b>,
122<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
123<h3>Arguments</h3>
124<dl>
125<dt><b>wsi</b>
126<dd>Websocket instance (available from user callback)
127<dt><b>buf</b>
128<dd>The data to send. For data being sent on a websocket
129connection (ie, not default http), this buffer MUST have
130LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
131and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
132in the buffer after (buf + len). This is so the protocol
133header and trailer data can be added in-situ.
134<dt><b>len</b>
135<dd>Count of the data bytes in the payload starting from buf
136<dt><b>protocol</b>
137<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
138of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
139data on a websockets connection. Remember to allow the extra
140bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
141are used.
142</dl>
143<h3>Description</h3>
144<blockquote>
145This function provides the way to issue data back to the client
146for both http and websocket protocols.
147<p>
148In the case of sending using websocket protocol, be sure to allocate
149valid storage before and after buf as explained above. This scheme
150allows maximum efficiency of sending data and protocol in a single
151packet while not burdening the user code with any protocol knowledge.
152</blockquote>
153<hr>
154<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
155<i>int</i>
156<b>libwebsockets_serve_http_file</b>
157(<i>struct libwebsocket *</i> <b>wsi</b>,
158<i>const char *</i> <b>file</b>,
159<i>const char *</i> <b>content_type</b>)
160<h3>Arguments</h3>
161<dl>
162<dt><b>wsi</b>
163<dd>Websocket instance (available from user callback)
164<dt><b>file</b>
165<dd>The file to issue over http
166<dt><b>content_type</b>
167<dd>The http content type, eg, text/html
168</dl>
169<h3>Description</h3>
170<blockquote>
171This function is intended to be called from the callback in response
172to http requests from the client. It allows the callback to issue
173local files down the http link in a single step.
174</blockquote>
175<hr>
Andy Green38e57bb2011-01-19 12:20:27 +0000176<h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
177<i>size_t</i>
178<b>libwebsockets_remaining_packet_payload</b>
179(<i>struct libwebsocket *</i> <b>wsi</b>)
180<h3>Arguments</h3>
181<dl>
182<dt><b>wsi</b>
183<dd>Websocket instance (available from user callback)
184</dl>
185<h3>Description</h3>
186<blockquote>
187This function is intended to be called from the callback if the
188user code is interested in "complete packets" from the client.
189libwebsockets just passes through payload as it comes and issues a buffer
190additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
191callback handler can use this API to find out if the buffer it has just
192been given is the last piece of a "complete packet" from the client --
193when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
1940.
195<p>
196Many protocols won't care becuse their packets are always small.
197</blockquote>
198<hr>
Andy Green8f037e42010-12-19 22:13:26 +0000199<h2>callback - User server actions</h2>
200<i>int</i>
201<b>callback</b>
202(<i>struct libwebsocket *</i> <b>wsi</b>,
203<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
204<i>void *</i> <b>user</b>,
205<i>void *</i> <b>in</b>,
206<i>size_t</i> <b>len</b>)
207<h3>Arguments</h3>
208<dl>
209<dt><b>wsi</b>
210<dd>Opaque websocket instance pointer
211<dt><b>reason</b>
212<dd>The reason for the call
213<dt><b>user</b>
214<dd>Pointer to per-session user data allocated by library
215<dt><b>in</b>
216<dd>Pointer used for some callback reasons
217<dt><b>len</b>
218<dd>Length set for some callback reasons
219</dl>
220<h3>Description</h3>
221<blockquote>
222This callback is the way the user controls what is served. All the
223protocol detail is hidden and handled by the library.
224<p>
225For each connection / session there is user data allocated that is
226pointed to by "user". You set the size of this user data area when
227the library is initialized with libwebsocket_create_server.
228<p>
229You get an opportunity to initialize user data when called back with
230LWS_CALLBACK_ESTABLISHED reason.
231</blockquote>
232<h3>LWS_CALLBACK_ESTABLISHED</h3>
233<blockquote>
234after successful websocket handshake
235</blockquote>
236<h3>LWS_CALLBACK_CLOSED</h3>
237<blockquote>
238when the websocket session ends
239</blockquote>
240<h3>LWS_CALLBACK_BROADCAST</h3>
241<blockquote>
242signal to send to client (you would use
243<b>libwebsocket_write</b> taking care about the
244special buffer requirements
245</blockquote>
246<h3>LWS_CALLBACK_RECEIVE</h3>
247<blockquote>
248data has appeared for the server, it can be
249found at *in and is len bytes long
250</blockquote>
251<h3>LWS_CALLBACK_HTTP</h3>
252<blockquote>
253an http request has come from a client that is not
254asking to upgrade the connection to a websocket
255one. This is a chance to serve http content,
256for example, to send a script to the client
257which will then open the websockets connection.
258<tt><b>in</b></tt> points to the URI path requested and
259<b>libwebsockets_serve_http_file</b> makes it very
260simple to send back a file to the client.
261</blockquote>
262<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000263<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
264<b>struct libwebsocket_protocols</b> {<br>
265&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
Andy Greene77ddd82010-11-13 10:03:47 +0000266&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 +0000267&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenb45993c2010-12-18 15:13:50 +0000268&nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
269&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
270&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
271&nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000272};<br>
273<h3>Members</h3>
274<dl>
275<dt><b>name</b>
276<dd>Protocol name that must match the one given in the client
277Javascript new WebSocket(url, 'protocol') name
278<dt><b>callback</b>
279<dd>The service callback used for this protocol. It allows the
280service action for an entire protocol to be encapsulated in
281the protocol-specific callback
282<dt><b>per_session_data_size</b>
283<dd>Each new connection using this protocol gets
284this much memory allocated on connection establishment and
285freed on connection takedown. A pointer to this per-connection
286allocation is passed into the callback in the 'user' parameter
Andy Greenb45993c2010-12-18 15:13:50 +0000287<dt><b>owning_server</b>
288<dd>the server init call fills in this opaque pointer when
289registering this protocol with the server.
290<dt><b>broadcast_socket_port</b>
291<dd>the server init call fills this in with the
292localhost port number used to forward broadcasts for this
293protocol
294<dt><b>broadcast_socket_user_fd</b>
295<dd>the server init call fills this in ... the <b>main</b>
296process context can write to this socket to perform broadcasts
297(use the <b>libwebsockets_broadcast</b> api to do this instead,
298it works from any process context)
299<dt><b>protocol_index</b>
300<dd>which protocol we are starting from zero
Andy Green4f3943a2010-11-12 10:44:16 +0000301</dl>
302<h3>Description</h3>
303<blockquote>
304This structure represents one protocol supported by the server. An
305array of these structures is passed to <b>libwebsocket_create_server</b>
306allows as many protocols as you like to be handled by one server.
307</blockquote>
308<hr>