Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple clients #48

Closed
appleshine opened this issue Jun 19, 2019 · 7 comments
Closed

Multiple clients #48

appleshine opened this issue Jun 19, 2019 · 7 comments

Comments

@appleshine
Copy link

Hi, is it possible to have multiple StompClientLib clients that connect to different URL? How can the delegate differentiate the clients?

Thanks in advance!

@WrathChaos
Copy link
Owner

Hello @appleshine,
I haven't tried it before but it is possible. You simply need to create two client variable but also you need to write your own delegates extends from mine and set each of them in here:

var socketClient = StompClientLib()
let url = NSURL(string: "your-socket-url-is-here")!
socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL) , delegate: YOUR-CUSTOM-DELEGATE)

delegate: YOUR-CUSTOM-DELEGATE

Please keep me inform about multiple client issue :)

@appleshine
Copy link
Author

I created my own delegate extending from StompClientLibDelegate

socketClient.openSocketWithURLRequest(request: url, delegate: myDelegate)

the websocket is connecting properly and the topic is being subscribed. but for some reason I cant get any response from the subscribed topic.

 func stompClient(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: AnyObject?, withHeader header: [String : String]?, withDestination destination: String) {
        print("DESTINATION : \(destination)")
        print("JSON BODY : \(String(describing: jsonBody))")    
    }

this method is not going through, if i set the delegate back to self. i can receive the message with no problem. both the classes have the same delegate functions and code tho

@WrathChaos
Copy link
Owner

@appleshine Can you share the example project with me? We can make it faster in this way. Also please tell me which version of the library do you use?

@appleshine
Copy link
Author

Unfortunately I do not have an example project. My websocket url is localhost :( But my delegate is as below:

import StompClientLib
import SwiftyJSON

class TestSocketDelegate : StompClientLibDelegate {
    var url = NSURLRequest(url: URL(string: "ws://localhost:8080/subscribe/websocket")!)
    let topic = "/topic/area"
    var socketClient = StompClientLib()
    
    // MARK: - WEB SOCKET
    
    func stompClientDidConnect(client: StompClientLib!) {
        let topic = self.topic
        print("Socket is Connected : \(topic)")
        socketClient.subscribe(destination: "/topic/area")
        // Auto Disconnect after 3 sec
        //socketClient.autoDisconnect(time: 5)
        // Reconnect after 4 sec
        //socketClient.reconnect(request: url, delegate: self as StompClientLibDelegate, time: 4.0)
    }
    
    func stompClientDidDisconnect(client: StompClientLib!) {
        print("Socket is Disconnected")
    }
    
    func stompClient(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: AnyObject?, withHeader header: [String : String]?, withDestination destination: String) {
        print("DESTINATION : \(destination)")
        print("JSON BODY : \(String(describing: jsonBody))")
        
        let jsonObj = JSON(arrayLiteral: jsonBody!)
    }
    
    func stompClientJSONBody(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: String?, withHeader header: [String : String]?, withDestination destination: String) {
        print("DESTINATION : \(destination)")
        print("String JSON BODY : \(String(describing: jsonBody))")
        do {
            guard let data = Data(base64Encoded: jsonBody!) else { return }
            let jsonObj = try JSON(data: data)
        } catch let error {
            print("parse error: \(error.localizedDescription)")
        }
        
    }
    
    func serverDidSendReceipt(client: StompClientLib!, withReceiptId receiptId: String) {
        print("Receipt : \(receiptId)")
    }
    
    func serverDidSendError(client: StompClientLib!, withErrorMessage description: String, detailedErrorMessage message: String?) {
        print("Error : \(String(describing: message))")
    }
    
    func serverDidSendPing() {
        print("Server Ping")
    }
}

in my main view controller

let myDelegate = TestSocketDelegate()
        socketClient.openSocketWithURLRequest(request: url, delegate: myDelegate, connectionHeaders: ["accept-version": "1.1,1.2"])

@WrathChaos
Copy link
Owner

WrathChaos commented Jul 9, 2019

Hey @appleshine,
Sorry for the late response. What happens when you run this code? Delegate not working or shows up an error?

Also, I have a little question for you. Why do you need to use multiclient? You can access more than one topic with just one client? Please describe your use-case maybe you're just trying a hard method to solve it :)

@appleshine
Copy link
Author

Theres no error but I receive no response/message from the server.

func stompClient(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: AnyObject?, withHeader header: [String : String]?, withDestination destination: String) {
        print("DESTINATION : \(destination)")
        print("JSON BODY : \(String(describing: jsonBody))")    
    }

I understand that I can subscribe to multiple topics, but in my case, the websocket servers are created on multiple VMs thats why I have multiple urls for me to connect to.

@WrathChaos
Copy link
Owner

@appleshine Can you provide me with an example server-side project for me? I would like to help you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants