ios - 无法在服务器(SpringBoot)和 iOS 之间建立 WebSocket 连接

标签 ios swift spring-boot websocket stomp

我正在尝试在 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进行套接字连接,并且它具有订阅方法。

https://github.com/wrathchaos/StompClientLib

关于ios - 无法在服务器(SpringBoot)和 iOS 之间建立 WebSocket 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46973744/

相关文章:

ios - Swift 范围数组导致问题

ios - 如何在ios中检查下载的图像是纵向还是横向?

swift - Firebase Swift 3 从组中获取多个值

swift - 如何快速搜索多维字符数组

swift - 如何在 swift didEnterRegion 中获取 UUID

java - SpringBoot : @HystrixCommand not working

ios - 在 iOS 9.0 中以编程方式显示表情符号键盘

ios - 带有半透明导航栏的状态栏背景颜色

java - 我如何处理JPA注释@ManytoMany

java - Docker容器无法将日志发送到docker ELK堆栈