通过 goRPC 发送图像

huangapple go评论99阅读模式
英文:

sending and image through goRPC

问题

我想使用golang包valyala goRPC通过rpc发送一张图片,但是在服务器端接收图片类型时遇到了一些问题。

这是我的客户端代码,它接收一个.jpg图片,解码后通过rpc发送:

c := &gorpc.Client{
    // 服务器的TCP地址。
    Addr: "127.0.0.1:12345",
}
c.Start()
reader, err := os.Open("barranco.jpg")
if err != nil{
    log.Fatal(err)
}

defer reader.Close()

img, _, err := image.Decode(reader)
if err != nil {
    log.Fatal(err)
}


fmt.Print("按回车键发送。\n")
bufio.NewReader(os.Stdin).ReadBytes('\n') 

gorpc.RegisterType(img)

resp, err := c.Call(img)

在这段代码中,我获取了名为barranco.jpg的图片,解码后在客户端注册了该类型,然后再发送给服务器。
我的问题是,我该如何在服务器端注册这个类型?我一直在服务器端遇到相同的失败,因为我无法注册该图片类型 /:

提前感谢。

英文:

I want to send an image through rpc using the golang packet valyala goRPC and I'm having some problems to receive the image type in the server.

This is my client code, who takes an .jpg image, decodes it and send it through rpc:

c := &gorpc.Client{
  // TCP address of the server.
   Addr: "127.0.0.1:12345",
}
c.Start()
reader, err := os.Open("barranco.jpg")
if err != nil{
    log.Fatal(err)
}

defer reader.Close()

img, _, err := image.Decode(reader)
if err != nil {
    log.Fatal(err)
}


fmt.Print("Pulsa intro para enviar.\n")
bufio.NewReader(os.Stdin).ReadBytes('\n') 

gorpc.RegisterType(img)

resp, err := c.Call(img)

So in this code, I take an image called barranco.jpg, I decode it but before sending it to the server I register the type on the client.
My problem is, how can I register that type in the server? I always get the same fail in the server because I can't register that image type /:

Thanks in advance.

答案1

得分: 1

image.Image 类型是一个接口,因此无法注册它,它太抽象了。然而,JPEG 的底层实现可以是 image.Grayimage.YCbCr,它们都是具体的结构体,你可以使用类型断言 img.(Gray) 来确定具体是哪个类型。尝试注册 image.Grayimage.YCbCr,然后断言你拥有的类型,并以具体类型发送。

英文:

Type image.Image is an interface so you can't register it, it's too abstract. Underlaying implementations for jpeg however are either image.Gray or image.YCbCr, both concrete structs, you may type assert img.(Gray) to decide which. Try to register both image.Gray and image.YCbCr, assert which you have have and then send as concrete type.

huangapple
  • 本文由 发表于 2016年4月24日 00:26:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/36813361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定