douchang6770 2018-06-28 19:39
浏览 85

发送带有标题文本的二进制数据

I am making a service in Golang & Gorilla WebSocket which will convert speech to text, by using Microsoft Bing speech service. For reference

https://docs.microsoft.com/en-us/azure/cognitive-services/speech/api-reference-rest/websocketprotocol

javascript implementation

https://github.com/Azure-Samples/SpeechToText-WebSockets-Javascript

Here we need to send text data as well as audio data with headers. I am able to send text data but unable to send audio binary data with headers.

Please suggest me how to send data

Thanks Dinesh Gupta

  • 写回答

1条回答 默认 最新

  • dtml3340 2018-07-04 15:26
    关注

    After a lot of Googling, I make below solution whether it good or worst. Please provide where I may make improvements.

    I have created 2 bytes for a header, then the length of the header string and finally the length of the buffered data. The code is below:

    import(
       "encoding/binary"
       "fmt"
       "time"
       "strings"
       "encoding/json"
    )
    func BufferToBytes(buffer *[]byte, requestId string) *[]byte{
       headersString := GetHeaders("audio", requestId, "audio/x-wav")
       headersString = strings.Replace(headersString, "
    ", "
    ", -1)
       headerInt8Array := []byte(headersString)
       payload := make([]byte,2)
       binary.BigEndian.PutUint16(payload, uint16(len(headerInt8Array)))//till 1st part
       t := make([]byte, len(headerInt8Array))
       for i := 0; i < len(headerInt8Array); i++ {
        t[i] = headerInt8Array[i];
       }
       payload = append(payload, t...)//till 2nd part
       payload = append(payload, *buffer...)//final part
       return &payload;
    }
    
    func GetHeaders(params... string) string{
        header := fmt.Sprintf("path: %s
    ", params[0])
        header += fmt.Sprintf("x-requestid: %s
    ",params[1])//request id
        header += fmt.Sprintf("x-timestamp: %s
    ",GetTimestamp())// timestamp
        header += fmt.Sprintf("content-type: %s
    ",params[2])////content type
        return header
    }
    
    func GetTimestamp() string {
       format := "2006-01-02T15:04:05.000Z"
       return time.Now().UTC().Format(format)
    }
    

    Then this data is accepted by MS bing.

    Thanks

    评论

报告相同问题?

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问