connectivity/networking/api/websocket.html · connectivity/networking/api/websocket.rst 공식 원문 ↗

Zephyr 3.7.0 · Connectivity · Networking API

WebSocket Client API

연결된 TCP socket을 WebSocket transport로 감싸 application data나 MQTT를 전달합니다.

Official pathconnectivity/networking/api/websocket.html
Source fileconnectivity/networking/api/websocket.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

RFC 6455 연결, MQTT transport 설정, TEXT/BINARY 전송 차이와 wrapper·TCP socket 수명주기를 설명합니다.

Protocol

RFC 6455

Transport

MQTT · Application

Binary

websocket_send_msg

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 89812C99072B7FDE3FB3A794890C9157862DC2A12F98224F4569D1218B20727D
.. _websocket_interface:

Websocket Client API
####################

.. contents::
    :local:
    :depth: 2

Overview
********

The Websocket client library allows Zephyr to connect to a Websocket server.
The Websocket client API can be used directly by application to establish
a Websocket connection to server, or it can be used as a transport for other
network protocols like MQTT.

See this
`Websocket Wikipedia article <https://en.wikipedia.org/wiki/WebSocket>`_
for a detailed overview of how Websocket works.

For more information about the protocol itself, see
`IETF RFC6455 The WebSocket Protocol <https://tools.ietf.org/html/rfc6455>`_.

Websocket Transport
*******************

The Websocket API allows it to be used as a transport for other high level
protocols like MQTT. The Zephyr MQTT client library can be configured to use
Websocket transport by enabling :kconfig:option:`CONFIG_MQTT_LIB_WEBSOCKET` and
:kconfig:option:`CONFIG_WEBSOCKET_CLIENT` Kconfig options.

First a socket needs to be created and connected to the Websocket server:

.. code-block:: c

    sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
    ...
    ret = connect(sock, addr, addr_len);
    ...

The Websocket transport socket is then created like this:

.. code-block:: c

    ws_sock = websocket_connect(sock, &config, timeout, user_data);

The Websocket socket can then be used to send or receive data, and the
Websocket client API will encapsulate the sent or received data to/from
Websocket packet payload. Both the :c:func:`websocket_xxx()` API or normal
BSD socket API functions can be used to send and receive application data.

.. code-block:: c

    ret = websocket_send_msg(ws_sock, buf_to_send, buf_len,
                             WEBSOCKET_OPCODE_DATA_BINARY, true, true,
			     K_FOREVER);
    ...
    ret = send(ws_sock, buf_to_send, buf_len, 0);

If normal BSD socket functions are used, then currently only TEXT data
is supported. In order to send BINARY data, the :c:func:`websocket_send_msg()`
must be used.

When done, the Websocket transport socket must be closed. User should handle
the lifecycle(close/reuse) of tcp socket after websocket_disconnect.

.. code-block:: c

    ret = close(ws_sock);
    or
    ret = websocket_disconnect(ws_sock);


API Reference
*************

.. doxygengroup:: websocket

Part 3

한국어 전문 번역

개요

WebSocket client library는 Zephyr가 WebSocket server에 연결할 수 있게 합니다. Application이 WebSocket client API를 직접 사용해 server와 WebSocket connection을 맺거나, MQTT 같은 다른 network protocol의 transport로 사용할 수 있습니다.

WebSocket의 동작 개요는 WebSocket Wikipedia 문서를, protocol 자체는 IETF RFC 6455 The WebSocket Protocol을 참고하십시오.

WebSocket Transport

WebSocket API는 MQTT 같은 상위 protocol의 transport로 사용할 수 있습니다. CONFIG_MQTT_LIB_WEBSOCKET과 CONFIG_WEBSOCKET_CLIENT Kconfig option을 활성화하면 Zephyr MQTT client library가 WebSocket transport를 사용하도록 구성할 수 있습니다.

먼저 socket을 만들고 WebSocket server에 연결합니다.

sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
...
ret = connect(sock, addr, addr_len);
...

그 다음 WebSocket transport socket을 생성합니다.

ws_sock = websocket_connect(sock, &config, timeout, user_data);

WebSocket socket으로 data를 보내거나 받을 수 있고, client API가 송수신 data를 WebSocket packet payload로 encapsulate하거나 payload에서 꺼냅니다. Application data 송수신에는 websocket_xxx() API와 일반 BSD socket API를 모두 사용할 수 있습니다.

ret = websocket_send_msg(ws_sock, buf_to_send, buf_len,
                         WEBSOCKET_OPCODE_DATA_BINARY, true, true,
    K_FOREVER);
...
ret = send(ws_sock, buf_to_send, buf_len, 0);

일반 BSD socket function을 사용하면 현재 TEXT data만 지원합니다. BINARY data를 보내려면 반드시 websocket_send_msg()를 사용해야 합니다.

사용이 끝나면 WebSocket transport socket을 닫아야 합니다. websocket_disconnect 뒤의 TCP socket close 또는 reuse lifecycle은 사용자가 직접 관리해야 합니다.

ret = close(ws_sock);
or
ret = websocket_disconnect(ws_sock);

API Reference

Doxygen group websocket이 API reference를 제공합니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.