StompClientLib
Simple STOMP Client library, Swift 3 and 4, 4.2, 5 compatible
Install / Use
/learn @kuraydev/StompClientLibREADME
StompClientLib
<p align="center"> <img width="200" height="200" src="Screenshots/socket.png"> </p> <p align="center"> <a href="https://github.com/WrathChaos/StompClientLib"> <img src="https://img.shields.io/cocoapods/l/StompClientLib.svg" alt="License"> </a> <a href="https://github.com/WrathChaos/StompClientLib"> <img src="https://img.shields.io/cocoapods/p/StompClientLib.svg" alt="platform"> </a> <a href="https://github.com/WrathChaos/StompClientLib"> <img src="https://img.shields.io/badge/CocoaPods-compatible-4BC51D.svg" alt="Cocoapods"> </a> </p> <p align="center"> <a href="https://github.com/WrathChaos/MJPEGStreamLib"> <img src="https://img.shields.io/badge/Swift-5.0-red.svg" alt="Swift 5.0"> <img src="https://img.shields.io/badge/Swift-4.2-orange.svg" alt="Swift 4.2"> <img src="https://img.shields.io/badge/Swift-3.0-blue.svg" alt="Swift 3.0"> </a> <a href="https://github.com/WrathChaos/StompClientLib"> <img src="https://img.shields.io/cocoapods/v/StompClientLib.svg" alt="Pod Version"> </a> <a href="https://github.com/WrathChaos/StompClientLib"> <img src="https://img.shields.io/github/issues/WrathChaos/StompClientLib.svg" alt="Issues"> </a> </p> <p align="center"> <img alt="Swift 5+ StompClient Library" src="Screenshots/StompClientLib-Example.gif" width="500px" /> </p>Introduction
StompClientLib is a stomp client in Swift. It uses Facebook's SocketRocket as a websocket dependency. SocketRocket is written in Objective-C but StompClientLib's STOMP part is written in Swift and its usage is Swift. You can use this library in your Swift 5+, 4+ and 3+ projects.
Supported Stomp Versions
Stomp version
- 1.1
- 1.2
- Might not be worked with 1.0 (Never tested)
Example
To run the example project, clone the repo, and run pod install from the Example directory first.
Requirements
- iOS 8.0+
- XCode 8.1, 8.2, 8.3
- XCode 9.0+
- XCode 10.0 +
- XCode 12.1 +
- Swift 3.0, 3.1, 3.2
- Swift 4.0, Swift 4.1, Swift 4.2, Swift 5.0
Installation
StompClientLib is available through CocoaPods. To install it, simply add the following line to your Podfile:
Cocoapods
pod "StompClientLib"
Carthage
github "WrathChaos/StompClientLib"
Usage
import StompClientLib
Once imported, you can open a connection to your WebSocket server.
var socketClient = StompClientLib()
let url = NSURL(string: "your-socket-url-is-here")!
socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL) , delegate: self)
After you are connected, there are some delegate methods that you need to implement.
StompClientLibDelegate
stompClientDidConnect
func stompClientDidConnect(client: StompClientLib!) {
print("Socket is connected")
// Stomp subscribe will be here!
socketClient.subscribe(destination: topic)
// Note : topic needs to be a String object
}
stompClientDidDisconnect
func stompClientDidDisconnect(client: StompClientLib!) {
print("Socket is Disconnected")
}
didReceiveMessageWithJSONBody ( Message Received via STOMP )
Your json message will be converted to JSON Body as AnyObject and you will receive your message in this function
func stompClient(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: AnyObject?, akaStringBody stringBody: String?, withHeader header: [String : String]?, withDestination destination: String) {
print("Destination : \(destination)")
print("JSON Body : \(String(describing: jsonBody))")
print("String Body : \(stringBody ?? "nil")")
}
didReceiveMessageWithJSONBody ( Message Received via STOMP as String )
Your json message will be converted to JSON Body as AnyObject and you will receive your message in this function
func stompClientJSONBody(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: String?, withHeader header: [String : String]?, withDestination destination: String) {
print("DESTINATION : \(destination)")
print("String JSON BODY : \(String(describing: jsonBody))")
}
serverDidSendReceipt
If you will use STOMP for in-app purchase, you might need to use this function to get receipt
func serverDidSendReceipt(client: StompClientLib!, withReceiptId receiptId: String) {
print("Receipt : \(receiptId)")
}
serverDidSendError
Your error message will be received in this function
func serverDidSendError(client: StompClientLib!, withErrorMessage description: String, detailedErrorMessage message: String?) {
print("Error Send : \(String(describing: message))")
}
serverDidSendPing
If you need to control your server's ping, here is your part
func serverDidSendPing() {
print("Server ping")
}
How to subscribe and unsubscribe
There are functions for subscribing and unsubscribing. Note : You should handle your subscribe and unsubscribe methods ! Suggestion : Subscribe to your topic in "stompClientDidConnect" function and unsubcribe to your topic in stompClientWillDisconnect method.
Subscribe
socketClient.subscribe(destination: topic)
// Note : topic needs to be a String object
Unsubscribe
socketClient.unsubscribe(destination: topic)
Important : You have to send your destination for both subscribe or unsubscribe!
Unsubsribe with header
let destination = "/topic/your_topic"
let ack = destination
let id = destination
let header = ["destination": destination, "ack": ack, "id": id]
// subscribe
socketClient?.subscribeWithHeader(destination: destination, withHeader: header)
// unsubscribe
socketClient?.unsubscribe(destination: subsId)
Auto Reconnect with a given time
You can use this feature if you need to auto reconnect with a specific time or it will just try to reconnect every second.
// Reconnect after 4 sec
socketClient.reconnect(request: NSURLRequest(url: url as URL) , delegate: self as StompClientLibDelegate, time: 4.0)
Auto Disconnect with a given time
// Auto Disconnect after 3 sec
socketClient.autoDisconnect(time: 3)
Login Passcode Implementation
This is just an example. You need to convert to your implementation. #42
let connectFrame = "CONNECT\n login:admin\n passcode:password\n\n\n\0"
socket.write(string: connectFrame)
Future Enhancements
- [x] ~~Complete a working Example~~
- [x] ~~Add Carthage installation option~~
- [x] ~~Add Swift Package Manager installation option~~
- [x] ~~XCode 9 compatibility~~
- [x] ~~Swift 4 compatibility and tests~~
- [ ] Apple's New Socket for iOS 13 Implementation from stratch
Changelog
Implemented enhancements:
- SUBSCRIBE and UNSUBSCRIBE Delegate is missing #16
Closed issues:
- Error Domain=SRWebSocketErrorDomain Code=2132 "received bad response code from server 403" #86
- the delegate should be weak #83
1.3.8 (2020-03-08)
Implemented enhancements:
- Socket stopped working once I moved from ws//: to wss//: #67
- Self sign certificate, 400 error #66
- How to increase output buffer #37
Fixed bugs:
Closed issues:
- Can't find header in initial call #81
- Can't connect to websocket : received bad response code from server 422 #80
- I just closed the issue because of the stale & reproducible problem #79
- unable to install #77
- stompClientDidConnect not called with Spring boot #73
- Issue with Cookies in header #71
- Can't see any Websocket traffic in Charles Proxy #70
- End of stream error #69
- Cannot connect with Stomp Websocket when custom header #64
- Can't connect Socket with Spring Boot 2.x.x #63
- Multiple clients #48
- IPV6 #38
Merged pull requests:
1.3.7 (2019-08-26)
Closed issues:
- The problem when receiving the messages from the Spring boot. #68
1.3.6 (2019-08-06)
[Full Changelog](
