10. 이벤트 스트림 리스너
이벤트 채널(포트) 10020)에 연결하여 레벨, 이름 패턴, 제외 조건에 따라 임의로 필터링한 후 시스템 이벤트를 출력합니다. 읽기 전용이므로 세션 프로필은 전송되지 않으며, 명령어도 전송되지 않습니다.
이 튜토리얼에서 배울 수 있는 내용:
- 수동적인 관찰자로서이벤트 WebSocket(포트)
10020)를 구독하는 것 - 이벤트 JSON분석 —
level、name、category、data、message - 최저 심각도 수준에 따른 필터링
- 와일드카드를 이용한 이름 일치 및 제외
- 다른 튜토리얼과 함께 이 튜토리얼을 활용하여제어 루프의 성능을 측정해 보십시오.
워크플로우
- CLI 플래그(
--level、--name、--exclude、--no-hide-rate、--port)를 분석합니다. ws://localhost:10020(이벤트 채널)에 연결합니다. 읽기 전용 관찰자 역할을 하므로 세션 프로필을 전송하지 않습니다.- 수신된 메시지마다 이벤트 JSON을 파싱하고 필터를 적용합니다:
level이 값이 최소 심각도보다 낮은 경우 폐기합니다.--name패턴이 지정된 경우, 그중 하나와 일치하지 않으면 버립니다(name단독으로 또는category/name과 대조합니다).--exclude다음 패턴 중 하나와 일치하는 경우 해당 항목을 삭제합니다.
- 남은 이벤트를 레벨, 수식명, (선택 사항인) 메시지, (선택 사항인)
data페이로드와 함께 출력합니다.
매개변수
| 플래그 | 기본값 | 용도 |
|---|---|---|
--port | 10020 | 이벤트 채널의 포트 |
-l, --level | info | 가장 낮은 심각도.info、notice、warning、error、critical、panic 다음 중 하나 |
-n, --name | (없음) | 패턴과 일치하는 이벤트만 표시 (glob, 여러 개 지정 가능) |
-x, --exclude | (없음) | 패턴과 일치하는 이벤트 숨기기 (glob, 여러 개 지정 가능) |
--no-hide-rate | false | system-rate-report 이벤트 포함 (기본적으로 숨김 — 잡음이 매우 많기 때문) |
임의의 튜토리얼(04-hello-floor, 05-position-control, 06-combined...)을 한 터미널에서 실행하고, 다른 터미널에서는 --no-hide-rate 지정된 이벤트 리스너를 실행해 주세요.system-rate-report 이 이벤트에는 실제 장치의 틱 레이트가 포함됩니다. 동일한 튜토리얼의 Python/C++ nlohmann 버전과 C++ Glaze 버전을 비교하여, 약 4 kHz의 제어 루프에서 발생하는 직렬화 오버헤드를 확인해 보십시오.
# Terminal 1 — pick a variant
./04-haply-inverse-hello-floor # C++ nlohmann
./04-haply-inverse-hello-floor-glz # C++ Glaze
python 04-haply-inverse-hello-floor.py # Python
# Terminal 2 — watch the rate
./10-haply-inverse-events --no-hide-rate
읽혀지는 상태 필드
수신된 각 메시지는 하나의 이벤트입니다:
level— 문자열의 심각도 (info...panic)name— 이벤트명 (예:device-connected、system-rate-report)category— 카테고리 (예:device、session、system)message— 임의의, 사람이 읽을 수 있는 문자열data— 임의의 중첩된 JSON 페이로드(형식은 이벤트에 따라 다름)
이벤트 전체 목록은 ‘이벤트 및 모니터링 ’을 참조하십시오.
보내기 / 받기
수동형 구독의 경우,recv() 만 사용하며, 아무것도 반환하지 않습니다. 세 가지 변형 모두 동일한 필터 파이프라인을 구현하고 있습니다. Inverse API의 특징 중 하나는 이벤트 구조체의 형태입니다.
- 파이썬
- C++ (nlohmann)
- C++ (Glaze)
async for msg in websocket 는 수신 전용 루프를 가장 간결하게 작성한 방식입니다.
async with websockets.connect(uri) as websocket:
async for msg in websocket:
try:
event = json.loads(msg)
except json.JSONDecodeError:
continue
if accept_event(event, min_level_index, name_patterns, exclude_patterns):
print(format_event(event))
libhv의 콜백 방식입니다.onmessage 는 I/O 스레드에서 실행되며, 메인 스레드는 ENTER 키 입력을 대기합니다. 이벤트는 형이 지정되지 않은 JSON으로 처리되며, 각 필드는 기본값이 지정된 .value(...) 에서 읽힙니다.
ws.onmessage = [&](const std::string &msg) {
json event;
try { event = json::parse(msg); } catch (...) { return; }
if (accept_event(event, opts.min_level, opts.name_patterns, opts.exclude_patterns))
print_event(event);
};
ws.open("ws://localhost:10020");
while (std::cin.get() != '\n') {} // block main thread
알려진 이벤트 필드는 형이 지정된 event_data 구조체에 매핑됩니다.data 필드는 glz::raw_json_view 그대로 유지되므로, (이벤트에 따라 달라지는) 임의의 중첩된 페이로드를 완전한 스키마를 정의하지 않고도 그대로 전달할 수 있습니다.
// Struct model — known fields typed, nested `data` kept as raw JSON
struct event_data {
std::string level;
std::string name;
std::string category;
std::string message;
glz::raw_json_view data{};
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
event_data event{};
if (glz::read<glz_settings>(event, msg)) return; // drop malformed
if (accept_event(event, opts.min_level, opts.name_patterns, opts.exclude_patterns))
print_event(event);
};
ws.open("ws://localhost:10020");
while (std::cin.get() != '\n') {} // block main thread
튜토리얼 10도 SDK와 함께 로컬에 설치되어 있습니다. 서비스의 설치 디렉터리 내의 tutorials/10-haply-inverse-events/ 확인해 주십시오.
관련 항목: 이벤트 및 모니터링 · WebSocket 프로토콜