Swift-IOS,Spring:使用StompClientLib和Spring发送和接收STOMP消息时出现问题

时间:2018-10-24 12:00:09

标签: ios swift spring-websocket stomp

Stomp客户端成功连接到套接字,当我尝试向服务器发送消息时,给定路径上的服务器方法根本不执行。我尝试使用完整的“ ws:// localhost:8080 / app / hello”路径,但没有成功。 当我在浏览器上使用JS客户端时,它可以完美运行。 我的猜测是我为send和subscription方法提供了错误的路径。 这是来自服务器和客户端实现的代码。

春天:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements 
       WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/gs-guide-websocket").withSockJS();
}

}

@Controller
public class GreetingController {


@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(String message) throws Exception {
    System.out.println("Message sent: " + message);
    return new Greeting("Hello, " + HtmlUtils.htmlEscape(message + "!"));
}

}

Swift客户端:

import UIKit
import StompClientLib

class ViewController: UIViewController, StompClientLibDelegate {

@IBOutlet weak var nameInput: UITextField!
@IBOutlet weak var messagesLabel: UILabel!

var socketClient = StompClientLib()

let url = URL(string: "ws://localhost:8080/gs-guide-websocket/websocket/")!
let subscribePath = "/topic/greetings"
let sendMessagePath = "/app/hello"

override func viewDidLoad() {
    super.viewDidLoad()

    socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url) , delegate: self)

    socketClient.subscribe(destination: subscribePath)
}

@IBAction func subscribeAction() {
}

@IBAction func disconnectAction() {
}

@IBAction func sendAction() {
    let name = nameInput.text!

    socketClient.sendMessage(message: name, toDestination: sendMessagePath, withHeaders: nil, withReceipt: nil)
}


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

func stompClientJSONBody(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: String?, withHeader header: [String : String]?, withDestination destination: String) {
    print(jsonBody)
}

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

func stompClientDidConnect(client: StompClientLib!) {

}

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

}

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

}

func serverDidSendPing() {
    print("serverDidSendPing")

}
}

1 个答案:

答案 0 :(得分:0)

我想问题在于订阅太早了。

painless

此主题中应该包含订阅主题的调用。 如果有人犯了与我相同的错误,这是正确的解决方案。