gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:03:57 28 4
gpt4 key购买 nike

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com