Code Monkey home page Code Monkey logo

stompclientlib's Introduction

StompClientLib

License platform Cocoapods Downloads

Build Issues Swift 4.0 Swift 3.0 Pod Version

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 4+ and 3+ projects.

This is original a fork from AKStompClient (This library is not working right now)

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 +
  • Swift 3.0, 3.1, 3.2
  • Swift 4.0+

Installation

StompClientLib is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "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")
}

stompClientWillDisconnect

func stompClientWillDisconnect(client: StompClientLib!, withError error: NSError) {

}

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?, withHeader header: [String : String]?, withDestination destination: String) {
print("Destination : \(destination)")
print("JSON Body : \(String(describing: jsonBody))")
}

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("DESTIONATION : \(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 unsubscibe 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!

Future Enhancements

  • Complete a working Example
  • Add Carthage installation option
  • Add Swift Package Manager installation option
  • XCode 9 compatibility
  • Swift 4 compatibility and tests
  • Add Quick Testing
  • Re-write websocket full swift version instead of Facebook's SocketRocket lib

Author

FreakyCoder, [email protected]

License

StompClientLib is available under the MIT license. See the LICENSE file for more info.

stompclientlib's People

Contributors

wrathchaos avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.