blob: e7dcb825b931399c80239aec1c0f15a46af2e2b0 [file] [log] [blame]
Andy Green4f3943a2010-11-12 10:44:16 +00001<h2>callback - User server actions</h2>
2<i>int</i>
3<b>callback</b>
4(<i>struct libwebsocket *</i> <b>wsi</b>,
5<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
6<i>void *</i> <b>user</b>,
7<i>void *</i> <b>in</b>,
8<i>size_t</i> <b>len</b>)
9<h3>Arguments</h3>
10<dl>
11<dt><b>wsi</b>
12<dd>Opaque websocket instance pointer
13<dt><b>reason</b>
14<dd>The reason for the call
15<dt><b>user</b>
16<dd>Pointer to per-session user data allocated by library
17<dt><b>in</b>
18<dd>Pointer used for some callback reasons
19<dt><b>len</b>
20<dd>Length set for some callback reasons
21</dl>
22<h3>Description</h3>
23<blockquote>
24This callback is the way the user controls what is served. All the
25protocol detail is hidden and handled by the library.
26<p>
27For each connection / session there is user data allocated that is
28pointed to by "user". You set the size of this user data area when
29the library is initialized with libwebsocket_create_server.
30<p>
31You get an opportunity to initialize user data when called back with
32LWS_CALLBACK_ESTABLISHED reason.
33</blockquote>
34<h3>LWS_CALLBACK_ESTABLISHED</h3>
35<blockquote>
36after successful websocket handshake
37</blockquote>
38<h3>LWS_CALLBACK_CLOSED</h3>
39<blockquote>
40when the websocket session ends
41</blockquote>
42<h3>LWS_CALLBACK_SEND</h3>
43<blockquote>
44opportunity to send to client (you would use
45<b>libwebsocket_write</b> taking care about the
46special buffer requirements
47</blockquote>
48<h3>LWS_CALLBACK_RECEIVE</h3>
49<blockquote>
50data has appeared for the server, it can be
51found at *in and is len bytes long
52</blockquote>
53<h3>LWS_CALLBACK_HTTP</h3>
54<blockquote>
55an http request has come from a client that is not
56asking to upgrade the connection to a websocket
57one. This is a chance to serve http content,
58for example, to send a script to the client
59which will then open the websockets connection.
60<tt><b>in</b></tt> points to the URI path requested and
61<b>libwebsockets_serve_http_file</b> makes it very
62simple to send back a file to the client.
63</blockquote>
64<hr>
Andy Green62a12932010-11-03 11:19:23 +000065<h2>libwebsocket_create_server - Create the listening websockets server</h2>
66<i>int</i>
67<b>libwebsocket_create_server</b>
68(<i>int</i> <b>port</b>,
Andy Green4f3943a2010-11-12 10:44:16 +000069<i>const struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Green3faa9c72010-11-08 17:03:03 +000070<i>const char *</i> <b>ssl_cert_filepath</b>,
71<i>const char *</i> <b>ssl_private_key_filepath</b>,
72<i>int</i> <b>gid</b>,
73<i>int</i> <b>uid</b>)
Andy Green62a12932010-11-03 11:19:23 +000074<h3>Arguments</h3>
75<dl>
76<dt><b>port</b>
77<dd>Port to listen on
Andy Green4f3943a2010-11-12 10:44:16 +000078<dt><b>protocols</b>
79<dd>Array of structures listing supported protocols and a protocol-
80specific callback for each one. The list is ended with an
81entry that has a NULL callback pointer.
Andy Green3faa9c72010-11-08 17:03:03 +000082<dt><b>ssl_cert_filepath</b>
83<dd>If libwebsockets was compiled to use ssl, and you want
84to listen using SSL, set to the filepath to fetch the
85server cert from, otherwise NULL for unencrypted
86<dt><b>ssl_private_key_filepath</b>
87<dd>filepath to private key if wanting SSL mode,
88else ignored
89<dt><b>gid</b>
90<dd>group id to change to after setting listen socket, or -1.
91<dt><b>uid</b>
92<dd>user id to change to after setting listen socket, or -1.
Andy Green62a12932010-11-03 11:19:23 +000093</dl>
94<h3>Description</h3>
95<blockquote>
96This function forks to create the listening socket and takes care
97of all initialization in one step.
98<p>
99The callback function is called for a handful of events including
100http requests coming in, websocket connections becoming
101established, and data arriving; it's also called periodically to allow
102async transmission.
103<p>
104The server created is a simple http server by default; part of the
105websocket standard is upgrading this http connection to a websocket one.
106<p>
107This allows the same server to provide files like scripts and favicon /
108images or whatever over http and dynamic data over websockets all in
109one place; they're all handled in the user callback.
110</blockquote>
111<hr>
Andy Green62a12932010-11-03 11:19:23 +0000112<h2>libwebsocket_write - Apply protocol then write data to client</h2>
113<i>int</i>
114<b>libwebsocket_write</b>
115(<i>struct libwebsocket *</i> <b>wsi</b>,
116<i>unsigned char *</i> <b>buf</b>,
117<i>size_t</i> <b>len</b>,
118<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
119<h3>Arguments</h3>
120<dl>
121<dt><b>wsi</b>
122<dd>Websocket instance (available from user callback)
123<dt><b>buf</b>
124<dd>The data to send. For data being sent on a websocket
125connection (ie, not default http), this buffer MUST have
126LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
127and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
128in the buffer after (buf + len). This is so the protocol
129header and trailer data can be added in-situ.
130<dt><b>len</b>
131<dd>Count of the data bytes in the payload starting from buf
132<dt><b>protocol</b>
133<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
134of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
135data on a websockets connection. Remember to allow the extra
136bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
137are used.
138</dl>
139<h3>Description</h3>
140<blockquote>
141This function provides the way to issue data back to the client
142for both http and websocket protocols.
143<p>
144In the case of sending using websocket protocol, be sure to allocate
145valid storage before and after buf as explained above. This scheme
146allows maximum efficiency of sending data and protocol in a single
147packet while not burdening the user code with any protocol knowledge.
148</blockquote>
149<hr>
150<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
151<i>int</i>
152<b>libwebsockets_serve_http_file</b>
153(<i>struct libwebsocket *</i> <b>wsi</b>,
154<i>const char *</i> <b>file</b>,
155<i>const char *</i> <b>content_type</b>)
156<h3>Arguments</h3>
157<dl>
158<dt><b>wsi</b>
159<dd>Websocket instance (available from user callback)
160<dt><b>file</b>
161<dd>The file to issue over http
162<dt><b>content_type</b>
163<dd>The http content type, eg, text/html
164</dl>
165<h3>Description</h3>
166<blockquote>
167This function is intended to be called from the callback in response
168to http requests from the client. It allows the callback to issue
169local files down the http link in a single step.
170</blockquote>
171<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000172<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
173<b>struct libwebsocket_protocols</b> {<br>
174&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
175&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>
176&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
177};<br>
178<h3>Members</h3>
179<dl>
180<dt><b>name</b>
181<dd>Protocol name that must match the one given in the client
182Javascript new WebSocket(url, 'protocol') name
183<dt><b>callback</b>
184<dd>The service callback used for this protocol. It allows the
185service action for an entire protocol to be encapsulated in
186the protocol-specific callback
187<dt><b>per_session_data_size</b>
188<dd>Each new connection using this protocol gets
189this much memory allocated on connection establishment and
190freed on connection takedown. A pointer to this per-connection
191allocation is passed into the callback in the 'user' parameter
192</dl>
193<h3>Description</h3>
194<blockquote>
195This structure represents one protocol supported by the server. An
196array of these structures is passed to <b>libwebsocket_create_server</b>
197allows as many protocols as you like to be handled by one server.
198</blockquote>
199<hr>