1

我正在为客户端-服务器应用程序尝试 GRPC-Swift。我在客户端和服务器端都使用 GRPC-Swift 客户端是一个 iPhone 应用程序,我在 iPhone 模拟器上尝试过。

我关注了客户端流式 RPC 的链接。当我从客户端向服务器发送消息时,我在服务器的控制台中收到以下错误消息,

error io.grpc.server_channel_call : unable to determine http version

从服务器中

HTTPProtocolSwitcher.swift

在函数内部func channelRead(context: ChannelHandlerContext, data: NIOAny),它正在检查HTTPProtocolVersion,但它丢失了。

如何从客户端代码发送 HTTPVersion?

更新:

客户代码

import GRPC
import NIO
class HTTPClient {
    
    private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
    
    private var channel: ClientConnection?
    private var client: ChatGuide_ChatGuideClient?
    private var clientCall: ClientStreamingCall<ChatGuide_TextMessage, ChatGuide_TextMessage>?
    
    func connect(host: String, port: Int) throws {
        let channel = ClientConnection.secure(group: self.group)
            .connect(host: host, port: port)
        self.channel = channel
        self.client = ChatGuide_ChatGuideClient(channel: channel)
    }
    
    func disconnect() {
        do {
            self.clientCall?.sendEnd(promise: nil)
            _ = try self.clientCall?.status.wait()
            try self.group.syncShutdownGracefully()
        } catch let error {
            print("\(type(of: self)): Could not shutdown gracefully -", error.localizedDescription)
        }
    }
    
    func initiateClient() {
        let timeAmount = TimeAmount.minutes(1)
        let timeLimit = TimeLimit.timeout(timeAmount)
        let options = CallOptions(timeLimit: timeLimit)
        let call = self.client?.chat(callOptions: options)
        
        call?.response.whenSuccess { (message) in
            print("\(type(of: self)): Message from server -", message.text)
        }
        
        call?.response.whenFailure { (error) in
            print("\(type(of: self)): Response error -", error.localizedDescription)
        }
        self.clientCall = call
    }
    
    func send(text: String) {
        if self.clientCall == nil {
            self.initiateClient()
        }
        let message = ChatGuide_TextMessage.with {
            $0.text = text
        }
        
        self.clientCall?.sendMessage(message, promise: nil)
    }
}
4

2 回答 2

0

从服务器我启动了不安全的服务器,

let server = Server.insecure(group: self.group)

从客户端我已经启动了安全 ClientConnection 作为,

let channel = ClientConnection.secure(group: self.group)

我从这里得到了这个澄清 所以我让 ClientConnection 也不安全,因为,

let channel = ClientConnection.insecure(group: self.group)

在此之后,它现在正在工作。

于 2020-07-18T06:34:00.057 回答
0

嘿维涅什,

我目前正在自己​​学习 gRPC-Swift,所以我希望我能提供服务,而不是进一步搞砸事情。

但是,在我看来,如果您查看 HTTP1ToGRPCServerCodec.swift 文件,您并没有配置 HTTP/1.x 层来传输 Protobuf 数据包

我认为您将对如何调整代码有一个更清晰的想法,很抱歉我无法提供更多详细信息,但是如果没有进一步测试和审查代码库,我自己并不太确定。

最好的问候,如果我确实有帮助,请随时通知我,

干杯

于 2020-07-17T15:04:58.240 回答