我正在为客户端-服务器应用程序尝试 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)
}
}