Skip to content

Commit a75d68c

Browse files
committed
samples: net: websocket: App for doing Websocket client requests
This is BSD sockets based application for connecting to Websocket server. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 110a221 commit a75d68c

File tree

8 files changed

+608
-0
lines changed

8 files changed

+608
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
5+
project(http_client)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
set(gen_dir ${ZEPHYR_BINARY_DIR}/include/generated/)
11+
12+
generate_inc_file_for_target(
13+
app
14+
src/https-cert.der
15+
${gen_dir}/https-cert.der.inc
16+
)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
.. _sockets-websocket-client-sample:
2+
3+
Socket Websocket Client
4+
#######################
5+
6+
Overview
7+
********
8+
9+
This sample application implements a Websocket client that will do an HTTP
10+
or HTTPS handshake request to HTTP server, then start to send data and wait for
11+
the responses from the Websocket server.
12+
13+
The source code for this sample application can be found at:
14+
:zephyr_file:`samples/net/sockets/websocket_client`.
15+
16+
Requirements
17+
************
18+
19+
- :ref:`networking_with_host`
20+
21+
Building and Running
22+
********************
23+
24+
You can use this application on a supported board, including
25+
running it inside QEMU as described in :ref:`networking_with_qemu`.
26+
27+
Build websocket-client sample application like this:
28+
29+
.. zephyr-app-commands::
30+
:zephyr-app: samples/net/sockets/websocket_client
31+
:board: <board to use>
32+
:conf: <config file to use>
33+
:goals: build
34+
:compact:
35+
36+
Enabling TLS support
37+
====================
38+
39+
Enable TLS support in the sample by building the project with the
40+
``overlay-tls.conf`` overlay file enabled using these commands:
41+
42+
.. zephyr-app-commands::
43+
:zephyr-app: samples/net/sockets/websocket_client
44+
:board: qemu_x86
45+
:conf: "prj.conf overlay-tls.conf"
46+
:goals: build
47+
:compact:
48+
49+
An alternative way is to specify ``-DOVERLAY_CONFIG=overlay-tls.conf`` when
50+
running ``west build`` or ``cmake``.
51+
52+
The certificate and private key used by the sample can be found in the sample's
53+
:zephyr_file:`samples/net/sockets/websocket_client/src/` directory.
54+
55+
56+
Running websocket-server in Linux Host
57+
======================================
58+
59+
You can run this ``websocket-client`` sample application in QEMU
60+
and run the ``zephyr-websocket-server.py`` (from net-tools) on a Linux host.
61+
Other alternative is to install `websocketd <http://websocketd.com/>`_ and
62+
use that.
63+
64+
To use QEMU for testing, follow the :ref:`networking_with_qemu` guide.
65+
66+
In a terminal window you can do either:
67+
68+
.. code-block:: console
69+
70+
$ ./zephyr-websocket-server.py
71+
72+
or
73+
74+
.. code-block:: console
75+
76+
$ websocketd --port=9001 cat
77+
78+
Run ``websocket-client`` application in QEMU:
79+
80+
.. zephyr-app-commands::
81+
:zephyr-app: samples/net/sockets/websocket_client
82+
:host-os: unix
83+
:board: qemu_x86
84+
:conf: prj.conf
85+
:goals: run
86+
:compact:
87+
88+
Note that ``zephyr-websocket-server.py`` or ``websocketd`` must be running in
89+
the Linux host terminal window before you start the ``websocket-client``
90+
application in QEMU. Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`.
91+
92+
Current version of ``zephyr-websocket-server.py`` found in
93+
`net-tools <https://github.com/zephyrproject-rtos/net-tools>`_ project, does
94+
not support TLS.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_MAIN_STACK_SIZE=3072
2+
CONFIG_NET_BUF_RX_COUNT=80
3+
CONFIG_NET_BUF_TX_COUNT=80
4+
5+
# TLS configuration
6+
CONFIG_MBEDTLS=y
7+
CONFIG_MBEDTLS_BUILTIN=y
8+
CONFIG_MBEDTLS_ENABLE_HEAP=y
9+
CONFIG_MBEDTLS_HEAP_SIZE=60000
10+
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048
11+
12+
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
13+
CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=6
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Networking config
2+
CONFIG_NETWORKING=y
3+
CONFIG_NET_IPV4=y
4+
CONFIG_NET_IPV6=y
5+
CONFIG_NET_TCP=y
6+
CONFIG_NET_SHELL=y
7+
CONFIG_NET_STATISTICS=y
8+
9+
# Sockets
10+
CONFIG_NET_SOCKETS=y
11+
CONFIG_NET_SOCKETS_POSIX_NAMES=y
12+
CONFIG_NET_SOCKETS_POLL_MAX=4
13+
14+
# Network driver config
15+
CONFIG_TEST_RANDOM_GENERATOR=y
16+
17+
# Network address config
18+
CONFIG_NET_CONFIG_SETTINGS=y
19+
CONFIG_NET_CONFIG_NEED_IPV4=y
20+
CONFIG_NET_CONFIG_NEED_IPV6=y
21+
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
22+
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
23+
# Address of HTTP IPv4 server
24+
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
25+
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
26+
# Address of HTTP IPv6 server
27+
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
28+
29+
# HTTP & Websocket
30+
CONFIG_HTTP_CLIENT=y
31+
CONFIG_WEBSOCKET_CLIENT=y
32+
33+
# Network debug config
34+
CONFIG_LOG=y
35+
CONFIG_LOG_IMMEDIATE=y
36+
CONFIG_NET_LOG=y
37+
#CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=y
38+
#CONFIG_NET_HTTP_LOG_LEVEL_DBG=y
39+
#CONFIG_NET_WEBSOCKET_LOG_LEVEL_DBG=y
40+
#CONFIG_NET_CONTEXT_LOG_LEVEL_DBG=y
41+
#CONFIG_NET_TCP_LOG_LEVEL_DBG=y
42+
43+
CONFIG_MAIN_STACK_SIZE=2048
44+
CONFIG_HEAP_MEM_POOL_SIZE=1500
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
common:
2+
tags: net http http_client websocket_client websocket
3+
min_ram: 35
4+
# Blacklist qemu_x86_64 because of SSE compile error, see #19066 for details
5+
platform_exclude: qemu_x86_64
6+
sample:
7+
description: Websocket client sample
8+
name: websocket_client
9+
tests:
10+
sample.net.sockets.websocket_client:
11+
harness: net
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define CA_CERTIFICATE_TAG 1
8+
9+
#define TLS_PEER_HOSTNAME "localhost"
10+
11+
/* This is the same cert as what is found in net-tools/https-cert.pem file
12+
*/
13+
static const unsigned char ca_certificate[] = {
14+
#include "https-cert.der.inc"
15+
};
767 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)