duanlie2709 2018-01-05 16:17
浏览 1287
已采纳

通过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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止