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

时间:2017-10-27 11:13:44

标签: 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 '委托方法被调用。

我还没有使用过WebSocket。因此,无法理解原因是什么。任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:2)

您可以使用StopmClientLib建立套接字连接,也可以使用订阅方法。

https://github.com/wrathchaos/StompClientLib