blob: 0c2d8eb4c61cb7f220deec600f95442e80bbf3e1 [file] [log] [blame]
Andy Green62a12932010-11-03 11:19:23 +00001<h2>libwebsocket_create_server - Create the listening websockets server</h2>
2<i>int</i>
3<b>libwebsocket_create_server</b>
4(<i>int</i> <b>port</b>,
5<i>int (*</i><b>callback</b>) <i>(struct libwebsocket *, enum libwebsocket_callback_reasons, void *, void *, size_t)</i>,
6<i>int</i> <b>protocol</b>,
Andy Green3faa9c72010-11-08 17:03:03 +00007<i>size_t</i> <b>user_area_size</b>,
8<i>const char *</i> <b>ssl_cert_filepath</b>,
9<i>const char *</i> <b>ssl_private_key_filepath</b>,
10<i>int</i> <b>gid</b>,
11<i>int</i> <b>uid</b>)
Andy Green62a12932010-11-03 11:19:23 +000012<h3>Arguments</h3>
13<dl>
14<dt><b>port</b>
15<dd>Port to listen on
16<dt><b>callback</b>
17<dd>The callback in user code to perform actual serving
18<dt><b>protocol</b>
19<dd>Which version of the websockets protocol (currently 76)
20<dt><b>user_area_size</b>
21<dd>How much memory to allocate per connection session
22which will be used by the user application to store
23per-session data. A pointer to this space is given
24when the user callback is called.
Andy Green3faa9c72010-11-08 17:03:03 +000025<dt><b>ssl_cert_filepath</b>
26<dd>If libwebsockets was compiled to use ssl, and you want
27to listen using SSL, set to the filepath to fetch the
28server cert from, otherwise NULL for unencrypted
29<dt><b>ssl_private_key_filepath</b>
30<dd>filepath to private key if wanting SSL mode,
31else ignored
32<dt><b>gid</b>
33<dd>group id to change to after setting listen socket, or -1.
34<dt><b>uid</b>
35<dd>user id to change to after setting listen socket, or -1.
Andy Green62a12932010-11-03 11:19:23 +000036</dl>
37<h3>Description</h3>
38<blockquote>
39This function forks to create the listening socket and takes care
40of all initialization in one step.
41<p>
42The callback function is called for a handful of events including
43http requests coming in, websocket connections becoming
44established, and data arriving; it's also called periodically to allow
45async transmission.
46<p>
47The server created is a simple http server by default; part of the
48websocket standard is upgrading this http connection to a websocket one.
49<p>
50This allows the same server to provide files like scripts and favicon /
51images or whatever over http and dynamic data over websockets all in
52one place; they're all handled in the user callback.
53</blockquote>
54<hr>
55<h2>libwebsocket_get_uri - Return the URI path being requested</h2>
56<i>const char *</i>
57<b>libwebsocket_get_uri</b>
58(<i>struct libwebsocket *</i> <b>wsi</b>)
59<h3>Arguments</h3>
60<dl>
61<dt><b>wsi</b>
62<dd>Websocket instance
63</dl>
64<h3>Description</h3>
65<blockquote>
66The user code can find out the local path being opened from this
67call, it's valid on HTTP or established websocket connections.
68If the client opened the connection with "http://127.0.0.1/xyz/abc.d"
69then this call will return a pointer to "/xyz/abc.d"
70</blockquote>
71<hr>
72<h2>libwebsocket_write - Apply protocol then write data to client</h2>
73<i>int</i>
74<b>libwebsocket_write</b>
75(<i>struct libwebsocket *</i> <b>wsi</b>,
76<i>unsigned char *</i> <b>buf</b>,
77<i>size_t</i> <b>len</b>,
78<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
79<h3>Arguments</h3>
80<dl>
81<dt><b>wsi</b>
82<dd>Websocket instance (available from user callback)
83<dt><b>buf</b>
84<dd>The data to send. For data being sent on a websocket
85connection (ie, not default http), this buffer MUST have
86LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
87and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
88in the buffer after (buf + len). This is so the protocol
89header and trailer data can be added in-situ.
90<dt><b>len</b>
91<dd>Count of the data bytes in the payload starting from buf
92<dt><b>protocol</b>
93<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
94of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
95data on a websockets connection. Remember to allow the extra
96bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
97are used.
98</dl>
99<h3>Description</h3>
100<blockquote>
101This function provides the way to issue data back to the client
102for both http and websocket protocols.
103<p>
104In the case of sending using websocket protocol, be sure to allocate
105valid storage before and after buf as explained above. This scheme
106allows maximum efficiency of sending data and protocol in a single
107packet while not burdening the user code with any protocol knowledge.
108</blockquote>
109<hr>
110<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
111<i>int</i>
112<b>libwebsockets_serve_http_file</b>
113(<i>struct libwebsocket *</i> <b>wsi</b>,
114<i>const char *</i> <b>file</b>,
115<i>const char *</i> <b>content_type</b>)
116<h3>Arguments</h3>
117<dl>
118<dt><b>wsi</b>
119<dd>Websocket instance (available from user callback)
120<dt><b>file</b>
121<dd>The file to issue over http
122<dt><b>content_type</b>
123<dd>The http content type, eg, text/html
124</dl>
125<h3>Description</h3>
126<blockquote>
127This function is intended to be called from the callback in response
128to http requests from the client. It allows the callback to issue
129local files down the http link in a single step.
130</blockquote>
131<hr>