WrathChaos / StompClientLib

Simple STOMP Client library, Swift 3 and 4, 4.2, 5 compatible
https://www.freakycoder.com
MIT License
154 stars 81 forks source link

It not able to connect web socket. #13

Closed hukusuke1007 closed 6 years ago

hukusuke1007 commented 6 years ago

I have a problem. It not able to connect web socket.

■Source code.

let baseURL = "http://alice2.nem.ninja:7890" print("URL : (baseURL)") let url = NSURL(string: baseURL)! StompClientLib().openSocketWithURLRequest(request: NSURLRequest(url: url as URL) , delegate: self)

■Result

URL : http://alice2.nem.ninja:7890 2018-01-19 02:07:04.566143+0900 TestNem[35284:6662686] [MC] Lazy loading NSBundle MobileCoreServices.framework 2018-01-19 02:07:04.568846+0900 TestNem[35284:6662686] [MC] Loaded MobileCoreServices.framework didFailWithError: Optional(Error Domain=SRWebSocketErrorDomain Code=2132 "received bad response code from server 404" UserInfo={NSLocalizedDescription=received bad response code from server 404, HTTPResponseStatusCode=404}) Error : Optional("received bad response code from server 404") 2018-01-19 02:08:05.167697+0900 TestNem[35284:6666614] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert 2018-01-19 02:08:05.168153+0900 TestNem[35284:6666614] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert

Is it need to set other parameter?? I want your advice.

Best regards.

WrathChaos commented 6 years ago

Hello @hukusuke1007 , Are you sure about your server socket using the stomp??

hukusuke1007 commented 6 years ago

@WrathChaos Yes. I heard that "http://alice2.nem.ninja:7890" is supported STOMP web socket. → I changed it to "http://alice2.nem.ninja:7778/w/messages/websocket". It's correct from this material(https://rb2nem.github.io/nem-dev-guide/07-monitoring-blockchain/).

But it not called override method.

・Additional infomation. I implemented it as follows. It not occur 404 error. But it not called override method(stompClientDidConnect). And i tried it in both iOS simulator and real machine. Is the usage of the your library correct?

import UIKit
import StompClientLib

class ViewController: UIViewController, StompClientLibDelegate {

    struct mySecretInfo {
        let adress:String = "NCH5JDPEAKA3CKQBSEPE4VIH53FQO4AU55XTVRPY"
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        RegisterSocket()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    public func RegisterSocket() {
        let baseURL = "http://alice2.nem.ninja:7778/w/messages/websocket"
        let completedWSURL = baseURL
        print("URL : \(completedWSURL)")
        let url = NSURL(string: completedWSURL)!
        let socketClient = StompClientLib()
        socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL) , delegate: self)
    }

    // ■ MARK: delegate.
    func stompClientDidConnect(client: StompClientLib!) {

        let info = mySecretInfo()
        // ■ Subscribe.
        let topic = "/account/mosaic/owned/" + info.adress
        print("Socket is Connected : \(topic)")
        client.subscribe(destination: topic)

        // ■ Send.
        let sendData = "{'account':'\(info.adress)'}"
        let sendTopic = "/w/api/account/mosaic/owned"
        client.sendJSONForDict(dict: sendData as AnyObject, toDestination: sendTopic)
    }

    func stompClientDidDisconnect(client: StompClientLib!) {
        print("Socket is Disconnected")
    }

    func stompClientWillDisconnect(client: StompClientLib!, withError error: NSError) {
        print("Socket is Disconnected error \(error)")
    }

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

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

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

■Log. URL : http://alice2.nem.ninja:7778/w/messages/websocket 2018-01-20 12:34:23.070525+0900 TestNem[6505:815958] [MC] Lazy loading NSBundle MobileCoreServices.framework 2018-01-20 12:34:23.071388+0900 TestNem[6505:815958] [MC] Loaded MobileCoreServices.framework

WrathChaos commented 6 years ago

@hukusuke1007 Okey, I will try to get data from your socket. Do I need any other information to connect your socket url?

WrathChaos commented 6 years ago

@hukusuke1007 Hello again, I've tried your url on the example and it works perfectly. Please delete

let wsURL = baseURL.prefix(7)
let completedWSURL = "ws://\(wsURL)gateway/websocket"
print("Completed WS URL : \(completedWSURL)")

These lines on the example and try it :) Give me a feedback please Have fun !

hukusuke1007 commented 6 years ago

@WrathChaos I'm sorry for late. It successful to connect when I implemented a following source code. But no response when call SEND message(sendJSONForDict(dict: sendData as AnyObject, toDestination: sendTopic).

import UIKit
import StompClientLib

class ViewController: UIViewController, StompClientLibDelegate {

    private var socketClient_:StompClientLib!

    override func viewDidLoad() {
        super.viewDidLoad()
        //getXemByJpyFromZaif_()

        socketClient_ = StompClientLib()

        RegisterSocket()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    public func RegisterSocket() {

        let baseURL = "http://alice2.nem.ninja:7778/w/messages/websocket"

        let completedWSURL = baseURL
        print("URL : \(completedWSURL)")
        let url = NSURL(string: completedWSURL)!
        socketClient_.openSocketWithURLRequest(request: NSURLRequest(url: url as URL) , delegate: self)
    }

    // ■ MARK: delegate.
    func stompClientDidConnect(client: StompClientLib!) {

        let info = mySecretInfo()
        // ■ Subscribe.
        let topic = "/blocks/new"         // OK
        let header = ["id":"0"]
        print("Subscrib : \(topic)")
        client.subscribeWithHeader(destination: topic, withHeader: header)

        // ■ Send.
        let sendTopic = "/w/api/account/mosaic/owned/"
        let sendData = ["account":"NCH5JDPEAKA3CKQBSEPE4VIH53FQO4AU55XTVRPY"]
        print("Send topic : \(sendTopic)")
        client.sendJSONForDict(dict: sendData as AnyObject, toDestination: sendTopic)
    }

    func stompClientDidDisconnect(client: StompClientLib!) {
        print("Socket is Disconnected")
        RegisterSocket()
    }

    func stompClientWillDisconnect(client: StompClientLib!, withError error: NSError) {
        print("Socket is Disconnected error \(error)")
    }

    func stompClient(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: AnyObject?, withHeader header: [String : String]?, withDestination destination: String) {

        if let data = header {
            data.forEach {print("\($0.0):\($0.1)") }
        }
        print("DESTIONATION : \(destination)")
        print("JSON BODY : \(String(describing: jsonBody))")
    }

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

    func serverDidSendReceipt(client: StompClientLib!, withReceiptId receiptId: String) {
        print("Receipt : \(receiptId)")
    }

    func serverDidSendError(client: StompClientLib!, withErrorMessage description: String, detailedErrorMessage message: String?) {
        print("Error : \(description) \(String(describing: message))")
    }

    func serverDidSendPing() {
        print("Server Ping")
    }
}
WrathChaos commented 6 years ago

@hukusuke1007 Sorry for very late response, had a tough week. I tried to subscribe your topic but it is failed.. Can you please check that your topic string is valid or not. Have fun :)

WrathChaos commented 6 years ago

@hukusuke1007 Could you solve your issue?

hukusuke1007 commented 6 years ago

@WrathChaos Sorry for very late response. OK. I check my topic.

Thank you for your advice.