【发布时间】:2020-11-06 06:05:30
【问题描述】:
我正在为客户端-服务器应用程序尝试 GRPC-Swift。 我在客户端和服务器上都使用 GRPC-Swift Client 是一个 iPhone 应用程序,我用 iPhone Simulator 尝试过。
我关注了这个link 用于客户端流式 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)
}
}
【问题讨论】:
-
您如何设置客户端流式 RPC 调用?您需要edit 您的问题以minimal reproducible example 的形式包含所有相关代码,以便使问题成为主题。
标签: ios swift grpc grpc-swift