1 <h2>libwebsockets_hangup_on_client - Server calls to terminate client connection</h2>
3 <b>libwebsockets_hangup_on_client</b>
4 (<i>struct libwebsocket_context *</i> <b>context</b>,
9 <dd>libwebsockets context
11 <dd>Connection socket descriptor
14 <h2>libwebsockets_get_peer_addresses - Get client address information</h2>
16 <b>libwebsockets_get_peer_addresses</b>
17 (<i>int</i> <b>fd</b>,
18 <i>char *</i> <b>name</b>,
19 <i>int</i> <b>name_len</b>,
20 <i>char *</i> <b>rip</b>,
21 <i>int</i> <b>rip_len</b>)
25 <dd>Connection socket descriptor
27 <dd>Buffer to take client address name
29 <dd>Length of client address name buffer
31 <dd>Buffer to take client address IP qotted quad
33 <dd>Length of client address IP buffer
37 This function fills in <tt><b>name</b></tt> and <tt><b>rip</b></tt> with the name and IP of
38 the client connected with socket descriptor <tt><b>fd</b></tt>. Names may be
39 truncated if there is not enough room. If either cannot be
40 determined, they will be returned as valid zero-length strings.
43 <h2>libwebsocket_service_fd - Service polled socket with something waiting</h2>
45 <b>libwebsocket_service_fd</b>
46 (<i>struct libwebsocket_context *</i> <b>context</b>,
47 <i>struct pollfd *</i> <b>pollfd</b>)
53 <dd>The pollfd entry describing the socket fd and which events
58 This function closes any active connections and then frees the
59 context. After calling this, any further use of the context is
63 <h2>libwebsocket_context_destroy - Destroy the websocket context</h2>
65 <b>libwebsocket_context_destroy</b>
66 (<i>struct libwebsocket_context *</i> <b>context</b>)
74 This function closes any active connections and then frees the
75 context. After calling this, any further use of the context is
79 <h2>libwebsocket_service - Service any pending websocket activity</h2>
81 <b>libwebsocket_service</b>
82 (<i>struct libwebsocket_context *</i> <b>context</b>,
83 <i>int</i> <b>timeout_ms</b>)
89 <dd>Timeout for poll; 0 means return immediately if nothing needed
90 service otherwise block and service immediately, returning
91 after the timeout if nothing needed service.
95 This function deals with any pending websocket traffic, for three
96 kinds of event. It handles these events on both server and client
97 types of connection the same.
99 1) Accept new connections to our context's server
101 2) Perform pending broadcast writes initiated from other forked
102 processes (effectively serializing asynchronous broadcasts)
104 3) Call the receive callback for incoming frame data received by
105 server or client connections.
107 You need to call this service function periodically to all the above
108 functions to happen; if your application is single-threaded you can
109 just call it in your main event loop.
111 Alternatively you can fork a new process that asynchronously handles
112 calling this service in a loop. In that case you are happy if this
113 call blocks your thread until it needs to take care of something and
114 would call it with a large nonzero timeout. Your loop then takes no
115 CPU while there is nothing happening.
117 If you are calling it in a single-threaded app, you don't want it to
118 wait around blocking other things in your loop from happening, so you
119 would call it with a timeout_ms of 0, so it returns immediately if
120 nothing is pending, or as soon as it services whatever was pending.
123 <h2>libwebsocket_callback_on_writable - Request a callback when this socket becomes able to be written to without blocking</h2>
125 <b>libwebsocket_callback_on_writable</b>
126 (<i>struct libwebsocket_context *</i> <b>context</b>,
127 <i>struct libwebsocket *</i> <b>wsi</b>)
131 <dd>libwebsockets context
133 <dd>Websocket connection instance to get callback for
136 <h2>libwebsocket_callback_on_writable_all_protocol - Request a callback for all connections using the given protocol when it becomes possible to write to each socket without blocking in turn.</h2>
138 <b>libwebsocket_callback_on_writable_all_protocol</b>
139 (<i>const struct libwebsocket_protocols *</i> <b>protocol</b>)
143 <dd>Protocol whose connections will get callbacks
146 <h2>libwebsocket_set_timeout - marks the wsi as subject to a timeout</h2>
148 <b>libwebsocket_set_timeout</b>
149 (<i>struct libwebsocket *</i> <b>wsi</b>,
150 <i>enum pending_timeout</i> <b>reason</b>,
151 <i>int</i> <b>secs</b>)
155 <dd>Websocket connection instance
164 You will not need this unless you are doing something special
167 <h2>libwebsocket_get_socket_fd - returns the socket file descriptor</h2>
169 <b>libwebsocket_get_socket_fd</b>
170 (<i>struct libwebsocket *</i> <b>wsi</b>)
174 <dd>Websocket connection instance
179 You will not need this unless you are doing something special
182 <h2>libwebsocket_rx_flow_control - Enable and disable socket servicing for receieved packets.</h2>
184 <b>libwebsocket_rx_flow_control</b>
185 (<i>struct libwebsocket *</i> <b>wsi</b>,
186 <i>int</i> <b>enable</b>)
190 <dd>Websocket connection instance to get callback for
192 <dd>0 = disable read servicing for this connection, 1 = enable
197 If the output side of a server process becomes choked, this allows flow
198 control for the input side.
201 <h2>libwebsocket_canonical_hostname - returns this host's hostname</h2>
203 <b>libwebsocket_canonical_hostname</b>
204 (<i>struct libwebsocket_context *</i> <b>context</b>)
208 <dd>Websocket context
213 This is typically used by client code to fill in the host parameter
214 when making a client connection. You can only call it after the context
218 <h2>libwebsocket_create_context - Create the websocket handler</h2>
219 <i>struct libwebsocket_context *</i>
220 <b>libwebsocket_create_context</b>
221 (<i>int</i> <b>port</b>,
222 <i>const char *</i> <b>interf</b>,
223 <i>struct libwebsocket_protocols *</i> <b>protocols</b>,
224 <i>struct libwebsocket_extension *</i> <b>extensions</b>,
225 <i>const char *</i> <b>ssl_cert_filepath</b>,
226 <i>const char *</i> <b>ssl_private_key_filepath</b>,
227 <i>const char *</i> <b>ssl_ca_filepath</b>,
228 <i>int</i> <b>gid</b>,
229 <i>int</i> <b>uid</b>,
230 <i>unsigned int</i> <b>options</b>,
231 <i>void *</i> <b>user</b>)
235 <dd>Port to listen on... you can use 0 to suppress listening on
236 any port, that's what you want if you are not running a
237 websocket server at all but just using it as a client
239 <dd>NULL to bind the listen socket to all interfaces, or the
240 interface name, eg, "eth2"
242 <dd>Array of structures listing supported protocols and a protocol-
243 specific callback for each one. The list is ended with an
244 entry that has a NULL callback pointer.
245 It's not const because we write the owning_server member
246 <dt><b>extensions</b>
247 <dd>NULL or array of libwebsocket_extension structs listing the
248 extensions this context supports
249 <dt><b>ssl_cert_filepath</b>
250 <dd>If libwebsockets was compiled to use ssl, and you want
251 to listen using SSL, set to the filepath to fetch the
252 server cert from, otherwise NULL for unencrypted
253 <dt><b>ssl_private_key_filepath</b>
254 <dd>filepath to private key if wanting SSL mode,
256 <dt><b>ssl_ca_filepath</b>
257 <dd>CA certificate filepath or NULL
259 <dd>group id to change to after setting listen socket, or -1.
261 <dd>user id to change to after setting listen socket, or -1.
263 <dd>0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
265 <dd>optional user pointer that can be recovered via the context
266 pointer using libwebsocket_context_user
270 This function creates the listening socket and takes care
271 of all initialization in one step.
273 After initialization, it returns a struct libwebsocket_context * that
274 represents this server. After calling, user code needs to take care
275 of calling <b>libwebsocket_service</b> with the context pointer to get the
276 server's sockets serviced. This can be done in the same process context
277 or a forked process, or another thread,
279 The protocol callback functions are called for a handful of events
280 including http requests coming in, websocket connections becoming
281 established, and data arriving; it's also called periodically to allow
284 HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
285 at that time websocket protocol has not been negotiated. Other
286 protocols after the first one never see any HTTP callack activity.
288 The server created is a simple http server by default; part of the
289 websocket standard is upgrading this http connection to a websocket one.
291 This allows the same server to provide files like scripts and favicon /
292 images or whatever over http and dynamic data over websockets all in
293 one place; they're all handled in the user callback.
296 <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>
298 <b>libwebsockets_fork_service_loop</b>
299 (<i>struct libwebsocket_context *</i> <b>context</b>)
303 <dd>server context returned by creation function
306 <h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
307 <i>const struct libwebsocket_protocols *</i>
308 <b>libwebsockets_get_protocol</b>
309 (<i>struct libwebsocket *</i> <b>wsi</b>)
313 <dd>pointer to struct websocket you want to know the protocol of
318 This is useful to get the protocol to broadcast back to from inside
322 <h2>libwebsockets_broadcast - Sends a buffer to the callback for all active connections of the given protocol.</h2>
324 <b>libwebsockets_broadcast</b>
325 (<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
326 <i>unsigned char *</i> <b>buf</b>,
327 <i>size_t</i> <b>len</b>)
331 <dd>pointer to the protocol you will broadcast to all members of
333 <dd>buffer containing the data to be broadcase. NOTE: this has to be
334 allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
335 the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
336 case you are calling this function from callback context.
338 <dd>length of payload data in buf, starting from buf.
342 This function allows bulk sending of a packet to every connection using
343 the given protocol. It does not send the data directly; instead it calls
344 the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
345 wants to actually send the data for that connection, the callback itself
346 should call <b>libwebsocket_write</b>.
348 <b>libwebsockets_broadcast</b> can be called from another fork context without
349 having to take any care about data visibility between the processes, it'll
353 <h2>lws_set_log_level - Set the logging bitfield</h2>
355 <b>lws_set_log_level</b>
356 (<i>int</i> <b>level</b>,
357 <i>void (*</i><b>log_emit_function</b>) <i>(const char *line)</i>)
361 <dd>OR together the LLL_ debug contexts you want output from
362 <dt><b>log_emit_function</b>
363 <dd>NULL to leave it as it is, or a user-supplied
364 function to perform log string emission instead of
365 the default stderr one.
369 log level defaults to "err" and "warn" contexts enabled only and
373 <h2>libwebsocket_write - Apply protocol then write data to client</h2>
375 <b>libwebsocket_write</b>
376 (<i>struct libwebsocket *</i> <b>wsi</b>,
377 <i>unsigned char *</i> <b>buf</b>,
378 <i>size_t</i> <b>len</b>,
379 <i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
383 <dd>Websocket instance (available from user callback)
385 <dd>The data to send. For data being sent on a websocket
386 connection (ie, not default http), this buffer MUST have
387 LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
388 and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
389 in the buffer after (buf + len). This is so the protocol
390 header and trailer data can be added in-situ.
392 <dd>Count of the data bytes in the payload starting from buf
394 <dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
395 of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
396 data on a websockets connection. Remember to allow the extra
397 bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
402 This function provides the way to issue data back to the client
403 for both http and websocket protocols.
405 In the case of sending using websocket protocol, be sure to allocate
406 valid storage before and after buf as explained above. This scheme
407 allows maximum efficiency of sending data and protocol in a single
408 packet while not burdening the user code with any protocol knowledge.
411 <h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
413 <b>libwebsockets_serve_http_file</b>
414 (<i>struct libwebsocket_context *</i> <b>context</b>,
415 <i>struct libwebsocket *</i> <b>wsi</b>,
416 <i>const char *</i> <b>file</b>,
417 <i>const char *</i> <b>content_type</b>)
421 <dd>Websocket instance (available from user callback)
423 <dd>The file to issue over http
424 <dt><b>content_type</b>
425 <dd>The http content type, eg, text/html
429 This function is intended to be called from the callback in response
430 to http requests from the client. It allows the callback to issue
431 local files down the http link in a single step.
434 <h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
436 <b>libwebsockets_remaining_packet_payload</b>
437 (<i>struct libwebsocket *</i> <b>wsi</b>)
441 <dd>Websocket instance (available from user callback)
445 This function is intended to be called from the callback if the
446 user code is interested in "complete packets" from the client.
447 libwebsockets just passes through payload as it comes and issues a buffer
448 additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
449 callback handler can use this API to find out if the buffer it has just
450 been given is the last piece of a "complete packet" from the client --
451 when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
454 Many protocols won't care becuse their packets are always small.
457 <h2>libwebsocket_client_connect - Connect to another websocket server</h2>
458 <i>struct libwebsocket *</i>
459 <b>libwebsocket_client_connect</b>
460 (<i>struct libwebsocket_context *</i> <b>context</b>,
461 <i>const char *</i> <b>address</b>,
462 <i>int</i> <b>port</b>,
463 <i>int</i> <b>ssl_connection</b>,
464 <i>const char *</i> <b>path</b>,
465 <i>const char *</i> <b>host</b>,
466 <i>const char *</i> <b>origin</b>,
467 <i>const char *</i> <b>protocol</b>,
468 <i>int</i> <b>ietf_version_or_minus_one</b>)
472 <dd>Websocket context
474 <dd>Remote server address, eg, "myserver.com"
476 <dd>Port to connect to on the remote server, eg, 80
477 <dt><b>ssl_connection</b>
478 <dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
481 <dd>Websocket path on server
483 <dd>Hostname on server
485 <dd>Socket origin name
487 <dd>Comma-separated list of protocols being asked for from
488 the server, or just one. The server will pick the one it
490 <dt><b>ietf_version_or_minus_one</b>
491 <dd>-1 to ask to connect using the default, latest
492 protocol supported, or the specific protocol ordinal
496 This function creates a connection to a remote server
499 <h2>libwebsocket_client_connect_extended - Connect to another websocket server</h2>
500 <i>struct libwebsocket *</i>
501 <b>libwebsocket_client_connect_extended</b>
502 (<i>struct libwebsocket_context *</i> <b>context</b>,
503 <i>const char *</i> <b>address</b>,
504 <i>int</i> <b>port</b>,
505 <i>int</i> <b>ssl_connection</b>,
506 <i>const char *</i> <b>path</b>,
507 <i>const char *</i> <b>host</b>,
508 <i>const char *</i> <b>origin</b>,
509 <i>const char *</i> <b>protocol</b>,
510 <i>int</i> <b>ietf_version_or_minus_one</b>,
511 <i>void *</i> <b>userdata</b>)
515 <dd>Websocket context
517 <dd>Remote server address, eg, "myserver.com"
519 <dd>Port to connect to on the remote server, eg, 80
520 <dt><b>ssl_connection</b>
521 <dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
524 <dd>Websocket path on server
526 <dd>Hostname on server
528 <dd>Socket origin name
530 <dd>Comma-separated list of protocols being asked for from
531 the server, or just one. The server will pick the one it
533 <dt><b>ietf_version_or_minus_one</b>
534 <dd>-1 to ask to connect using the default, latest
535 protocol supported, or the specific protocol ordinal
537 <dd>Pre-allocated user data
541 This function creates a connection to a remote server
544 <h2>callback - User server actions</h2>
545 <i>LWS_EXTERN int</i>
547 (<i>struct libwebsocket_context *</i> <b>context</b>,
548 <i>struct libwebsocket *</i> <b>wsi</b>,
549 <i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
550 <i>void *</i> <b>user</b>,
551 <i>void *</i> <b>in</b>,
552 <i>size_t</i> <b>len</b>)
556 <dd>Websockets context
558 <dd>Opaque websocket instance pointer
560 <dd>The reason for the call
562 <dd>Pointer to per-session user data allocated by library
564 <dd>Pointer used for some callback reasons
566 <dd>Length set for some callback reasons
570 This callback is the way the user controls what is served. All the
571 protocol detail is hidden and handled by the library.
573 For each connection / session there is user data allocated that is
574 pointed to by "user". You set the size of this user data area when
575 the library is initialized with libwebsocket_create_server.
577 You get an opportunity to initialize user data when called back with
578 LWS_CALLBACK_ESTABLISHED reason.
580 <h3>LWS_CALLBACK_ESTABLISHED</h3>
582 after the server completes a handshake with
585 <h3>LWS_CALLBACK_CLIENT_CONNECTION_ERROR</h3>
587 the request client connection has
588 been unable to complete a handshake with the remote server
590 <h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
592 after your client connection completed
593 a handshake with the remote server
595 <h3>LWS_CALLBACK_CLOSED</h3>
597 when the websocket session ends
599 <h3>LWS_CALLBACK_BROADCAST</h3>
601 signal to send to client (you would use
602 <b>libwebsocket_write</b> taking care about the
603 special buffer requirements
605 <h3>LWS_CALLBACK_RECEIVE</h3>
607 data has appeared for this server endpoint from a
608 remote client, it can be found at *in and is
611 <h3>LWS_CALLBACK_CLIENT_RECEIVE_PONG</h3>
613 if you elected to see PONG packets,
614 they appear with this callback reason. PONG
615 packets only exist in 04+ protocol
617 <h3>LWS_CALLBACK_CLIENT_RECEIVE</h3>
619 data has appeared from the server for the
620 client connection, it can be found at *in and
623 <h3>LWS_CALLBACK_HTTP</h3>
625 an http request has come from a client that is not
626 asking to upgrade the connection to a websocket
627 one. This is a chance to serve http content,
628 for example, to send a script to the client
629 which will then open the websockets connection.
630 <tt><b>in</b></tt> points to the URI path requested and
631 <b>libwebsockets_serve_http_file</b> makes it very
632 simple to send back a file to the client.
633 Normally after sending the file you are done
634 with the http connection, since the rest of the
635 activity will come by websockets from the script
636 that was delivered by http, so you will want to
637 return 1; to close and free up the connection.
638 That's important because it uses a slot in the
639 total number of client connections allowed set
642 <h3>LWS_CALLBACK_HTTP_FILE_COMPLETION</h3>
644 a file requested to be send down
645 http link has completed.
647 <h3>LWS_CALLBACK_SERVER_WRITEABLE</h3>
650 <b>libwebsocket_callback_on_writable</b> on a connection, you will
651 get one of these callbacks coming when the connection socket
652 is able to accept another write packet without blocking.
653 If it already was able to take another packet without blocking,
654 you'll get this callback at the next call to the service loop
655 function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
656 and servers get LWS_CALLBACK_SERVER_WRITEABLE.
658 <h3>LWS_CALLBACK_FILTER_NETWORK_CONNECTION</h3>
660 called when a client connects to
661 the server at network level; the connection is accepted but then
662 passed to this callback to decide whether to hang up immediately
663 or not, based on the client IP. <tt><b>user</b></tt> contains the connection
664 socket's descriptor. Return non-zero to terminate
665 the connection before sending or receiving anything.
666 Because this happens immediately after the network connection
667 from the client, there's no websocket protocol selected yet so
668 this callback is issued only to protocol 0.
670 <h3>LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION</h3>
672 called when the handshake has
673 been received and parsed from the client, but the response is
674 not sent yet. Return non-zero to disallow the connection.
675 <tt><b>user</b></tt> is a pointer to an array of struct lws_tokens, you can
676 use the header enums lws_token_indexes from libwebsockets.h
677 to check for and read the supported header presence and
678 content before deciding to allow the handshake to proceed or
679 to kill the connection.
681 <h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS</h3>
684 including OpenSSL support, this callback allows your user code
685 to perform extra <b>SSL_CTX_load_verify_locations</b> or similar
686 calls to direct OpenSSL where to find certificates the client
687 can use to confirm the remote server identity. <tt><b>user</b></tt> is the
690 <h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS</h3>
693 including OpenSSL support, this callback allows your user code
694 to load extra certifcates into the server which allow it to
695 verify the validity of certificates returned by clients. <tt><b>user</b></tt>
696 is the server's OpenSSL SSL_CTX*
698 <h3>LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION</h3>
701 libwebsockets context was created with the option
702 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
703 callback is generated during OpenSSL verification of the cert
704 sent from the client. It is sent to protocol[0] callback as
705 no protocol has been negotiated on the connection yet.
706 Notice that the libwebsockets context and wsi are both NULL
707 during this callback. See
711 //www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
712 to understand more detail about the OpenSSL callback that
713 generates this libwebsockets callback and the meanings of the
714 arguments passed. In this callback, <tt><b>user</b></tt> is the x509_ctx,
715 <tt><b>in</b></tt> is the ssl pointer and <tt><b>len</b></tt> is preverify_ok
716 Notice that this callback maintains libwebsocket return
717 conventions, return 0 to mean the cert is OK or 1 to fail it.
718 This also means that if you don't handle this callback then
719 the default callback action of returning 0 allows the client
722 <h3>LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER</h3>
724 this callback happens
725 when a client handshake is being compiled. <tt><b>user</b></tt> is NULL,
726 <tt><b>in</b></tt> is a char **, it's pointing to a char * which holds the
727 next location in the header buffer where you can add
728 headers, and <tt><b>len</b></tt> is the remaining space in the header buffer,
729 which is typically some hundreds of bytes. So, to add a canned
730 cookie, your handler code might look similar to:
732 char **p = (char **)in;
737 *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
741 Notice if you add anything, you just have to take care about
742 the CRLF on the line you added. Obviously this callback is
743 optional, if you don't handle it everything is fine.
745 Notice the callback is coming to protocols[0] all the time,
746 because there is no specific protocol handshook yet.
748 <h3>LWS_CALLBACK_CONFIRM_EXTENSION_OKAY</h3>
750 When the server handshake code
751 sees that it does support a requested extension, before
752 accepting the extension by additing to the list sent back to
753 the client it gives this callback just to check that it's okay
754 to use that extension. It calls back to the requested protocol
755 and with <tt><b>in</b></tt> being the extension name, <tt><b>len</b></tt> is 0 and <tt><b>user</b></tt> is
756 valid. Note though at this time the ESTABLISHED callback hasn't
757 happened yet so if you initialize <tt><b>user</b></tt> content there, <tt><b>user</b></tt>
758 content during this callback might not be useful for anything.
759 Notice this callback comes to protocols[0].
761 <h3>LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED</h3>
764 connection is being prepared to start a handshake to a server,
765 each supported extension is checked with protocols[0] callback
766 with this reason, giving the user code a chance to suppress the
767 claim to support that extension by returning non-zero. If
768 unhandled, by default 0 will be returned and the extension
769 support included in the header to the server. Notice this
770 callback comes to protocols[0].
772 The next four reasons are optional and only need taking care of if you
773 will be integrating libwebsockets sockets into an external polling
776 <h3>LWS_CALLBACK_ADD_POLL_FD</h3>
778 libwebsocket deals with its <b>poll</b> loop
779 internally, but in the case you are integrating with another
780 server you will need to have libwebsocket sockets share a
781 polling array with the other server. This and the other
782 POLL_FD related callbacks let you put your specialized
783 poll array interface code in the callback for protocol 0, the
784 first protocol you support, usually the HTTP protocol in the
785 serving case. This callback happens when a socket needs to be
787 <h3>added to the polling loop</h3>
789 <tt><b>user</b></tt> contains the fd, and
790 <tt><b>len</b></tt> is the events bitmap (like, POLLIN). If you are using the
791 internal polling loop (the "service" callback), you can just
792 ignore these callbacks.
794 <h3>LWS_CALLBACK_DEL_POLL_FD</h3>
796 This callback happens when a socket descriptor
797 needs to be removed from an external polling array. <tt><b>user</b></tt> is
798 the socket desricptor. If you are using the internal polling
799 loop, you can just ignore it.
801 <h3>LWS_CALLBACK_SET_MODE_POLL_FD</h3>
803 This callback happens when libwebsockets
804 wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
805 The handler should OR <tt><b>len</b></tt> on to the events member of the pollfd
806 struct for this socket descriptor. If you are using the
807 internal polling loop, you can just ignore it.
809 <h3>LWS_CALLBACK_CLEAR_MODE_POLL_FD</h3>
811 This callback occurs when libwebsockets
812 wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
813 The handler should AND ~<tt><b>len</b></tt> on to the events member of the
814 pollfd struct for this socket descriptor. If you are using the
815 internal polling loop, you can just ignore it.
818 <h2>extension_callback - Hooks to allow extensions to operate</h2>
819 <i>LWS_EXTERN int</i>
820 <b>extension_callback</b>
821 (<i>struct libwebsocket_context *</i> <b>context</b>,
822 <i>struct libwebsocket_extension *</i> <b>ext</b>,
823 <i>struct libwebsocket *</i> <b>wsi</b>,
824 <i>enum libwebsocket_extension_callback_reasons</i> <b>reason</b>,
825 <i>void *</i> <b>user</b>,
826 <i>void *</i> <b>in</b>,
827 <i>size_t</i> <b>len</b>)
831 <dd>Websockets context
835 <dd>Opaque websocket instance pointer
837 <dd>The reason for the call
839 <dd>Pointer to per-session user data allocated by library
841 <dd>Pointer used for some callback reasons
843 <dd>Length set for some callback reasons
847 Each extension that is active on a particular connection receives
848 callbacks during the connection lifetime to allow the extension to
849 operate on websocket data and manage itself.
851 Libwebsockets takes care of allocating and freeing "user" memory for
852 each active extension on each connection. That is what is pointed to
853 by the <tt><b>user</b></tt> parameter.
855 <h3>LWS_EXT_CALLBACK_CONSTRUCT</h3>
857 called when the server has decided to
858 select this extension from the list provided by the client,
859 just before the server will send back the handshake accepting
860 the connection with this extension active. This gives the
861 extension a chance to initialize its connection context found
862 in <tt><b>user</b></tt>.
864 <h3>LWS_EXT_CALLBACK_CLIENT_CONSTRUCT</h3>
866 same as LWS_EXT_CALLBACK_CONSTRUCT
867 but called when client is instantiating this extension. Some
868 extensions will work the same on client and server side and then
869 you can just merge handlers for both CONSTRUCTS.
871 <h3>LWS_EXT_CALLBACK_DESTROY</h3>
873 called when the connection the extension was
874 being used on is about to be closed and deallocated. It's the
875 last chance for the extension to deallocate anything it has
876 allocated in the user data (pointed to by <tt><b>user</b></tt>) before the
877 user data is deleted. This same callback is used whether you
878 are in client or server instantiation context.
880 <h3>LWS_EXT_CALLBACK_PACKET_RX_PREPARSE</h3>
882 when this extension was active on
883 a connection, and a packet of data arrived at the connection,
884 it is passed to this callback to give the extension a chance to
885 change the data, eg, decompress it. <tt><b>user</b></tt> is pointing to the
886 extension's private connection context data, <tt><b>in</b></tt> is pointing
887 to an lws_tokens struct, it consists of a char * pointer called
888 token, and an int called token_len. At entry, these are
889 set to point to the received buffer and set to the content
890 length. If the extension will grow the content, it should use
891 a new buffer allocated in its private user context data and
892 set the pointed-to lws_tokens members to point to its buffer.
894 <h3>LWS_EXT_CALLBACK_PACKET_TX_PRESEND</h3>
896 this works the same way as
897 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
898 extension a chance to change websocket data just before it will
899 be sent out. Using the same lws_token pointer scheme in <tt><b>in</b></tt>,
900 the extension can change the buffer and the length to be
901 transmitted how it likes. Again if it wants to grow the
902 buffer safely, it should copy the data into its own buffer and
903 set the lws_tokens token pointer to it.
906 <h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
907 <b>struct libwebsocket_protocols</b> {<br>
908 <i>const char *</i> <b>name</b>;<br>
909 <i>callback_function *</i> <b>callback</b>;<br>
910 <i>size_t</i> <b>per_session_data_size</b>;<br>
911 <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
912 <i>int</i> <b>broadcast_socket_port</b>;<br>
913 <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
914 <i>int</i> <b>protocol_index</b>;<br>
919 <dd>Protocol name that must match the one given in the client
920 Javascript new WebSocket(url, 'protocol') name
922 <dd>The service callback used for this protocol. It allows the
923 service action for an entire protocol to be encapsulated in
924 the protocol-specific callback
925 <dt><b>per_session_data_size</b>
926 <dd>Each new connection using this protocol gets
927 this much memory allocated on connection establishment and
928 freed on connection takedown. A pointer to this per-connection
929 allocation is passed into the callback in the 'user' parameter
930 <dt><b>owning_server</b>
931 <dd>the server init call fills in this opaque pointer when
932 registering this protocol with the server.
933 <dt><b>broadcast_socket_port</b>
934 <dd>the server init call fills this in with the
935 localhost port number used to forward broadcasts for this
937 <dt><b>broadcast_socket_user_fd</b>
938 <dd>the server init call fills this in ... the <b>main</b>
939 process context can write to this socket to perform broadcasts
940 (use the <b>libwebsockets_broadcast</b> api to do this instead,
941 it works from any process context)
942 <dt><b>protocol_index</b>
943 <dd>which protocol we are starting from zero
947 This structure represents one protocol supported by the server. An
948 array of these structures is passed to <b>libwebsocket_create_server</b>
949 allows as many protocols as you like to be handled by one server.
952 <h2>struct libwebsocket_extension - An extension we know how to cope with</h2>
953 <b>struct libwebsocket_extension</b> {<br>
954 <i>const char *</i> <b>name</b>;<br>
955 <i>extension_callback_function *</i> <b>callback</b>;<br>
956 <i>size_t</i> <b>per_session_data_size</b>;<br>
957 <i>void *</i> <b>per_context_private_data</b>;<br>
962 <dd>Formal extension name, eg, "deflate-stream"
965 <dt><b>per_session_data_size</b>
966 <dd>Libwebsockets will auto-malloc this much
967 memory for the use of the extension, a pointer
968 to it comes in the <tt><b>user</b></tt> callback parameter
969 <dt><b>per_context_private_data</b>
970 <dd>Optional storage for this externsion that
971 is per-context, so it can track stuff across
972 all sessions, etc, if it wants