Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send HTTPProtocolVersion from Client #899

Closed
jvigneshcs opened this issue Jul 17, 2020 · 3 comments
Closed

Send HTTPProtocolVersion from Client #899

jvigneshcs opened this issue Jul 17, 2020 · 3 comments
Labels

Comments

@jvigneshcs
Copy link

What are you trying to achieve?

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

What have you tried so far?

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

@Lukasa
Copy link
Collaborator

Lukasa commented Jul 17, 2020

How did you configure your server? Is it configured to be secure? It seems like it probably isn't, leading it to mistake the TLS handshake messages for HTTP and close the connection.

@jvigneshcs
Copy link
Author

Here is the Server Code,

import GRPC
import NIO

class HTTPServer {
    
    private let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
    private var host: String?
    private var port: Int?
    
    init(host: String, port: Int) {
        self.host = host
        self.port = port
    }
    
    func run() throws {
        guard let host = self.host else {
            throw HTTPError.invalidHost
        }
        guard let port = self.port else {
            throw HTTPError.invalidPort
        }
        let provider = GRPCProviderServer()
        let server = Server.insecure(group: self.group)
            .withServiceProviders([provider])
            .bind(host: host, port: port)
        
        server.map {
            $0.channel.localAddress
        }.whenSuccess { (address) in
            print("\(type(of: self)): Server started on port \(address!.ipAddress!):\(address!.port!)")
        }
    }
    
    func shutdown() {
        do {
            try self.group.syncShutdownGracefully()
        } catch let error {
            print("\(type(of: self)): Could not shut down gracefully - \(error)")
        }
    }
}

@Lukasa
Copy link
Collaborator

Lukasa commented Jul 17, 2020

Yes, a Client.secure cannot communicate with a Server.insecure. Change the client to an insecure client, or (preferably) the server to a secure server.

@Lukasa Lukasa closed this as completed Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants