duanqiang9212 2018-11-30 15:41
浏览 195
已采纳

JSON大小超过65500个字符时,golang中的Chrome本机消息传递主机失败

I am trying to write a native messaging host for chrome in golang. For this purpose, I tried using chrome-go as well as chrome-native-messaging packages. Both presented with the same problem as explained below.

Here is the code. I have added the relevant parts from the chrome-go package to the main file instead of importing it for easy understanding.

The following code actually works when I send a json message to it like {content:"Apple Mango"}. However, it stops working once the length of the json goes over approximately 65500 characters, give or take a 100 characters. There is no error output either.

package main

import (
  "encoding/binary"
  "encoding/json"
  "fmt"
  "io"
  "os"
)

var byteOrder binary.ByteOrder = binary.LittleEndian

func Receive(reader io.Reader) ([]byte, error) {
   // Read message length in native byte order
   var length uint32
   if err := binary.Read(reader, byteOrder, &length); err != nil {
       return nil, err
   }

// Return if no message
if length == 0 {
    return nil, nil
}

// Read message body
received := make([]byte, length)
if n, err := reader.Read(received); err != nil || n != len(received) {
    return nil, err
}
return received, nil
}

type response struct {
    Content string `json:"content"`
}

func main() {

  msg, err := Receive(os.Stdin)
  if err != nil {
    panic(err)
  }
  var res response
  err = json.Unmarshal([]byte(msg), &res)
  if err != nil {
     panic(err)
  }
  fmt.Println(res.Content)
 }

For those interested in testing, I have set up a repository with instructions. Run the following

  git clone --depth=1  https://tesseract-index@bitbucket.org/tesseract-index/chrome-native-messaging-test-riz.git && cd chrome-native-messaging-test-riz
 ./json2msg.js < test-working.json | go run main.go
 ./json2msg.js < test-not-working.json | go run main.go

You will see that test-not-working.json gives no output, although its difference with test-working.json is a few hundred characters only.

What is the issue here?

  • 写回答

1条回答 默认 最新

  • douzhan1868 2018-11-30 16:41
    关注

    There is a limitation of a pipe buffer which varies across systems. Mac OS X, for example, uses a capacity of 16384 bytes by default.

    You can use this bash script to check your buffer capacity:

    M=0; while printf A; do >&2 printf "$((++M)) B"; done | sleep 999
    

    So it is not related to go, because I tried to change your code to read from file and Unmarshal and it worked:

    func main() {
        reader, err := os.Open("test-not-working.json")
        if err != nil {
            panic(err)
        }
    
        var res response
        decoder := json.NewDecoder(reader)
        err = decoder.Decode(&res)
        if err != nil {
            panic(err)
        }
    
        fmt.Println(res.Content)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建