ios - swift - IOS, Spring : Problem with sending and receiving STOMP messages with StompClientLib and Spring

标签 ios swift spring-websocket stomp

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

Spring :

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

}
}

最佳答案

我猜问题出在订阅太早。

func stompClientDidConnect(client: StompClientLib!) {

    socketClient.subscribe(destination: topic)
}

订阅主题的调用应该在此函数中。 如果其他人犯了和我一样的错误,这是正确的解决方案。

关于ios - swift - IOS, Spring : Problem with sending and receiving STOMP messages with StompClientLib and Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968478/

相关文章:

ios - : tableView. cellForRow 和 dataSource.cellForRowAtIndexPath 之间的区别

swift - 使用 SWIFT 以编程方式自定义 UITableViewCell

ios - 为什么在 heightForRowAt 方法中我的单元格为零?

java - 在WebSocketHandler中访问HTTP session (Spring-websocket)

java - 如何使用 Spring 实现自定义 WebSocket 子协议(protocol)

ios - 当滑回根 viewController 时,大导航栏背景会变得清晰

ios - UITableViewCell 用动画填充颜色

ios - 我的一个约束有问题

ios - 关联类型为类的协议(protocol)的扩展?

spring-boot - 在 ServletContext 中找不到属性 ServerContainer