英文:
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.Gray
或 image.YCbCr
,它们都是具体的结构体,你可以使用类型断言 img.(Gray)
来确定具体是哪个类型。尝试注册 image.Gray
和 image.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论