将iOS上的Apollo订阅与用于Websocket后端的Action Cable集成

时间:2019-02-07 14:56:10

标签: swift websocket apollo actioncable starscream

我正在尝试使Apollo subscriptions在iOS上与使用Action Cable实现websocket的后端一起工作。我了解到,iOS应用需要将commandchannelchannel id发送到后端才能进行订阅(see here)。初始化Apollo实例时,我尝试在WebSocketTransport对象的func write(_ str: String, force forced: Bool = false, id: Int? = nil)中使用写WebSocketTransport.swift的功能。在下面,您可以看到我的操作方式。

        let userDefault = UserDefaults.standard
        var authPayloads = Dictionary<String, String> ()
        var authToken = ""
        if let token = userDefault.object(forKey: "token") {
            authToken = "\(token)"
            authPayloads.updateValue(authToken, forKey: "authorization")
        }
       let configuration = URLSessionConfiguration.default
       configuration.httpAdditionalHeaders = authPayloads

        let map: GraphQLMap = authPayloads
        let wsEndpointURL = URL(string: "ws://localhost:8080/subscriptions/\(authToken)")!
        let endpointURL = URL(string: "http://localhost:8080/api")!

        websocket = WebSocketTransport(request: URLRequest(url: wsEndpointURL), connectingPayload: map)
       var channelId = Int(arc4random_uniform(100000))
       websocket?.write(stringify(json: ["command":"subscribe", "identifier": stringify(json: ["channel":"channelName", "channelId": channelId])]))

        let splitNetworkTransport = SplitNetworkTransport(
            httpNetworkTransport: HTTPNetworkTransport(
                url: endpointURL,
                configuration: configuration
            ),
            webSocketNetworkTransport: websocket!
        )
        return ApolloClient(networkTransport: splitNetworkTransport)
    }()

但是,后端在他们的日志中看不到我正在写的WebSocket Transport对象,并且我无法订阅该特定通道。知道后端如何使用Action Cable时如何在iOS上使用Apollo订阅,并使两者一起工作吗?

1 个答案:

答案 0 :(得分:-1)

目前唯一的解决方案是使用 https://plot.ly/python/line-and-scatter/ 接收流,并使用来自 ApolloClient 的变异/查询。

不懈! Apollo iOS 不知道如何与ActionCable频道进行通信,此问题在Apollo-iOS Swift-ActionCableClient和您的问题{{ 3}}