canonical libwebsockets.org websocket library
Find a file
Joakim Soderberg 4c53123677 CMake support + fixed windows build.
- Finalized CMake support (tested on windows only so far).
  - Uses a generated lws_config.h that is included in
  private-libwebsocket to pass defines, only used if CMAKE_BUILD is set.
  - Support for SSL on Windows.
  - Initial support for CyaSSL replacement of OpenSSL (This has been added
    to my older CMake-fork but haven't been tested on this version yet).
- Fixed windows build (see below for details).
- Fixed at least the 32-bit Debug build for the existing Visual Studio
  Project. (Not to keen fixing all the others when we have CMake support
  anyway (which can generate much better project files)...)
- BUGFIXES:
  - handshake.c
    - used C99 definition of handshake_0405 function
  - libwebsocket.c
    - syslog not available on windows, put in ifdefs.
    - Fixed previous known crash bug on Windows where WSAPoll in
      Ws2_32.dll would not be present, causing the poll function pointer
      being set to NULL.
    - Uninitialized variable context->listen_service_extraseen would
      result in stack overflow because of infinite recursion. Fixed by
      initializing in libwebsocket_create_context
    - SO_REUSADDR means something different on Windows compared to Unix.
    - Setting a socket to nonblocking is done differently on Windows.
      (This should probably broken out into a helper function instead)
    - lwsl_emit_syslog -> lwsl_emit_stderr on Windows.
  - private-libwebsocket.h
    - PATH_MAX is not available on Windows, define as MAX_PATH
    - Always define LWS_NO_DAEMONIZE on windows.
    - Don't define lws_latency as inline that does nothing. inline is not
      support by the Microsoft compiler, replaced with an empty define
      instead. (It's __inline in MSVC)
  - server.c
    - Fixed nonblock call on windows
  - test-ping.c
    - Don't use C99 features (Microsoft compiler does not support it).
    - Move non-win32 headers into ifdefs.
    - Skip use of sighandler on Windows.
  - test-server.c
    - ifdef syslog parts on Windows.
2013-02-06 15:49:12 +09:00
cmake CMake support + fixed windows build. 2013-02-06 15:49:12 +09:00
lib CMake support + fixed windows build. 2013-02-06 15:49:12 +09:00
m4 add empty m4 dir as workaround for autoreconf issue 2013-01-15 21:56:12 +08:00
scripts refactor-into-dirs.patch 2010-11-01 09:12:17 +00:00
test-server CMake support + fixed windows build. 2013-02-06 15:49:12 +09:00
win32port CMake support + fixed windows build. 2013-02-06 15:49:12 +09:00
.gitignore Ignoring linux build files 2013-01-09 15:46:11 +08:00
autogen.sh use autogen.sh 2012-10-06 15:17:01 +08:00
changelog introduce library version plus git hash 2013-02-01 10:50:15 +08:00
CMakeLists.txt CMake support + fixed windows build. 2013-02-06 15:49:12 +09:00
config.h.cmake CMake support + fixed windows build. 2013-02-06 15:49:12 +09:00
configure.ac introduce library version plus git hash 2013-02-01 10:50:15 +08:00
COPYING add-COPYING-lgpl-2.1.patch 2010-11-08 17:12:19 +00:00
INSTALL add-INSTALL.patch 2010-11-12 13:11:50 +00:00
libwebsockets-api-doc.html remove one more mention of broadcast callback 2013-02-01 11:17:02 +08:00
libwebsockets.pc.in add pkg-config file libwebsockets.pc 2012-10-19 18:26:22 +08:00
libwebsockets.spec uplevel-rpm-spec-to-include-new-extpoll-sample.patch 2011-02-13 09:08:11 +00:00
Makefile.am introduce library version plus git hash 2013-02-01 10:50:15 +08:00
README.build add note about MIPS opewrt configure options 2013-02-02 18:10:29 +08:00
README.coding additional casts allow test server build as cpp 2013-02-01 08:42:15 +08:00
README.test-apps introduce test echo 2013-01-30 12:28:44 +08:00

Testing server with a browser
-----------------------------

If you run libwebsockets-test-server and point your browser
(eg, Chrome) to

  http://127.0.0.1:7681

It will fetch a script in the form of test.html, and then run the
script in there on the browser to open a websocket connection.
Incrementing numbers should appear in the browser display.

By default the test server logs to both stderr and syslog, you can control
what is logged using -d <log level>, see later.


Running test server as a Daemon
-------------------------------

You can use the -D option on the test server to have it fork into the
background and return immediately.  In this daemonized mode all stderr is
disabled and logging goes only to syslog, eg, /var/log/messages or similar.

The server maintains a lockfile at /tmp/.lwsts-lock that contains the pid
of the master process, and deletes this file when the master process
terminates.

To stop the daemon, do

  kill `cat /tmp/.lwsts-lock`

If it finds a stale lock (the pid mentioned in the file does not exist
any more) it will delete the lock and create a new one during startup.

If the lock is valid, the daemon will exit with a note on stderr that
it was already running.s


Using SSL on the server side
----------------------------

To test it using SSL/WSS, just run the test server with

$ libwebsockets-test-server --ssl

and use the URL

  https://127.0.0.1:7681

The connection will be entirely encrypted using some generated
certificates that your browser will not accept, since they are
not signed by any real Certificate Authority.  Just accept the
certificates in the browser and the connection will proceed
in first https and then websocket wss, acting exactly the
same.

test-server.c is all that is needed to use libwebsockets for
serving both the script html over http and websockets.


Testing websocket client support
--------------------------------

If you run the test server as described above, you can also
connect to it using the test client as well as a browser.

$ libwebsockets-test-client localhost

will by default connect to the test server on localhost:7681
and print the dumb increment number from the server at the
same time as drawing random circles in the mirror protocol;
if you connect to the test server using a browser at the
same time you will be able to see the circles being drawn.


Testing simple echo
-------------------

You can test against echo.websockets.org as a sanity test like
this (the client connects to port 80 by default):

$ libwebsockets-test-echo --client echo.websocket.org

This echo test is of limited use though because it doesn't
negotiate any protocol.  You can run the same test app as a
local server, by default on localhost:7681

$ libwebsockets-test-echo

and do the echo test against the local echo server

$ libwebsockets-test-echo --client localhost --port 7681

If you add the --ssl switch to both the client and server, you can also test
with an encrypted link.


Testing SSL on the client side
------------------------------

To test SSL/WSS client action, just run the client test with

$ libwebsockets-test-client localhost --ssl

By default the client test applet is set to accept selfsigned
certificates used by the test server, this is indicated by the
use_ssl var being set to 2.  Set it to 1 to reject any server
certificate that it doesn't have a trusted CA cert for.


Using the websocket ping utility
--------------------------------

libwebsockets-test-ping connects as a client to a remote
websocket server using 04 protocol and pings it like the
normal unix ping utility.

$ libwebsockets-test-ping localhost                
handshake OK for protocol lws-mirror-protocol
Websocket PING localhost.localdomain (127.0.0.1) 64 bytes of data.
64 bytes from localhost: req=1 time=0.1ms
64 bytes from localhost: req=2 time=0.1ms
64 bytes from localhost: req=3 time=0.1ms
64 bytes from localhost: req=4 time=0.2ms
64 bytes from localhost: req=5 time=0.1ms
64 bytes from localhost: req=6 time=0.2ms
64 bytes from localhost: req=7 time=0.2ms
64 bytes from localhost: req=8 time=0.1ms
^C
--- localhost.localdomain websocket ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7458ms
rtt min/avg/max = 0.110/0.185/0.218 ms
$

By default it sends 64 byte payload packets using the 04
PING packet opcode type.  You can change the payload size
using the -s= flag, up to a maximum of 125 mandated by the
04 standard.

Using the lws-mirror protocol that is provided by the test
server, libwebsockets-test-ping can also use larger payload
sizes up to 4096 is BINARY packets; lws-mirror will copy
them back to the client and they appear as a PONG.  Use the
-m flag to select this operation.

The default interval between pings is 1s, you can use the -i=
flag to set this, including fractions like -i=0.01 for 10ms
interval.

Before you can even use the PING opcode that is part of the
standard, you must complete a handshake with a specified
protocol.  By default lws-mirror-protocol is used which is
supported by the test server.  But if you are using it on
another server, you can specify the protcol to handshake with
by --protocol=protocolname


Fraggle test app
----------------

By default it runs in server mode

$ libwebsockets-test-fraggle
libwebsockets test fraggle
(C) Copyright 2010-2011 Andy Green <andy@warmcat.com> licensed under LGPL2.1
 Compiled with SSL support, not using it
 Listening on port 7681
server sees client connect
accepted v06 connection
Spamming 360 random fragments
Spamming session over, len = 371913. sum = 0x2D3C0AE
Spamming 895 random fragments
Spamming session over, len = 875970. sum = 0x6A74DA1
...

You need to run a second session in client mode, you have to
give the -c switch and the server address at least:

$ libwebsockets-test-fraggle -c localhost 
libwebsockets test fraggle
(C) Copyright 2010-2011 Andy Green <andy@warmcat.com> licensed under LGPL2.1
 Client mode
Connecting to localhost:7681
denied deflate-stream extension
handshake OK for protocol fraggle-protocol
client connects to server
EOM received 371913 correctly from 360 fragments
EOM received 875970 correctly from 895 fragments
EOM received 247140 correctly from 258 fragments
EOM received 695451 correctly from 692 fragments
...

The fraggle test sends a random number up to 1024 fragmented websocket frames
each of a random size between 1 and 2001 bytes in a single message, then sends
a checksum and starts sending a new randomly sized and fragmented message.

The fraggle test client receives the same message fragments and computes the
same checksum using websocket framing to see when the message has ended.  It
then accepts the server checksum message and compares that to its checksum.


proxy support
-------------

The http_proxy environment variable is respected by the client
connection code for both ws:// and wss://.  It doesn't support
authentication.

You use it like this

export http_proxy=myproxy.com:3128
libwebsockets-test-client someserver.com


debug logging
-------------

By default logging of severity "notice", "warn" or "err" is enabled to stderr.

Again by default other logging is comiled in but disabled from printing.

If you want to eliminate the debug logging below notice  in severity, use the
--disable-debug configure option to have it removed from the code by the
preprocesser.

If you want to see more detailed debug logs, you can control a bitfield to
select which logs types may print using the lws_set_log_level() api, in the
test apps you can use -d <number> to control this.  The types of logging
available are (OR together the numbers to select multiple)

 1   ERR
 2   WARN
 4   NOTICE
 8   INFO
 16  DEBUG
 32  PARSER
 64  HEADER
 128 EXTENSION
 256 CLIENT
 512 LATENCY


Websocket version supported
---------------------------

The final IETF standard is supported for both client and server, protocol
version 13.


Latency Tracking
----------------

Since libwebsockets runs using poll() and a single threaded approach, any
unexpected latency coming from system calls would be bad news.  There's now
a latency tracking scheme that can be built in with --with-latency at
configure-time, logging the time taken for system calls to complete and if
the whole action did complete that time or was deferred.

You can see the detailed data by enabling logging level 512 (eg, -d 519 on
the test server to see that and the usual logs), however even without that
the "worst" latency is kept and reported to the logs with NOTICE severity
when the context is destroyed.

Some care is needed interpreting them, if the action completed the first figure
(in us) is the time taken for the whole action, which may have retried through
the poll loop many times and will depend on network roundtrip times.  High
figures here don't indicate a problem.  The figure in us reported after "lat"
in the logging is the time taken by this particular attempt.  High figures
here may indicate a problem, or if you system is loaded with another app at
that time, such as the browser, it may simply indicate the OS gave preferential
treatment to the other app during that call.