SwiftStompClient
SwiftStompClient 是在原生 WebSocket 上实现的 STOMP,从 iOS 13 开始支持
要求
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
使用 StompClient 的示例
let request = URLRequest(url: "wss://,ws://")
let webSocket = WebSocket(request: request)
let heartBeat = HeartBeat(clientHeartBeating: "2000,5000")
let stompClient = SwiftStompClient(webSocket: webSocket, heartBeat: heartBeat)
stompClient.stompDelegate = self
stompClient.openWebSocketConnection()
request
请求必须包含 ws:// 或 wss://
webSocket
用于发送 STOMP 消息的 WebSocket 连接
heartBeat
心跳是一个可选参数,用于接收 pong 消息和发送 ping 消息,初始化为字符串 "2000,5000",其中 2000 毫秒是发送 ping 消息到服务器的时间,5000 毫秒等待从服务器接收 pong 消息,更详细的信息请访问 心跳
stompDelegate
实现处理 WebSocket 连接/断开和接收帧(CONNECTED, MESSAGE, RECEIPT, ERROR, \n
)
openWebSocketConnection()
打开 WebSocket 连接,处理打开 WebSocket 的 func handleWebSocketConnect(session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol: String?)
然后,您可以使用 sendFrame(frame: StompFrameProtocol)
closeWebSocketConnection()
断开 WebSocket 连接,处理断开 WebSocket 的 func handleWebSocketDisconnect(session: URLSession, webSocketTask: URLSessionWebSocketTask, closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?)
sendFrame(frame: StompFrameProtocol)
通过 WebSocket 发送 STOMP 消息,如果需要发送自定义的消息,请订阅 StompFrameProtocol
对象
STOMP服务器示例
在终端中运行 $ java -jar stomp-server.jar
,服务器在 ws://127.0.0.1:8080/ws
上运行
安装
CocoaPods
pod 'SwiftStompClient', '~> 0.0.4'