Skip to content

Commit

Permalink
Subscribe and Unsubscibe methods are changed. Unnecessary prints and …
Browse files Browse the repository at this point in the history
…methods are removed. New ReadME file is added.
  • Loading branch information
WrathChaos committed Jul 8, 2017
1 parent f936d34 commit e057429
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 9 deletions.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,107 @@ it, simply add the following line to your Podfile:
pod "StompClientLib"
```

## Usage
```ruby
import StompClientLib
```

Once imported, you can open a connection to your WebSocket server.

```ruby
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
```ruby
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
```ruby
func stompClientDidDisconnect(client: StompClientLib!) {
print("Socket is Disconnected")
}
```


# stompClientWillDisconnect
```ruby
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
```ruby
func stompClient(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: AnyObject?, withHeader header: [String : String]?, withDestination destination: String) {
print("Destination : \(destination)")
print("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
```ruby
func serverDidSendReceipt(client: StompClientLib!, withReceiptId receiptId: String) {
print("Receipt : \(receiptId)")
}
```

#serverDidSendError

Your error message will be received in this function

```ruby
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

```ruby
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
```ruby
socketClient.subscribe(destination: topic)
// Note : topic needs to be a String object
```
# Unsubscribe

```ruby
socketClient.unsubscribe(destination: topic)
```

Important : You have to send your destination for both subscribe or unsubscribe!

## Author

FreakyCoder, [email protected]
Expand Down
4 changes: 2 additions & 2 deletions StompClientLib.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ StompClientLib let the websocket use the Stomp protocol. STOMP Protocol let the
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'FreakyCoder' => '[email protected]' }
s.source = { :git => 'https://github.com/wrathchaos/StompClientLib.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/freakycodercom'
s.social_media_url = 'https://twitter.com/freakycodercom'

s.ios.deployment_target = '8.0'
s.ios.deployment_target = '10.0'

s.source_files = 'StompClientLib/Classes/**/*'

Expand Down
10 changes: 3 additions & 7 deletions StompClientLib/Classes/StompClientLib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public protocol StompClientLibDelegate {
func stompClient(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: AnyObject?, withHeader header:[String:String]?, withDestination destination: String)

func stompClientDidDisconnect(client: StompClientLib!)
func stompClientWillDisconnect(client: StompClientLib!, withError error: NSError)
func stompClientDidConnect(client: StompClientLib!)
func serverDidSendReceipt(client: StompClientLib!, withReceiptId receiptId: String)
func serverDidSendError(client: StompClientLib!, withErrorMessage description: String, detailedErrorMessage message: String?)
Expand Down Expand Up @@ -267,7 +266,6 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
}

private func receiveFrame(command: String, headers: [String: String], body: String?) {
print("COMMAND : \(command)")
if command == StompCommands.responseFrameConnected {
// Connected
if let sessId = headers[StompCommands.responseHeaderSession] {
Expand All @@ -281,14 +279,12 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
}
} else if command == StompCommands.responseFrameMessage { // Message comes to this part
// Response
print("RESPONSE FROM MESSAGE ")
if let delegate = delegate {
DispatchQueue.main.async(execute: {
print("MESSAGE : \(String(describing: body))")
delegate.stompClient(client: self, didReceiveMessageWithJSONBody: self.dictForJSONString(jsonStr: body), withHeader: headers, withDestination: self.destinationFromHeader(header: headers))
})
}
} else if command == StompCommands.responseFrameReceipt {
} else if command == StompCommands.responseFrameReceipt { //
// Receipt
if let delegate = delegate {
if let receiptId = headers[StompCommands.responseHeaderReceiptId] {
Expand Down Expand Up @@ -363,13 +359,13 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
self.sendFrame(command: StompCommands.commandSubscribe, header: headers, body: nil)
}

public func subscribeToDestination(destination: String, withHeader header: [String: String]) {
public func subscribe(destination: String, withHeader header: [String: String]) {
var headerToSend = header
headerToSend[StompCommands.commandHeaderDestination] = destination
sendFrame(command: StompCommands.commandSubscribe, header: headerToSend, body: nil)
}

public func unsubscribeFromDestination(destination: String) {
public func unsubscribe(destination: String) {
var headerToSend = [String: String]()
headerToSend[StompCommands.commandHeaderDestinationId] = destination
sendFrame(command: StompCommands.commandUnsubscribe, header: headerToSend, body: nil)
Expand Down

0 comments on commit e057429

Please sign in to comment.