我正在尝试使iOS 上的Apollo 订阅与使用 Action Cable 实现 websockets 的后端一起工作。我了解到 iOS 应用程序需要发送command
和channel
到channel id
后端才能使订阅正常工作(请参见此处)。在初始化 Apollo 的实例时,我尝试在 WebSocketTransport 对象上使用 write func write(_ str: String, force forced: Bool = false, id: Int? = nil)
in函数。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 传输对象的内容,并且我无法订阅该特定频道。如果后端使用的是 Action Cable,我知道如何在 iOS 上使用 Apollo 订阅,并使两者协同工作吗?