GRPC-Swift Send HTTPProtocolVersion from Client

945 views Asked by At

I'm trying GRPC-Swift for Client-Server application. I'm using GRPC-Swift for both Client and Server Client is an iPhone application, which I tried with iPhone Simulator.

I followed this link for Client-side streaming RPC. When I send message to Server from Client, I got the following error message in the console from Server,

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

From the Server in the

HTTPProtocolSwitcher.swift

inside the function func channelRead(context: ChannelHandlerContext, data: NIOAny), it is checking for HTTPProtocolVersion, and it is missing.

How to send the HTTPVersion from the Client code?

Update:

Client Code

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)
    }
}
2

There are 2 answers

0
Jonathan K. Farber On

Hey Vignesh,

I am currently learning gRPC-Swift myself, so I hope I will be of service and not muck things further.

However, it looks to me that you are not configuring the HTTP/1.x layer in order to transfer Protobuf packets, if you take a look at the HTTP1ToGRPCServerCodec.swift file Here

I think you will have a much clearer idea of how to adjust your code, I am sorry I can't provide more details, however not being too sure myself without further testing and reviewing the codebase.

Best regards and keep me posted if indeed i was helpful,

cheers

0
Vignesh On

From the Server I have initiated insecure Server as,

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

From the Client I have initiated secure ClientConnection as,

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

And I got this clarification from here So I made the ClientConnection also insecure as,

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

And after this it is working now.