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

通过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()
}

图片转代码服务由CSDN问答提供 功能建议

我有一个二维字节数组:

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

,我需要通过网络套接字连接进行传输,但是我不知道 找不到找到一种方法来转换此数组,因此我可以在Java客户端上有效地将其转换回二维数组。

  func socketManager(连接* websocket.Conn  ){
 
 fmt.Print(“已建立Websocket连接”)
 
 //为{
 
 //等待来自客户端的消息
确定消息的请求
 messageType,message,err:= connection。  ReadMessage()
 if err!= nil {
 //错误发生时终止连接
 Logger.LogError(err。(错误).Error())
 break 
} 
 
 data:= [  ] [] byte {{104,105},{104,105}} 
 
 //如何发送这个二维字节数组?
 err = connection.WriteMessage(websocket.BinaryMessage,message)
  err!= nil {
 log.Println(“ w  rite:“,err)
 break 
} 
 
 
 
 
 err = connection.WriteMessage(messageType,message)
 if err!= nil {
 log.Println(” write:“  ,err)
 break 
} 
 
} 
 
 //关闭连接
延迟连接。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.

    点赞 打赏 评论

相关推荐 更多相似问题