V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
imherer
V2EX  ›  程序员

http 如何 response byte 数据

  •  
  •   imherer · 2019-05-07 12:33:07 +08:00 · 2673 次点击
    这是一个创建于 1814 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原数据( json ):{"id":1010,"na":"zhangsan"}

    转成 byte:[xxx xx xxx xx xx ...]

    现在想直接通过 http 的方式将 byte send 出去,好像不行啊? 用 golang 和 node.js 分别试了下,最后 send 出去的文件打开之后就直接变成了原 json 串了

    付代码:

    go

    s := `{"id":1010,"na":"zhangsan"}`
    data := []byte(s)
    w.Write(data)
    

    node

    let b = new Buffer(`{"id":1010,"na":"zhangsan"}`)
    response.send(b)
    
    18 条回复    2019-05-08 10:17:51 +08:00
    deepdark
        1
    deepdark  
       2019-05-07 12:44:33 +08:00 via Android
    从你的表述中已经成功实现了,因为已经作为文件打开了。所以你的目的到底是啥,以及为什么要把 json 转成 byte 再返回?
    binux
        2
    binux  
       2019-05-07 12:45:56 +08:00
    作为人类,我都搞不清楚你想要干什么。你还指望机器能搞清楚?
    imherer
        3
    imherer  
    OP
       2019-05-07 12:49:19 +08:00
    @deepdark
    @binux
    其实是项目中用了 protobuf,我看 protobuf 最后就是转成了 byte,我想把 protobuf 格式化后的结果发给客户端
    hearfish
        4
    hearfish  
       2019-05-07 12:54:04 +08:00
    Content-Type: application/octet-stream
    3dwelcome
        5
    3dwelcome  
       2019-05-07 12:55:14 +08:00 via Android
    protobuf 没 http 头部消息,要完全去掉感觉有点难度。
    单纯发 http 的二进制答复应该是可以的,图片都是二进制数据流。
    imherer
        6
    imherer  
    OP
       2019-05-07 12:57:21 +08:00
    @hearfish 这个试过了。 最后得到的结果还是 json 串
    imherer
        7
    imherer  
    OP
       2019-05-07 13:09:12 +08:00
    @3dwelcome 嗯。好像 http 默认把二进制解析了
    msg := &test.Msg{
    id: 1,
    na: "zhangsan"
    }

    data, _ := proto.Marshal(msg)

    w.Write(data)

    最后客户端收到的结果是:{"id":1010,"na":"zhangsan"}
    hearfish
        8
    hearfish  
       2019-05-07 13:09:51 +08:00
    @imherer 你例子发的不就是 json 串么,string 转 bytes 但是内容没变啊。。发一个 protobuf 试试看
    choury
        9
    choury  
       2019-05-07 13:12:17 +08:00 via Android
    你打印的时候用 byte 打印就是 byte 了
    nfroot
        10
    nfroot  
       2019-05-07 18:29:55 +08:00 via Android
    楼主是想把字符都转为 16 进制,对吧
    imherer
        11
    imherer  
    OP
       2019-05-07 19:38:44 +08:00
    @nfroot 可能我的方向错了
    我想实现的有两方面:
    1.能不能把数据压缩一下,在 json 的基础上看能不能怎么压缩下,尽量省流量
    2.降低可读性,json 比 byte 的可读性强太多了
    iceheart
        12
    iceheart  
       2019-05-07 19:54:26 +08:00 via Android
    你想要的是 gzip ?
    imherer
        13
    imherer  
    OP
       2019-05-07 20:15:37 +08:00
    @iceheart 嗯,这个我知道。 我是想在代码里还有没有别的方法能不能把 json 给压缩
    nfroot
        14
    nfroot  
       2019-05-07 21:25:19 +08:00
    @imherer 楼主,问问题的时候想的是什么问的就是什么,别自己胡乱创造名词又不说出自己想要的内容,你看看楼都歪成什么样了。

    开 gzip 就行了,它就会压缩了,传输的时候会压缩!!!!但是你要可读性降低,那就找一种 js 和你后端匹配的压缩算法,每次两边读写的时候都压缩和解压,

    你再发一个主题,你直接就说你的后端语言是什么,传输过程中要压缩内容,前端用户看到的是压缩后的最好是二进制那种,一定要说清楚你会不会写代码,否则白搭,如果你不会写代码,也是白搭。
    yxn1910
        16
    yxn1910  
       2019-05-08 09:48:22 +08:00
    你应该是没有序列化再返回,上面的写法是直接返回 json 字符串。
    如果是用 Protobuf 需要把 Protobuf 对象先序列化再返回,
    譬如 PHP 的官方库是 $protobufObject->serializeToString()
    Golang 中是 proto.Marshal(protobufObject)
    Node 中是 protobufObject.serializeBinary();
    imherer
        17
    imherer  
    OP
       2019-05-08 10:16:50 +08:00
    @nfroot 谢谢善意的提醒
    imherer
        18
    imherer  
    OP
       2019-05-08 10:17:51 +08:00
    @yxn1910 看我 7 楼的回复,序列化之后还是同样的结果。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3495 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:53 · PVG 18:53 · LAX 03:53 · JFK 06:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.