tmc / grpc-websocket-proxy

A proxy to transparently upgrade grpc-gateway streaming endpoints to use websockets
MIT License
553 stars 72 forks source link

How to send bytes data from client side? #37

Open yuta-hidaka opened 11 months ago

yuta-hidaka commented 11 months ago

Hi!

I'm trying to send an array byte as a client stream. When I tried to send the data, the grpc got EOF and never got data from the client side. In the client side, I'm using Uint8Array as a byte array.(grapc-gateway converts bytes to Uint8Array in javascript) I'm new to WebSocket, was I missing something?

If you need more detail I will create a minimal reproduction code.

.proto message definition is

message HealthCheckRequest {
    string health = 1;
    bytes buf = 2;
}

client-side is like this.

const byteMessage = new TextEncoder().encode("test message"); // create bytes(Uint8Array) for request
ws.send(JSON.stringify({ health: 'abc', buf: byteMessage})); // this code not working(got EOF)
ws.send(JSON.stringify({ health: 'abc'}));  // without `buf` property work correctly. And I could get data on the server side.

and the server side is like this.


func (s *Server) HealthCheckBiDiStream(stream pb.TranslatorService_HealthCheckBiDiStreamServer) error {
    cnt := 0
    for {
        _, err := stream.Recv()
        if err != nil && err != io.EOF {
            log.Fatal(err)
            return err
        }
        if err == io.EOF {
            return nil
        }

        stream.Send(&pb.HealthCheckResponse{
            Health: fmt.Sprintf("OK %d", cnt),
        })

        if err != nil {
            log.Fatal(err)
            return err
        }
        cnt += 1

    }

}