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