我正在尝试在 iOS 和服务器(SpringBoot)之间建立 WebSocket 连接。从服务器端,我们使用这样的 WebSocket 连接。
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker(URLMapping.WS_SEND);
config.setApplicationDestinationPrefixes(URLMapping.WS_PREFIX);
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/api/v1/transactionSocket").setAllowedOrigins("*").withSockJS();
}
}
在IOS中,我使用过StompClient用于建立 WebSocket 连接的库。
func makeConnection() {
let client = StompClient(url: URL(string: "/api/v1/transactionSocket")!)
client.delegate = self
client.connect()
}
func disconnectConnection() {
client.disconnect()
print("Disconnecting :\(client)")
}
func stompClientDidConnected(_ client: StompClient) {
print("Stomp got connected: \(client) .... \(client.isConnected)")
// client.subscribe("API")
}
func stompClient(_ client: StompClient, didErrorOccurred error: NSError) {
print("Stomp Error occures \(client) errror: \(error)")
}
func stompClient(_ client: StompClient, didReceivedData data: Data, fromDestination destination: String) {
print("Cliemt: \(client) Data: \(data) destination: \(destination)")
}
运行时既没有建立连接,也没有调用“stompClientDidConnected”委托(delegate)方法。
我还没怎么用过WebSocket。所以无法理解原因是什么。任何帮助将不胜感激。
谢谢
最佳答案
您可以使用StopmClientLib
进行套接字连接,并且它具有订阅方法。
关于ios - 无法在服务器(SpringBoot)和 iOS 之间建立 WebSocket 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46973744/