introduce-client-support.patch
[profile/ivi/libwebsockets.git] / libwebsockets-api-doc.html
1 <h2>libwebsocket_create_context - Create the websocket handler</h2>
2 <i>struct libwebsocket_context *</i>
3 <b>libwebsocket_create_context</b>
4 (<i>int</i> <b>port</b>,
5 <i>struct libwebsocket_protocols *</i> <b>protocols</b>,
6 <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>)
10 <h3>Arguments</h3>
11 <dl>
12 <dt><b>port</b>
13 <dd>Port to listen on... you can use 0 to suppress listening on
14 any port, that's what you want if you are not running a
15 websocket server at all but just using it as a client
16 <dt><b>protocols</b>
17 <dd>Array of structures listing supported protocols and a protocol-
18 specific callback for each one.  The list is ended with an
19 entry that has a NULL callback pointer.
20 It's not const because we write the owning_server member
21 <dt><b>ssl_cert_filepath</b>
22 <dd>If libwebsockets was compiled to use ssl, and you want
23 to listen using SSL, set to the filepath to fetch the
24 server cert from, otherwise NULL for unencrypted
25 <dt><b>ssl_private_key_filepath</b>
26 <dd>filepath to private key if wanting SSL mode,
27 else 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.
32 </dl>
33 <h3>Description</h3>
34 <blockquote>
35 This function creates the listening socket and takes care
36 of all initialization in one step.
37 <p>
38 After initialization, it returns a struct libwebsocket_context * that
39 represents this server.  After calling, user code needs to take care
40 of calling <b>libwebsocket_service</b> with the context pointer to get the
41 server's sockets serviced.  This can be done in the same process context
42 or a forked process, or another thread,
43 <p>
44 The protocol callback functions are called for a handful of events
45 including http requests coming in, websocket connections becoming
46 established, and data arriving; it's also called periodically to allow
47 async transmission.
48 <p>
49 HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
50 at that time websocket protocol has not been negotiated.  Other
51 protocols after the first one never see any HTTP callack activity.
52 <p>
53 The server created is a simple http server by default; part of the
54 websocket standard is upgrading this http connection to a websocket one.
55 <p>
56 This allows the same server to provide files like scripts and favicon /
57 images or whatever over http and dynamic data over websockets all in
58 one place; they're all handled in the user callback.
59 </blockquote>
60 <hr>
61 <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>
71 <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>
83 This is useful to get the protocol to broadcast back to from inside
84 the callback.
85 </blockquote>
86 <hr>
87 <h2>libwebsockets_broadcast - Sends a buffer to the callback for all active connections of the given protocol.</h2>
88 <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
99 allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
100 the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
101 case 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>
107 This function allows bulk sending of a packet to every connection using
108 the given protocol.  It does not send the data directly; instead it calls
109 the callback with a reason type of LWS_CALLBACK_BROADCAST.  If the callback
110 wants to actually send the data for that connection, the callback itself
111 should call <b>libwebsocket_write</b>.
112 <p>
113 <b>libwebsockets_broadcast</b> can be called from another fork context without
114 having to take any care about data visibility between the processes, it'll
115 "just work".
116 </blockquote>
117 <hr>
118 <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
131 connection (ie, not default http), this buffer MUST have
132 LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
133 and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
134 in the buffer after (buf + len).  This is so the protocol
135 header 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
140 of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
141 data on a websockets connection.  Remember to allow the extra
142 bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
143 are used.
144 </dl>
145 <h3>Description</h3>
146 <blockquote>
147 This function provides the way to issue data back to the client
148 for both http and websocket protocols.
149 <p>
150 In the case of sending using websocket protocol, be sure to allocate
151 valid storage before and after buf as explained above.  This scheme
152 allows maximum efficiency of sending data and protocol in a single
153 packet 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>
173 This function is intended to be called from the callback in response
174 to http requests from the client.  It allows the callback to issue
175 local files down the http link in a single step.
176 </blockquote>
177 <hr>
178 <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>
189 This function is intended to be called from the callback if the
190 user code is interested in "complete packets" from the client.
191 libwebsockets just passes through payload as it comes and issues a buffer
192 additionally when it hits a built-in limit.  The LWS_CALLBACK_RECEIVE
193 callback handler can use this API to find out if the buffer it has just
194 been given is the last piece of a "complete packet" from the client --
195 when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
196 0.
197 <p>
198 Many protocols won't care becuse their packets are always small.
199 </blockquote>
200 <hr>
201 <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>
224 This callback is the way the user controls what is served.  All the
225 protocol detail is hidden and handled by the library.
226 <p>
227 For each connection / session there is user data allocated that is
228 pointed to by "user".  You set the size of this user data area when
229 the library is initialized with libwebsocket_create_server.
230 <p>
231 You get an opportunity to initialize user data when called back with
232 LWS_CALLBACK_ESTABLISHED reason.
233 </blockquote>
234 <h3>LWS_CALLBACK_ESTABLISHED</h3>
235 <blockquote>
236 after successful websocket handshake
237 </blockquote>
238 <h3>LWS_CALLBACK_CLOSED</h3>
239 <blockquote>
240 when the websocket session ends
241 </blockquote>
242 <h3>LWS_CALLBACK_BROADCAST</h3>
243 <blockquote>
244 signal to send to client (you would use
245 <b>libwebsocket_write</b> taking care about the
246 special buffer requirements
247 </blockquote>
248 <h3>LWS_CALLBACK_RECEIVE</h3>
249 <blockquote>
250 data has appeared for the server, it can be
251 found at *in and is len bytes long
252 </blockquote>
253 <h3>LWS_CALLBACK_HTTP</h3>
254 <blockquote>
255 an http request has come from a client that is not
256 asking to upgrade the connection to a websocket
257 one.  This is a chance to serve http content,
258 for example, to send a script to the client
259 which 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
262 simple to send back a file to the client.
263 </blockquote>
264 <hr>
265 <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>
268 &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>
269 &nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
270 &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>
274 };<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
279 Javascript new WebSocket(url, 'protocol') name
280 <dt><b>callback</b>
281 <dd>The service callback used for this protocol.  It allows the
282 service action for an entire protocol to be encapsulated in
283 the protocol-specific callback
284 <dt><b>per_session_data_size</b>
285 <dd>Each new connection using this protocol gets
286 this much memory allocated on connection establishment and
287 freed on connection takedown.  A pointer to this per-connection
288 allocation is passed into the callback in the 'user' parameter
289 <dt><b>owning_server</b>
290 <dd>the server init call fills in this opaque pointer when
291 registering this protocol with the server.
292 <dt><b>broadcast_socket_port</b>
293 <dd>the server init call fills this in with the
294 localhost port number used to forward broadcasts for this
295 protocol
296 <dt><b>broadcast_socket_user_fd</b>
297 <dd>the server init call fills this in ... the <b>main</b>
298 process context can write to this socket to perform broadcasts
299 (use the <b>libwebsockets_broadcast</b> api to do this instead,
300 it works from any process context)
301 <dt><b>protocol_index</b>
302 <dd>which protocol we are starting from zero
303 </dl>
304 <h3>Description</h3>
305 <blockquote>
306 This structure represents one protocol supported by the server.  An
307 array of these structures is passed to <b>libwebsocket_create_server</b>
308 allows as many protocols as you like to be handled by one server.
309 </blockquote>
310 <hr>