일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- iot
- Architecture
- WebSocket
- dgcharts
- SampleApp
- SwiftUI
- LifeCycle
- swift
- philipshue
- WWDC24
- network
- chartsorg
- uikit
- URLSession
- weatherkit
- isolate
- Xcode
- dartz
- embedded-swift
- 문법
- AppleDeveloper
- dart
- tuist
- builder
- raspberrypi5
- designpattern
- flutter
- OpenAI
- GIT
- EventLoop
Archives
- Today
- Total
Jaebi의 Binary는 호남선
[Swift] WebSocket + STOMP 연동 본문
목차
STOMP Overview
- Simple (Streaming) Text Orientated Messaging Protocol
- 텍스트를 주고 받는 형식을 정의한 프로토콜
- 웹소켓에서 서버와 클라이언트간 데이터를 주고 받을때 정해진 형식이 없음
- 따라서 메시지를 일정한 규칙에 맞춰서 작성하자 → STOMP 통신 규약
- Frame기반 프로토콜이며 각 Frame은 HTTP 데이터 구조와 유사함 + Publish / Subscribe 구조
STOMP Frame 구성
- Command, Header, Body로 구성
- 해당 구성을 `String`화 하여 보냄
"""
COMMAND
header1:value1
header2:value2
Body^@
"""
- Command, header, Body의 구분을 줄 바꿈으로 구분하여 Parse
- 메시지가 끝났다는것을 null octect (위 예시에는 `^@`로 표현)으로 표시
STOMP Frame - COMMAND (일부)
CONNECT
- 처음 Socket을 열고 서버로부터 연결을 요청
- 필수 헤더:
- accept-version - STOMP 프로토콜 버전
- host - Client가 연결하고자 하는 Host 이름
- 선택 헤더: login, passcode, heart-beat
STOMP Frame | CONNECT (Client) | CONNECTED (Server) | ERROR (Server) |
Sample Code | CONNECT accept-version:1.0,1.1,2.0 host:stomp.github.org ^@ |
CONNECTED version:1.1 ^@ |
ERROR version:1.2,2.1 content-type:text/plain Supported protocol versions are 1.2 2.1 ^@ |
SEND
- 실제 대상에게 메시지 전송
- 필수 헤더:
- destination - 메시지를 보낼 위치 (Channel)
- Body에 실제로 보낼 메시지 기입
STOMP Frame | SEND (Client) |
Sample Code | SEND destination:/queue/a content-type:text/plain hello queue a ^@ |
SUBSCRIBE
- 특정 위치 (Channel)의 메시지를 구독
- 필수 헤더:
- destination - 메시지를 보낼 위치 (Channel)
- 선택 헤더:
- id - subscription 구분용 ID
STOMP Frame | SUBSCRIBE (Client) |
Sample Code | SUBSCRIBE id:0 destination:/queue/foo ack:client ^@ |
UNSUBSCRIBE
- 구독 취소
- 필수 헤더:
- id - 구독 취소 할 subscription ID
STOMP Frame | UNSUBSCRIBE (Client) |
Sample Code | UNSUBSCRIBE id:0 ^@ |
DISCONNECT
- 클라이언트와 서버와의 연결 종료
- STOMP에서 권장하는 Flow
- DISCONNECT COMMAND Frame을 서버로 송신 (header에 receipt 추가)
- 서버로 DISCONNECT Frame에 대한 RECEIPT Frame 수신
- receipt-id 확인 후 소켓 닫음
STOMP Frame | DISCONNECT (Client) | RECEIPT (Server) |
Sample Code | DISCONNECT receipt:77 ^@ |
RECEIPT receipt-id:77 ^@ |
STOMP Frame - Header
- Content-Length, Content-Type을 꼭 Header에 추가시켜 주어야 함 (ver. 1.2)
STOMP Frame - Body
- SEND, MESSAGE, ERROR Command에서만 Body가 포함되어야만 함
- 실제로 메시지가 전달되는 영역 (기존 HTTP API 호출할 때 Response Parameter와 유사)
WebSocket 통신 Sample Diagram

STOMP 통신 Sample Diagram

Reference
STOMP
STOMP is a very simple and easy to implement protocol, coming from the HTTP school of design; the server side may be hard to implement well, but it is very easy to write a client to get yourself connected. For example you can use Telnet to login to any STO
stomp.github.io
[Swift] STOMP 톺아보기
안녕하세요, 하노입니다 :D 오늘은 Websocket에서 사용할 수 있는 Protocol 중 하나인 STOMP (Simple Text Oriented messaging protocol) 에 대해서 간단하게 알아보려고 합니다! 사실 회사에서 STOMP 기술을 채택하면
glsman-111co.tistory.com
'Swift' 카테고리의 다른 글
[Swift] 대용량 파일 다운로드 (1) - FileDownloadService (0) | 2025.03.08 |
---|---|
[Swift] 대용량 파일 다운로드 (0) - 계획 (0) | 2025.03.08 |
[Swift] 실시간 데이터 표현 (0) | 2025.02.04 |
[Package] DGCharts - 차트 커스터마이징 (0) | 2025.02.04 |
[Swift] Combine & Async/Await 알아보기 (0) | 2024.08.19 |