4

我正在尝试在我的 iOS 应用程序中从 SoundtrackYourBrand 获得 GraphQL 订阅。查询/突变工作正常,但是当我使用 Web 套接字时,我会收到错误 400 或 403(“无效的 HTTP 升级”)或 1011(无消息),具体取决于我设置授权令牌的方式。

错误 WebSocketError(payload: nil, error: Optional(Starscream.WSError(type: Starscream.ErrorType.upgradeError, message: "Invalid HTTP upgrade", code: 403)), kind: ApolloWebSocket.WebSocketError.ErrorKind.networkError)

错误 WebSocketError(payload: nil, error: Optional(Starscream.WSError(type: Starscream.ErrorType.protocolError, message: "", code: 1011)), kind: ApolloWebSocket.WebSocketError.ErrorKind.networkError)

我正在使用 Apollo 来处理 graphQL API。它适用于突变/查询。我已经按照此处的 Apollo 文档设置了 splitNetwork Transport 。

var wssURL: String = "wss://api.soundtrackyourbrand.com/v2"

    client = {
        let request = URLRequest(url: wssURL)
        let authPayload = ["Authorization" : "Bearer \(token)"]
        let webSocketTransport = WebSocketTransport(request: request, connectingPayload: authPayload)
        
        let client = URLSessionClient()
        let cache = InMemoryNormalizedCache()
        let store = ApolloStore(cache: cache)
        let provider = NetworkInterceptorProvider(store: store, client: client)
        let normalTransport = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: endpointURL, additionalHeaders: ["authorization" : "Bearer \(token)"])
        
        let splitNetworkTransport = SplitNetworkTransport(
            uploadingNetworkTransport: normalTransport,
            webSocketNetworkTransport: webSocketTransport
        )
        
        return ApolloClient(networkTransport: splitNetworkTransport, store: store)
        
    }()

虽然上面是 Apollo 建议进行授权的方式,但在 SoundtrackYourbrand 文档中它说将令牌设置为查询参数:“您不会使用任何标头,因此需要将令牌作为查询参数传递。”

在这种情况下,我应该像下面这样设置 websocket。

下面的代码给出了协议错误 1011,而上面的代码给出了 400/403“无效的 HTTP 升级”错误。

    client = {
        
        let queryItems = [URLQueryItem(name: "Authorization", value: "Bearer \(token)")]
        var urlComps = URLComponents(string: wssURL)!
        urlComps.queryItems = queryItems
        let result = urlComps.url!
        let request = URLRequest(url: result)
        let webSocketTransport = WebSocketTransport(request: request)
        
        let client = URLSessionClient()
        let cache = InMemoryNormalizedCache()
        let store = ApolloStore(cache: cache)
        let provider = NetworkInterceptorProvider(store: store, client: client)
        let normalTransport = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: endpointURL, additionalHeaders: ["authorization" : "Bearer \(token)"])
        
        let splitNetworkTransport = SplitNetworkTransport(
            uploadingNetworkTransport: normalTransport,
            webSocketNetworkTransport: webSocketTransport
        )
        
        return ApolloClient(networkTransport: splitNetworkTransport, store: store)
        
    }()

我的起始网址也应该是:

  1. wss://api.soundtrackyourbrand.com/v2
  2. wss://api.soundtrackyourbrand.com/v2/websocket

查看文档,我认为它应该是 2),但是在 SoundtrackYourbrand工作区中获得订阅后,它使用 1) 我假设我必须使用 1)。

在工作区我使用默认提供的WS URL:wss://api.soundtrackyourbrand.com/v2,然后将授权头设置为“Bearer••••••••••••••••• ••••••••••••••••••••••••••••••••••••••••••...”。这有效,但似乎无法在 Apollo 的 iOS 应用程序中复制它

我究竟做错了什么?

任何帮助将不胜感激!

4

0 回答 0