duanlie2709
2018-01-05 16:17 阅读 253
已采纳

通过Golang中的Websocket发送和接收[] []字节

I have a two-dimensional byte array:

data := [][]byte{{104, 105}, {104, 105}}

which I need to transport through a websocket connection, but I don't find a way how to convert this array so I can efficiently reconvert it back to a two-dimensional array on my java client.

func socketManager (connection *websocket.Conn ){

    fmt.Print("Websocket connection established ")

    //determining the request
    for {

    //awaiting messages from clients
    messageType, message, err := connection.ReadMessage()
    if err != nil {
        //ending connection when error accursed
        Logger.LogError(err.(error).Error())
        break
    }

        data := [][]byte{{104, 105}, {104, 105}}

        //How can I send this two dimensional byte array?
        err = connection.WriteMessage(websocket.BinaryMessage, message)
        if err != nil {
            log.Println("write:", err)
            break
        }




    err = connection.WriteMessage(messageType, message)
    if err != nil {
        log.Println("write:", err)
        break
    }

    }

    //closing the connection
    defer connection.Close()
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dsjpqpdm620596 dsjpqpdm620596 2018-01-05 16:43

    Multi-dimensional arrays are usually defined in terms of both their dimensions (i.e. "width" and "height") and their contents (the entire list of individual items which should be broken up into constituent "rows" or "columns"). However, if the constituent arrays might have different lengths then you may need another approach entirely.

    Your task is to decide how you want to serialize this data so that your client can deserialize it and to emphasize any critical aspects of this transmission (e.g. data size, performance, ease of interoperability, etc).

    Here are some possible solutions:

    1. Matrix dimensions and contents. If your data will always have a consistent "width" and "height" then you could use a raw encoding to serialize two uint16s (or other appropriate size) representing those values and then a sequence of the raw bytes. To deserialize, the client would just need to read two integers to know the dimensions of the matrix and could then populate the rest of the data accordingly. An example message might look like this (as hexadecimal bytes):

      0002 0002 6869 6869

    2. Length and contents. If each sub-array could have a different length then you could simply raw encode a sequence of tuples of "length" (e.g. as uint32) and raw byte contents, e.g. (as hexadecimal bytes):

      0000 0002 6869 0000 0002 6869

    3. JSON and Base64. If interoperability and ease of integration are more important (and large data sizes are not a major concern) then you could simply serialize your data as a JSON array of strings, each of which is a Base64 encoded byte array. This way, the client need only deserialize the JSON array and Base64 decode each element. This solution also easily allows for varying size sub-arrays. An example message might look like this (as a JSON string):

      ["aGk=","aGk="]

    Ultimately, go has many builtin encoding packages that you can use to serialize this data. There is no single "right" answer, so your implementation choice should be driven by your business needs.

    点赞 评论 复制链接分享

相关推荐